首先看我的div布局:
图片有6个div有序排列,位置上下左右不一。
方法1:最简单的做法是设置每个div的width:px;height:px;
比如:
width:1200px;
height:800px;
但是这种方法在不同尺寸的电脑上就无法完全显示div或者div不能充满整个页面。
方法2:设置div的width:%;height:%;**
比如:
width:20%;
height:60%;
这种方法无论对于不同的电脑尺寸都能全屏显示,本文就选用这种方法。
- step1:
建立HTML,添加代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My First Web Demo!</title>
<style type="text/css" media="all">@import "css/master.css";</style>
<!--这是让浏览器知道你是使用 CSS1 样式规则。-->
</head>
<body>
<div id="page-container">
<div id="main-nav">main-nav
</div>
<div id="multi">
<div id="content">content
</div>
<div id="footer">
<div id="desk"> desk </div>
<div id="block"> block</div>
<div id="shelf"> shelf
</div>
</div>
</div>
<div id="sidebar-a">sidebar-A</div>
</div>
</body>
</html>
- step2:
建立CSS文件,添加代码如下:
#page-container
{
//clear:both;
overflow: hidden;
width: 100%;
height:100%;
position: relative;
margin: auto;
<!--是左右自适应两边距离一样--居中,-->
>
margin: auto;
background: red;
text-align: center;
}
html,body
{
<!--盒子和浏览器的顶端有空隙。由于浏览器的默认的填充和边界造成的。消除空隙-->
margin: auto;
height:100%;
width:100%;
font-family:Sans-serif;
font-size: 16px;
color: #666666;
background: #ffffff;
}
<!--加入类,可以使页面上任意属于hidden类的元素(element)不显示-->
.hidden {display: none;}
#main-nav
{
background: khaki;
/*position:absolute;会导致main-nav高度不符*/
height:20%;
width:100%;
border-style: dotted;/*点线*/
overflow: hidden;
}
#multi
{
background: black;
height:60%;
width:100%;
float:auto;
font-family: "黑体";
text-decoration: underline;
}
#content
{
background:cyan ;
clear: both;
height:100%;
width:20%;
float:left;
font-family: "黑体";
text-decoration: underline;
border-style:none;
}
#footer
{
background: green;
width:80%;
height:100%;
float:right;
}
#desk
{
clear: both;
margin:auto;
background: pink;
width:100%;
height:20%;
}
#shelf
{
background: yellow;
float:left;
width:60%;
height:80%;
//border-style:solid;
}
#block
{
clear: both;
background: lavender ;
float:right;
width:40%;
height:80%;
//border-style:solid;
}
#sidebar-a
{
clear:both;
background: tomato;
height:20%;
text-decoration: underline;
font-family: "华文新魏";
border-style:dashed; /*虚线*/
}