<style>
*{
padding:0;
margin:0;
}
.big div{
height:100px;
}
.big .left{
width:300px;
float:left;
background:red;
}
.big .right{
width:300px;
float:right;
background:yellow;
}
.big .center{
background:blue;
}
</style>
<div class="big">
<div class="left">
</div>
<div class="right">
</div>
<div class="center">
浮动解决方案
</div>
</div>
复制代码
<style>
.position{
margin-top:10px;
}
.position>div{
position:absolute;
height:100px;
}
.position .left{
left:0;
width:300px;
background:red;
}
.position .right{
right:0;
width:300px;
background:yellow;
}
.position .center{
left:300px;
right:300px;
background:blue;
}
</style>
<div class="position">
<div class="left">
</div>
<div class="right">
</div>
<div class="center">
绝对定位方案2
</div>
</div>
复制代码
<style>
.flex{
margin-top:120px;
display:flex;
}
.flex>div{
height:100px;
}
.flex .left{
width:300px;
background:red;
}
.flex .center{
flex:1;
background:blue;
}
.flex .right{
width:300px;
background:yellow;
}
</style>
<div class="flex">
<div class="left">
</div>
<div class="center">
flex方案
</div>
<div class="right">
</div>
</div>
复制代码
<style>
.table{
margin-top:10px;
width:100%;
display:table;
height:100px;
}
.table>div{
display:table-cell;
}
.table .left{
width:300px;
background:red;
}
.table .center{
background:blue;
}
.table .right{
width:300px;
background:yellow;
}
</style>
<div class="table">
<div class="left">
</div>
<div class="center">
table方案
</div>
<div class="right">
</div>
</div>
复制代码
<style>
.grid{
margin-top:10px;
display:grid;
width:100%;
grid-template-rows:100px;
grid-template-columns: 300px auto 300px;
}
.grid .left{
background:red;
}
.grid .center{
background:blue;
}
.grid .right{
background:yellow;
}
</style>
<body>
<div class="grid">
<div class="left">
</div>
<div class="center">
grid方案
</div>
<div class="right">
</div>
</div>
</body>
复制代码
问题没有结束,咱们继续讨论。五种解决方案哪个更好呢?笔者一直认为技术没有好坏之分,彻底取决于你用在什么地方。css
浮动:兼容性好,若是对兼容性有明确的要求使用浮动应该知足需求,可是必定要处理好与周边元素的关系,由于一不注意浮动就可能形成页面布局混乱等问题,不过解决浮动带来的反作用方法也很多这里咱们不作讨论。bootstrap
绝对定位:简单直接,可是会使父元素脱离正常文档流里面的子元素也会跟着脱离。布局
flex:目前看来比较完美,不过如今稍微完美一点的技术都存在或多或少的兼容性问题,一样这个样式IE9及如下是不支持的!(IE呀!)性能
表格布局:在咱们要细化结构的时候会显得很繁琐,同时表格布局三个单元格的高度会一块儿变更也不利于咱们进行布局。最重要的固然仍是表格渲染性能不好。flex
网格布局:代码优美简洁,不过仍是兼容性问题。可是将来是美好的!flexbox