頁面佈局--三欄佈局,高度已知,兩邊寬度固定,中間自適應實現方式
阿新 • • 發佈:2019-02-20
題目:假設高度已知,三欄佈局,其中左欄和右欄寬度為200px,中間自適應
全域性樣式
html *{
padding: 0;
margin: 0;
}
.layout{
margin-top: 20px;
}
.layout article div{
min-height: 100px;
}
1、浮動佈局
<section class="layout float"> <style media="screen"> .layout.float .left{ float: left; width: 200px; background: red; } .layout.float .right{ float: right; width: 200px; background: blue; } .layout.float .center{ background: yellow; } </style> <article class="left-right-center"> <div class="left"></div> <div class="right"></div> <div class="center"> <h1>浮動解決方案</h1> <p>1、這是三欄佈局中間部分</p> <p>2、這是三欄佈局中間部分</p> </div> </article> </section>
2、絕對定位
<section class="layout absolute"> <style media="screen"> .layout.absolute .left-right-center>div{ position: absolute; } .layout.absolute .left{ left: 0; width: 200px; background: red; } .layout.absolute .center{ left: 200px; right: 200px; background: yellow; } .layout.absolute .right{ right: 0; width: 200px; background: blue; } </style> <article class="left-right-center"> <div class="left"></div> <div class="center"> <h1>絕對定位解決方案</h1> <p>1、這是三欄佈局中間部分</p> <p>2、這是三欄佈局中間部分</p> </div> <div class="right"></div> </article> </section>
3、flexbox佈局
<section class="layout flexbox"> <style media="screen"> .layout.flexbox{ margin-top: 150px; } .layout.flexbox .left-right-center{ display: flex; } .layout.flexbox .left{ width: 200px; background: red; } .layout.flexbox .center{ flex: 1; background: yellow; } .layout.flexbox .right{ width: 200px; background: blue; } </style> <article class="left-right-center"> <div class="left"></div> <div class="center"> <h1>flex解決方案</h1> <p>1、這是三欄佈局中間部分</p> <p>2、這是三欄佈局中間部分</p> </div> <div class="right"></div> </article> </section>
4、表格佈局
<section class="layout table">
<style media="screen">
.layout.table .left-right-center{
display: table;
width: 100%;
height: 100px;
}
.layout.table .left-right-center>div{
display: table-cell;
}
.layout.table .left{
width: 200px;
background: red;
}
.layout.table .center{
background: yellow;
}
.layout.table .right{
width: 200px;
background: blue;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="center">
<h1>表格佈局解決方案</h1>
<p>1、這是三欄佈局中間部分</p>
<p>2、這是三欄佈局中間部分</p>
</div>
<div class="right"></div>
</article>
</section>
5、網格佈局
<section class="layout grid">
<style media="screen">
.layout.grid .left-right-center{
display: grid;
width: 100%;
height: 100px;
grid-template-rows: 100px;
grid-template-columns:200px auto 200px;
}
.layout.grid .left{
background: red;
}
.layout.grid .center{
background: yellow;
}
.layout.grid .right{
background: blue;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="center">
<h1>網格佈局解決方案</h1>
<p>1、這是三欄佈局中間部分</p>
<p>2、這是三欄佈局中間部分</p>
</div>
<div class="right"></div>
</article>
</section>
總結:
1、各方案優缺點
float:需要清除浮動,但是相容性好;
絕對定位:快捷,已理解,但是絕對定位脫離了文件流,可用性差;
flexbox:有相容性,其他的都挺好的;
表格佈局:相容性好;
網格佈局:有相容性
2、拓展
如果高度未知,flexbox和表格佈局仍然可以使用。