高度固定,左右300px,中间自适应
左 | 中 | 右 |
第一种: float
浮动会脱离文档流,要清除浮动,兼容性比较好
第二种: position
定位会脱离文档流,所以子元素也会脱离文档流,写法比较便捷
第三种: flex
不兼容IE8,最常用,兼容性也好
第四种: table
操作繁琐,兼容性好,兼容IE8
第五种: grid
代码量少,新出的属性
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
html *{
padding: 0;
margin: 0;
}
.layout{
margin-top: 20px;
}
.layout div{
min-height: 100px;
}
</style>
<style>
/*float*/
.layout .float .left{
float: left;
width: 300px;
background: red;
}
.layout .float .center{
background: green;
}
.layout .float .right{
float: right;
width: 300px;
background: blue;
}
</style>
<style>
/*position*/
.layout .position div{
position: absolute;
}
.layout .position .left{
left: 0;
width: 300px;
background: red;
}
.layout .position .center{
left: 300px;
right: 300px;
background: green;
}
.layout .position .right{
right: 0;
width: 300px;
background: blue;
}
</style>
<style>
/*flex*/
.layout .flex{
display: flex;
margin-top: 140px;
}
.layout .flex .left{
width: 300px;
background: red;
}
.layout .flex .center{
flex: 1 auto;
background: green;
}
.layout .flex .right{
width: 300px;
background: blue;
}
</style>
<style>
/*table*/
.layout .table{
display: table;
width: 100%;
height: 100px;
}
.layout .table .left{
display: table-cell;
width: 300px;
background: red;
}
.layout .table .center{
display: table-cell;
background: green;
}
.layout .table .right{
display: table-cell;
width: 300px;
background: blue;
}
</style>
<style>
/*grid*/
.layout .grid{
display: grid;
width: 100%;
grid-template-columns: 300px auto 300px;
grid-template-rows: 100px;
}
.layout .grid .left{
background: red;
}
.layout .grid .center{
background: green;
}
.layout .grid .right{
background: blue;
}
</style>
</head>
<body>
<!--float-->
<section class="layout">
<arcticle class="float">
<div class="left"></div>
<div class="right"></div>
<div class="center">
这是中间float
</div>
</arcticle>
</section>
<!--position-->
<section class="layout">
<arcticle class="position">
<div class="left"></div>
<div class="center">
这是中间position
</div>
<div class="right"></div>
</arcticle>
</section>
<!--flex-->
<section class="layout">
<arcticle class="flex">
<div class="left"></div>
<div class="center">
这是中间flex
</div>
<div class="right"></div>
</arcticle>
</section>
<!--table-->
<section class="layout">
<arcticle class="table">
<div class="left"></div>
<div class="center">
这是中间table
</div>
<div class="right"></div>
</arcticle>
</section>
<!--grid-->
<section class="layout">
<arcticle class="grid">
<div class="left"></div>
<div class="center">
这是中间grid
</div>
<div class="right"></div>
</arcticle>
</section>
</body>
</html>
结果如图:
问题思考:
1、上高度固定,下高度自适应(可以用position,table,flex,grid)
2、下高度固定,上高度自适应(可以用position,table,flex,grid)
3、左宽度固定,右自适应(可以用float,position,table,flex,grid)
4、右宽度固定,左自适应(可以用float,position,table,flex,grid)
5、上下高度固定,中间自适应(可以用table,flex,position,grid)