假設高度已知,其中左、右欄寬度各為300px,中間自適應
- 浮動解決方案
- 注意內容主體放在最后
<!--浮動布局 -->
<section class="layout float">
<style media="screen">
.layout article div {
min-height: 100px;
}
.layout.float .left {
float: left;
width: 300px;
background: red;
}
.layout.float .center {
background: yellow;
}
.layout.float .right {
float: right;
width: 300px;
background: blue;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h2>浮動解決方案</h2>
1.這是三欄布局的解決方案; 2.這是三欄布局的解決方案; 3.這是三欄布局的解決方案; 4.這是三欄布局的解決方案; 5.這是三欄布局的解決方案; 6.這是三欄布局的解決方案;
</div>
</article>
</section>
- 絕對定位解決方法
<!-- 絕對布局 -->
<section class="layout absolute">
<style>
.layout article div {
min-height: 100px;
}
.layout.absolute .left-center-right>div {
position: absolute;
}
.layout.absolute .left {
left: 0;
width: 300px;
background: red;
}
.layout.absolute .center {
left: 300px;
right: 300px;
background: yellow;
}
.layout.absolute .right {
right: 0;
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>絕對定位解決方案</h2>
1.這是三欄布局的解決方案; 2.這是三欄布局的解決方案; 3.這是三欄布局的解決方案; 4.這是三欄布局的解決方案; 5.這是三欄布局的解決方案; 6.這是三欄布局的解決方案;
</div>
<div class="right"></div>
</article>
</section>
- flexBox解決方案
<section class="layout flexbox">
<style>
.layout article div {
min-height: 100px;
}
.layout.flexbox .left-center-right {
display: flex;
}
.layout.flexbox .left {
width: 300px;
background: red;
}
.layout.flexbox .center {
flex: 1;
background: yellow;
}
.layout.flexbox .right {
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>flexbox解決方案</h2>
1.這是三欄布局的解決方案; 2.這是三欄布局的解決方案; 3.這是三欄布局的解決方案; 4.這是三欄布局的解決方案; 5.這是三欄布局的解決方案; 6.這是三欄布局的解決方案;
</div>
<div class="right"></div>
</article>
</section>
4.表格解決方案
<!-- 表格布局 -->
<section class="layout table">
<style>
.layout article div {
min-height: 100px;
}
.layout.table .left-center-right {
width: 100%;
height: 100px;
display: table;
}
.layout.table .left-center-right>div {
display: table-cell;
}
.layout.table .left {
width: 300px;
background: red;
}
.layout.table .center {
background: yellow;
}
.layout.table .right {
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>表格布局解決方案</h2>
1.這是三欄布局的解決方案; 2.這是三欄布局的解決方案; 3.這是三欄布局的解決方案; 4.這是三欄布局的解決方案; 5.這是三欄布局的解決方案; 6.這是三欄布局的解決方案;
</div>
<div class="right"></div>
</article>
</section>
- 網格布局解決方案
<!-- 網格布局 -->
<section class="layout grid">
<style>
.layout.grid .left-center-right {
width: 100%;
display: grid;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.layout.grid .left-center-right>div {}
.layout.grid .left {
width: 300px;
background: red;
}
.layout.grid .center {
background: yellow;
}
.layout.grid .right {
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>網格布局解決方案</h2>
1.這是三欄布局的解決方案; 2.這是三欄布局的解決方案; 3.這是三欄布局的解決方案; 4.這是三欄布局的解決方案; 5.這是三欄布局的解決方案; 6.這是三欄布局的解決方案;
</div>
<div class="right"></div>
</article>
</section>
五種方案的優缺點
浮動 | 絕對定位 | flex | table | grid | |
---|---|---|---|---|---|
優點 | 兼容性好 | 快速布局 | 解決脫離文檔流 | 兼容性好 | 可以完成復雜的布局 |
缺點 | 脫離文檔流 | 脫離文檔流 | 兼容性不好 | 中間高度增加,兩側同時增加 | 代碼量多 |
如果高度未知那些方案還適用
flex布局和table布局