文章来自:https://blog.csdn.net/Cinderella_hou/article/details/52156333
所谓三列自适应布局指的是两边定宽,中间block宽度自适应。这道题在今年网易内推前端工程师面试的时候也被问到。 我这里主要分为两大类,一类是基于position传统的实现,一类是基于css3新特性弹性盒模型布局实现。
1. 基于传统的position和margin等属性进行布局
1).绝对定位法
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>layout_box</title>
- <link rel="stylesheet" type="text/css" href="../css/layout_box.css">
- </head>
- <body>
- <h3>实现三列宽度自适应布局</h3>
- <div id = "left">我是左边</div>
- <div id = "right">我是右边</div>
- <div id = "center">我是中间</div>
- </body>
- </html>
- html,body{ margin: 0px;width: 100%; }
- h3{height: 100px;margin:20px 0 0;}
- #left,#right{width: 200px;height: 200px; background-color: #ffe6b8;position: absolute;top:120px;}
- #left{left:0px;}
- #right{right: 0px;}
- #center{margin:2px 210px ;background-color: #eee;height: 200px; }
2).使用自身浮动法
- <h3>使用自身浮动法定位</h3>
- <div id = "left_self">我是左边</div>
- <div id = "right_self">我是右边</div>
- <div id = "center_self">我是中间</div>
- #left_self,#right_self{ width: 200px;height: 200px; background-color: #ffe6b8 }
- #left_self {float: left;}
- #right_self{float: right;}
- #center_self{margin: 0 210px;height: 200px; background-color: #a0b3d6}
3). 圣杯布局
- <h3>使用margin负值法进行布局</h3>
- <div id = "wrap">
- <div id = "center"></div>
- </div>
- <div id = "left_margin"></div>
- <div id = "right_margin"></div>
- #wrap{ width: 100%;height: 100px; background-color: #fff;float: left;}
- #wrap #center{ margin:0 210px; height: 100px;background-color: #ffe6b8; }
- #left_margin,#right_margin{ float: left;width: 200px;height: 100px;background-color: darkorange }
- #left_margin {margin-left: -100%; background-color: lightpink}
- #right_margin{margin-left: -200px;}
2 , css3新特性
在外围包裹一层div,设置为display:flex;中间设置flex:1;但是盒模型默认紧紧挨着,可以使用margin控制外边距。
代码:
- <div id = "box">
- <div id = "left_box"></div>
- <div id = "center_box"></div>
- <div id = "right_box"></div>
- </div>
- #box{width:100%;display: flex; height: 100px;margin: 10px;}
- #left_box,#right_box{width: 200px;height: 100px; margin: 10px; background-color: lightpink}
- #center_box{ flex:1; height: 100px;margin: 10px; background-color: lightgreen}
效果图如下:
请用css实现左侧固定宽度20%,右侧固定宽度200px,中间自适应的页面布局
解法1--用圣杯式布局:
<style>
.container{
overflow:hidden;
}
.wrap{
float:left;
width: 100%;
background: gray;
}
.middle{
margin-left: 20%;
margin-right: 200px;
background:red;
}
.left{
width: 20%;
float:left;
margin-left: -100%;
background:blue;
}
.right{
width: 200px;
float:left;
background:yellow;
margin-left: -200px;
}
</style>
</head>
<body>
<div class='container'>
<div class='wrap'>
<div class='middle'>middlettttttttttttttttttttttttttttttttttttttt</div>
</div>
<div class='left'>left</div>
<div class='right'>right</div>
</div>
</body>
解法2--弹性盒
<style>
.wrap{
display:flex;
}
.middle{
flex: 1;
background:gray;
}
.left{
width: 20%;
background:blue;
}
.right{
width: 200px;
background:green;
}
</style>
</head>
<body>
<div class='wrap'>
<div class='left'>left</div>
<div class='middle'>middlettttttttttttttttttttttttttttttttttttttt</div>
<div class='right'>right</div>
</div>
</body>
</html>