1、浮动布局 float
<style>
.container{
width:100%;
height:100px;
}
.left{
width:250px;
height:100%;
background:red;
float:left;
}
.right{
width:250px;
height:100%;
background:pink;
float:right;
}
.center{
height:100%;
background:blue;
}
</style>
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center">浮动布局</div> //中间这一部分必须写在最后面
</div>
优点:简单
缺点:当中间部分加载内容较多时,用户体验不好;当浏览器窗口所小到一定程度时,会发生错位
2、绝对定位布局 position
<style>
.left{
width:250px;
height:100px;
background:red;
position: absolute;
left: 0px;
top: 0px;
}
.right{
width:250px;
height:100px;
background:pink;
position: absolute;
right: 0px;
top: 0px;
}
.center{
height:100px;
background:blue;
}
</style>
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div> //中间这一部分必须写在最后面
</div>
优缺点同1
3、flex布局:个人超级喜欢的一种
<style>
.container{
width:100%;
display: flex;
}
.left{
width:250px;
height:100px;
background:red;
}
.right{
width:250px;
height:100px;
background:pink;
}
.center{
height:100px;
background:blue;
flex: 1;
}
</style>
<div class="container">
<div class="left"></div>
<div class="center">flex布局</div> //区分上面两种的位置
<div class="right"></div>
</div>
4、table布局
<style>
.container{
width:100%;
display: table;
}
.container div{
display: table-cell;
}
.left{
width:250px;
height:100px;
background:red;
}
.right{
width:250px;
height:100px;
background:pink;
}
.center{
height:100px;
background:blue;
}
</style>
<div class="container">
<div class="left"></div>
<div class="center">table布局</div>
<div class="right"></div>
</div>
5、Grid网格布局
<style>
.container{
width:100%;
display: grid;
grid-template-rows: 100px; /*设置行高*/
grid-template-columns: 250px auto 250px;/*设置列数属性*/
}
.left{
background:red;
}
.right{
background:pink;
}
.center{
background:blue;
}
</style>
<div class="container">
<div class="left"></div>
<div class="center">Grid网格布局</div>
<div class="right"></div>
</div>
6、圣杯布局
7、双飞翼布局
8、BFC规则
参考文档:
https://blog.csdn.net/ganyingxie123456/article/details/77054124
https://www.jianshu.com/p/8b308d63fe23