代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>三列布局</title>
<style>
*{
margin: 0;
padding: 0;
}
.layout{
margin-top: 20px;
}
.layout article div{
min-height: 100px;
}
</style>
</head>
<body>
<!-- 方法一:float解决方案 -->
<section class="layout float">
<style>
.layout.float .left{
float: left;
width: 300px;
background-color: red;
}
.layout.float .right{
float: right;
width: 300px;
background-color: blue;
}
.layout.float .center{
background-color: yellow;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动解决方案</h1>
1. 这是三栏布局中间部分
2. 这是三栏布局中间部分
</div>
</article>
</section>
<!-- 方法二:绝对定位解决方案 -->
<section class="layout absolute">
<style>
.layout.absolute .left-center-right>div{
position: absolute;
}
.layout.absolute .left{
left: 0;
width: 300px;
background-color: red;
}
.layout.absolute .center{
left: 300px;
right: 300px;
background-color: yellow;
}
.layout.absolute .right{
right: 0;
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>绝对定位解决方案</h1>
1. 这是三栏布局绝对定位中间部分
2. 这是三栏布局绝对定位中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 方法三:flexbox解决方案 -->
<section class="layout flexbox">
<style>
.layout.flexbox{
margin-top: 140px;
}
.layout.flexbox .left-center-right{
display: flex;
}
.layout.flexbox .left{
width: 300px;
background-color: red;
}
.layout.flexbox .center{
flex: 1;
background-color: yellow;
}
.layout.flexbox .right{
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>flexbox解决方案</h1>
1. 这是三栏布局flexbox中间部分
2. 这是三栏布局flexbox中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 表格布局解决方案 -->
<section class="layout table">
<style>
.layout.table .left-center-right{
width: 100%;
display: table;
height: 100px;
}
.layout.table .left-center-right>div{
display: table-cell;
}
.layout.table .left{
width: 300px;
background-color: red;
}
.layout.table .center{
background-color: yellow;
}
.layout.table .right{
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>表格布局解决方案</h1>
1. 这是三栏布局表格布局中间部分
2. 这是三栏布局表格布局中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 网格布局解决方案 -->
<section class="layout grid">
<style>
.layout.grid .left-center-right{
display: grid;
width: 100%;
grid-template-rows: 100px; /* 行高的样式 */
grid-template-columns: 300px auto 300px; /* 列的样式,三个值分别对应三列的宽度 */
}
.layout.grid .left{
background-color: red;
}
.layout.grid .center{
background-color: yellow;
}
.layout.grid .right{
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>网格布局解决方案</h1>
1. 这是三栏布局网格布局中间部分
2. 这是三栏布局网格布局中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>
浮动解决方案缺点:关于清除浮动,因为浮动以后它是脱离文档流的,如果处理不好会带来很多问题,这是它的局限性。
浮动解决方案优点:兼容性比较好,只要清除浮动和其他浮动周边元素的关系处理好的话,它的兼容性比较好。
绝对定位解决方案缺点:因为布局已经脱离文档流了,那意味着所有的子元素也必须脱离文档流,导致这个方案的有效性和可使用性是比较差的
绝对定位解决方案优点:快捷
flexbox布局解决方案缺点:IE8及以下不支持flex
flexbox布局解决方案优点:CSS3中新出现的一种布局方式,为了解决上述两种布局方式的不足出现的,是比较完美的方式,现在的移动端基本都使用flex布局
表格布局解决方案缺点:麻烦,操作比较繁琐,对seo也不够友好,当其中某一个单元格的高度超出的时候,它两侧的单元格也是要跟着调整高度的,有时候我们的场景是不需要它同时增高的
表格布局解决方案优点:兼容性非常好,IE8支持
网格布局解决方案:通过网格布局可以做很多复杂的事情,但是代码量要简化得多
如果去掉高度已知,考虑纵向,哪种方案还适用,哪些又不再适用:
第三种方案flex布局和第四种方案表格布局是通用的,其它方案都不能用
浮动解决方案想要解决这个问题,就得创建BFC
section和article标签就是语义化标签,article表明是一个容器,每个区域的划分是用section