简述:定高100px,左右宽度均为200px,中间自适应。
公共代码块
*{padding: 0; margin: 0;}
html, body {padding: 0; margin: 0;}
.wrap {margin-bottom: 10px;}
1、浮动—float
<style>
.wrap > div {height: 200px; text-align: center; line-height: 200px; }
.float .left {
width: 200px;
float: left;
background-color: #f00;
}
.float .center {
background-color: green;
}
.float .right {
width: 200px;
float: right;
background-color: #ff0;
}
</style>
<div class="wrap float">
<div class="left">我是左边栏</div>
<div class="right">我是右边栏</div>
<div class="center">我是float</div>
</div>
float介绍
代码以及浏览器展示效果如上,浏览器拉伸也不会影响,可实现效果。但有两点需要注意:
1、页面布局的结构需如上,如果结构变动,所需效果不会实现。
2、注意清除浮动,否则会影响页面其他的结构。
3、若处理得当,兼容性非常好。
2、定位—position
<style>
.position .left {
width: 200px;
position: absolute;
left: 0;
background-color: #f00;
}
.position .center {
background-color: green;
position: absolute;
left: 200px;
right: 200px;
}
.position .right {
width: 200px;
position: absolute;
right: 0;
background-color: #ff0;
}
</style>
<div class="wrap position">
<div class="left">我是左边栏</div>
<div class="center">我是position</div>
<div class="right">我是右边栏</div>
</div>
position介绍
代码以及浏览器展示效果如上,浏览器拉伸也不会影响,可实现效果。但有两点需要注意:
1、页面布局的结构可正常写,如果布局中间位置与右部分对调,那么中间样式无需定位。
2、文案脱离文档流,实际应用是需要注意。
3、弹性盒子布局—flex
<style>
.flexbox {display: flex; margin-top: 220px;}
.flexbox .left {
width: 200px;
background-color: #f00;
}
.flexbox .center {
flex: 1;
background-color: green;
}
.flexbox .right {
width: 200px;
background-color: #ff0;
}
</style>
<div class="wrap flexbox">
<div class="left">我是左边栏</div>
<div class="center">flexbox</div>
<div class="right">我是右边栏</div>
</div>
flex
代码以及浏览器展示效果如上,浏览器拉伸也不会影响,可实现效果。需要注意:
1、不兼容ie8以下版本,若不考虑该情况,flex是不错的选择。
2、由于受第二种定位的方法影响,所以设置margin-top;否则会被盖住。
4、表格布局—table
<style>
.table {display: table; width: 100%;}
.table > div {display: table-cell}
.table .left {
width: 200px;
background-color: #f00;
}
.table .center {
background-color: green;
}
.table .right {
width: 200px;
background-color: #ff0;
}
</style>
<div class="wrap table">
<div class="left">我是左边栏</div>
<div class="center">table</div>
<div class="right">我是右边栏</div>
</div>
table介绍
代码以及浏览器展示效果如上,浏览器拉伸也不会影响,可实现效果,而且兼容性相对很好。
5、网格布局—grid
<style>
.grid {
display: grid;
width: 100%;
grid-template-rows: 200px;
grid-template-columns: 200px auto 200px;
}
.grid .left {
background-color: #f00;
}
.grid .center {
background-color: green;
}
.grid .right {
background-color: #ff0;
}
</style>
<div class="wrap grid">
<div class="left">我是左边栏</div>
<div class="center">grid</div>
<div class="right">我是右边栏</div>
</div>
grid网格布局介绍
代码以及浏览器展示效果如上,浏览器拉伸也不会影响,可实现效果,CSS3属性,若不考虑IE8,可以使用,而且网格布局有很多种形式,九宫格之类的样式都可以实现。
总结
以上为5种实现效果,各有利弊,开发过程中可根据实际情况使用对应的方法。