假設(shè)高度已知,請(qǐng)寫(xiě)出三欄布局,其中左欄、右欄寬度各為300px;中間自適應(yīng)。
- 浮動(dòng)
<section class="layout float">
<style media="screen">
* {
margin: 0;
padding: 0;
}
.left-center-right div {
min-height: 100px;
}
.left {
float: left;
background: red;
width: 300px;
}
.right {
float: right;
background: blue;
width: 300px;
}
.center {
background: yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h2>浮動(dòng)布局</h2>
1、浮動(dòng)布局左右固定寬度,中間自適應(yīng)
2、浮動(dòng)布局左右固定寬度,中間自適應(yīng)
</div>
</article>
</section>
- 絕對(duì)定位
<section class="layout position">
<style media="screen">
html * {
margin: 0;
padding: 0;
}
.left-center-right div {
position: absolute;
min-height: 100px;
}
.left {
left: 0;
width: 300px;
background: red;
}
.right {
right: 0;
width: 300px;
background: blue;
}
.center {
left: 300px;
right: 300px;
background: yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>絕對(duì)定位</h2>
1、定位布局左右固定寬度,中間自適應(yīng)
2、定位布局左右固定寬度,中間自適應(yīng)
</div>
<div class="right"></div>
</article>
</section>
- flex布局
<section class="layout flex">
<style>
html * {
margin: 0;
padding: 0;
}
.left-center-right div{
min-height: 100px;
}
.left-center-right {
display: flex;
}
.left {
width: 300px;
background: red;
order: -1;
/* order屬性定義項(xiàng)目的排列順序 數(shù)值越小 排列越靠前 默認(rèn)為0 */
}
.center {
flex: 1;
background: yellow;
}
.right {
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="center">
<h2>flex布局</h2>
1、flex布局左右固定寬度,中間自適應(yīng)
2、flex布局左右固定寬度,中間自適應(yīng)
</div>
<div class="left"></div>
<div class="right"></div>
</article>
</section>
- 表格布局
<section class="layout table">
<style>
html * {
margin: 0;
padding: 0;
}
.left-center-right {
display: table;
width: 100%;
height: 100px;
}
.left-center-right div {
display: table-cell;
min-height: 100px;
}
.left {
background: red;
width: 300px;
}
.center {
background: yellow;
}
.right {
background: blue;
width: 300px;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>tabale布局</h2>
1、table布局左右固定寬度,中間自適應(yīng)
2、table布局左右固定寬度,中間自適應(yīng)
</div>
<div class="right"></div>
</article>
</section>
- 網(wǎng)格布局
<section class="layout grid">
<style media="screen">
html * {
margin: 0;
padding: 0;
}
.left-center-right {
display: grid;
width: 100%;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.left-center-right div {
min-height: 100px;
}
.left {
background: red;
}
.center {
background: yellow;
}
.right {
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
1、網(wǎng)格布局左右固定寬度,中間自適應(yīng)
2、網(wǎng)格布局左右固定寬度,中間自適應(yīng)
</div>
<div class="right"></div>
</article>
</section>
總結(jié):
1、這五種解決方案各自的優(yōu)缺點(diǎn)
- 浮動(dòng):缺點(diǎn)是脫離文檔流的,需要做清浮動(dòng)處理;優(yōu)點(diǎn)是兼容性很好。
- 絕對(duì)定位:缺點(diǎn)是布局脫離文檔流,子元素也必須脫離文檔流,可使用性比較差。優(yōu)點(diǎn)是快捷,不容易出問(wèn)題。
- flex布局:為解決以上兩種布局方式的不足而出現(xiàn)的,比較完美,目前移動(dòng)端基本都屬于flex布局。
- 表格布局:缺點(diǎn)是操作繁瑣,對(duì)seo也不友好,當(dāng)其中一個(gè)單元格的高度變大時(shí),其他單元格的高度也會(huì)隨之變大。優(yōu)點(diǎn)是兼容性非常好。
- 網(wǎng)格布局:缺點(diǎn)是新出的技術(shù),低版本瀏覽器兼容性不是很好,優(yōu)點(diǎn)是可以做許多復(fù)雜的事情,但是代碼量要簡(jiǎn)化的多。
2、當(dāng)高度不定時(shí),兩側(cè)的高度也跟這中間的高度撐開(kāi),以上五種有哪幾種可以繼續(xù)用,哪幾種不能用了?
通過(guò)看效果:浮動(dòng)和絕對(duì)定位是不能用的,flex布局、表格布局和網(wǎng)格布局可以繼續(xù)使用。