布局搭框架###
浮动 vs 负margin
对于相邻的两个浮动元素,如果因为空间不够导致第二个浮动元素下移,可以通过给第二个浮动元素设置 margin-left: 负值 来让第二个元素上移,其中 负值 大于等于元素上移后和第一个元素重合的临界值
单列布局
一栏布局
<style>
.layout{
width: 960px;
margin: 0 auto;
} #header{
height: 60px;
background: red;
} #content{
height: 400px;
background: blue;
} #footer{
height: 50px;
background: yellow;
}
</style>
<div id="header" class="layout">头部</div>
<div id="content" class="layout">内容</div>
<div id="footer" class="layout">尾部</div>
通栏布局优化
<style>
.layout{
width: 960px;
margin: 0 auto;
}
body{
min-width: 960px;
}#header{
height: 60px;
background: red;
}#content{
height: 400px;
background: blue;
}#footer{
height: 50px;
background: yellow;
}
</style>
<div id="header">
<div class="layout">头部</div>
</div>
<div id="content" class="layout">内容</div>
<div id="footer">
<div class="layout">尾部</div>
</div>
内部元素水平居中
.parent{text-align:center;}
.child{display: inline-block;}
双列布局(一列固定宽度,另外一列自适应宽度)
左固定,右自适应——注意清父容器的浮动 注意margin-left的用法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>两列</title>
<style>
#content:after{
content: '';
display: block;
clear: both;
}
.aside{
width: 200px;
height: 500px;
background: yellow;
float: left;
}
.main{
margin-left: 210px;
height: 400px;
background: red;
}#footer{
background: #ccc;
}
</style>
</head>
<body>
<div id="content">
<div class="aside">aside</div>
<div class="main">content</div>
</div>
<div id="footer">footer</div>
</body>
</html>
三列布局
两侧两列固定宽度,中间列自适应宽度
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>三列</title>
<style> #content:after{
content: '';
display: block;
clear: both;
}
.menu{
width: 100px;
height: 500px;
background: pink;
float: left;
}
.aside{
width: 200px;
height: 500px;
background: yellow;
float: right;
}
.main{
margin-left: 110px; /为什么要加margin-left/
margin-right: 210px;
height: 400px;
background: red;
}#footer{
background: #ccc;
}</style>
</head>
<body>
<div id="content">
<div class="menu">aside</div>
<div class="aside">aside</div>
<div class="main">content</div>
</div>
<div id="footer">footer</div>
</body>
</html>
通用九宫格
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<style>
ul,li{
margin: 0;
padding: 0;
list-style: none;
}
.ct{
overflow:hidden;
width: 640px;
border:dashed 1px orange;
margin: 0 auto;
}
.ct .item{
float: left;
width:200px;
height:200px;
margin-right: 20px;
margin-top: 10px;
background: red;
}
.ct>ul{
margin-right: -20px;
}</style>
<div class="ct">
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
<li class="item">5</li>
<li class="item">6</li>
<li class="item">7</li>
<li class="item">8</li>
</ul>
</div>
</body>
</html>
圣杯布局:缺点浏览器拉小会出现问题
注意写法 margin-left:-100%
.aside{
width: 100px; /1/
height: 300px; /1/
background: red; /1/
margin-left: -100%; /4/
position: relative; /6/
left: -100px; /6/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>圣杯布局</title>
<style> #content:after{
content: ''; /8/
display: block; /8/
clear: both; /8/
} #content{
padding-left: 100px; /5/
padding-right: 150px; /5/
}
.aside, .main, .extra{
float: left; /2/
}
.aside{
width: 100px; /*1*/
height: 300px; /*1*/
background: red; /*1*/
margin-left: -100%; /*4*/
position: relative; /*6*/
left: -100px; /*6*/
}
.extra{
width: 150px; /*1*/
height: 300px; /*1*/
background: yellow; /*1*/
margin-left: -150px; /*5*/
position: relative; /*7*/
left: 150px; /*7*/
}
.main{
height: 350px; /*1*/
background: blue; /*1*/
width: 100%; /*3*/
}
</style>
</head>
<body>
<div id="content">
<div class="main">main</div>
<div class="aside">aside</div>
<div class="extra">extra</div>
</div>
</body>
</html>
双飞翼布局
<style> #content:after{
content: ''; /8/
display: block; /8/
clear: both; /8/
}#content{
}
.aside, .main, .extra{
float: left; /2/
}
.aside{
width: 100px; /1/
height: 300px; /1/
background: red; /1/
margin-left: -100%; /4/
}
.extra{
width: 150px; /1/
height: 300px; /1/
background: yellow; /1/
margin-left: -150px; /5/
}
.main{
/* background: blue; / /第1步添加,第7步注释掉/
/ height: 350px; / /第1步添加,第7步注释掉/
width: 100%; /3/
}
.wrap{
margin-left: 100px; /6/
margin-right: 150px; /6/
background: blue; /7/
height: 350px; /7*/
}
</style>
<div id="content">
<div class="main">
<div class="wrap">main</div>
</div>
<div class="aside">aside</div>
<div class="extra">extra</div>
</div>