圣杯布局
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.container {
padding-left: 200px;
padding-right: 200px;
}
.left {
float: left;
width: 200px;
height: 400px;
background: red;
margin-left: -100%;
position: relative;
left: -200px;
}
.center {
float: left;
width: 100%;
height: 400px;
background: yellow;
}
.right {
float: left;
width: 200px;
height: 400px;
background: blue;
margin-left: -200px;
position: relative;
right: -200px;
}
</style>
</head>
<body>
<section class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</section>
</body>
</html>
复制代码
步骤
- 先写
center
部分,width 100%
center
,left
,right
都是左浮动- 父容器
container
设置padding-left
和padding-right
- 设置
margin-left
为负值让left
和right
部分回到与center
部分同一行 - 设置相对定位,让
left
和right
部分移动到两边
缺点
- center部分的最小宽度不能小于left部分的宽度
- 其中一列内容高度拉长,其他两列的高度不会自动填充
双飞翼布局
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.container {
min-width: 600px;
}
.left {
float: left;
width: 200px;
height: 400px;
background: red;
margin-left: -100%;
}
.center {
float: left;
width: 100%;
height: 400px;
background: yellow;
}
.center .inner {
margin: 0 200px;
}
.right {
float: left;
width: 200px;
height: 400px;
background: blue;
margin-left: -200px;
}
</style>
</head>
<body>
<section class="container">
<div class="center">
<div class="inner">双飞翼布局</div>
</div>
<div class="left"></div>
<div class="right"></div>
</section>
</body>
</html>
复制代码
步骤
- 先写
center
部分,width 100%
center
,left
,right
都是左浮动center
部分增加一个内层div
,并设margin-left
,margin-right
- 设置
margin-left
为负值让left
和right
部分回到与center
部分同一行
缺点
- 多加一层
dom
树节点
总结
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END