常用布局
圣杯布局
html结构
<body>
<div class="container clearfix">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
<div>
<div>123</div>
<div>123</div>
<div>123</div>
</div>
</body>
css:
container
把左右两边距离空出来
.container {
padding: 0 200px;
}
center
设置widh为100%,即充满整个容器
.center {
100%;
}
浮动
设置left,center,right左浮动。
清除container浮动,保证后面的div正常布局
.left, .center, .right {
float: left;
}
.clearfix::after {
display: block;
height: 0;
content: "";
visibility: hidden;
clear: both
}
.clearfix {
*zoom: 1;
}
left
使用margin-left: -100%;
.left {
position: relative;
margin-left: -100%;
left: -200px;
}
right
使用margin-right: -200px;;
.right {
margin-right: -200px;
}
为什么不用相对定位来移动
.right {
position: relative;
left: -200px;
}
会在第二行向左移动200px,不能和center同行
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style type="text/css">
html,
body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}
.container {
padding: 0 200px;
}
.left,
.right {
200px;
min-height: 200px;
}
.left {
background: lightgreen;
}
.right {
background: lightgrey;
}
.center {
100%;
min-height: 400px;
background: lightsalmon;
}
.left,
.center,
.right {
float: left;
}
.left {
position: relative;
margin-left: -100%;
left: -200px;
}
.right {
margin-right: -200px;
/* position: relative; */
/* left: -200px; */
}
.clearfix::after {
display: block;
height: 0;
content: "";
visibility: hidden;
clear: both;
}
.clearfix {
*zoom: 1;
}
</style>
</head>
<body>
<div class="container clearfix">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
<div>
<div>123</div>
<div>123</div>
<div>123</div>
</div>
</body>
</html>
双飞翼布局
HTML结构
<body>
<div class="container">
<div class="cemter"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</body>
container
.container {
100%;
background: lightseagreen;
height: 700px;
}
center
.center {
min-height: 400px;
background: lightpink;
margin: 0 200px;
}
浮动
.container, .left, .right {
float: left;
}
.clearfix::after {
display: block;
height: 0;
content: "";
visibility: hidden;
clear: both;
}
.clearfix {
*zoom: 1;
}
left
.left {
margin-left: -100%;
200px;
min-height: 400px;
background: lightskyblue;
}
right
.right {
margin-left: -200px;
200px;
min-height: 400px;
background: lightgoldenrodyellow;
}
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style type="text/css">
html,
body {
100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
100%;
background: lightseagreen;
height: 700px;
}
.center {
min-height: 400px;
background: lightpink;
margin: 0 200px;
}
.container,
.left,
.right {
float: left;
}
.left {
margin-left: -100%;
200px;
min-height: 400px;
background: lightskyblue;
}
.right {
margin-left: -200px;
200px;
min-height: 400px;
background: lightgoldenrodyellow;
}
.clearfix::after {
display: block;
height: 0;
content: "";
visibility: hidden;
clear: both;
}
.clearfix {
*zoom: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="center"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</body>
</html>
图解: