css有什么方式可以實現(xiàn)等高布局?首先我們得知道什么是等高布局?小編給大家總結(jié)了以下內(nèi)容,一起往下看吧。
指在同一個父容器中,子元素高度相等的布局。
從等高布局實現(xiàn)方式來說分為兩類:
1、偽等高
子元素高度差依然存在,只是視覺上給人感覺就是等高。
2、真等高
子元素高度相等。
偽等高實現(xiàn)方式:
通過負(fù)margin和Padding實現(xiàn)
真等高實現(xiàn)方式:
1、table
2、absoult
3、flex
4、grid
5、js
偽等高之-負(fù)margin和padding
主要利用負(fù)margin來實現(xiàn),如下:
<div class="layout parent"> <div class="left"><p>left</p></div> <div class="center"> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> </div> <div class="right"><p>right</p></div> <div style="clear: both;">11111111111</div> </div>
.parent{ position: relative; overflow:hidden; color: #efefef; } .center, .left, .right { box-sizing: border-box; float: left; } .center { background-color: #2ECC71; width: 60%; } .left { width: 20%; background-color: #1ABC9C; } .right { width: 20%; background-color: #3498DB; } .left, .right, .center { margin-bottom: -99999px; padding-bottom: 99999px; }
真實等高之 - table布局
<div class="layout parent"> <div class="left"><p>left</p></div> <div class="center"> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> </div> <div class="right"><p>right</p></div> <div style="clear: both;">11111111111</div> </div>
.parent{ position: relative; display: table; color: #efefef; } .center, .left, .right { box-sizing: border-box; display: table-cell } .center { background-color: #2ECC71; width: 60%; } .left { width: 20%; background-color: #1ABC9C; } .right { width: 20%; background-color: #3498DB; }
真實等高之 - absolute
<div class="layout parent"> <div class="left"><p>left</p> </div> <div class="center"> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> </div> <div class="right"><p>right</p></div> </div>
.parent{ position: absolute; color: #efefef; width:100%; height: 200px; } .left, .right, .center { position: absolute; box-sizing: border-box; top:0; bottom:0; } .center { background-color: #2ECC71; left: 200px; right: 300px; } .left { width: 200px; background-color: #1ABC9C; } .right { right:0; width: 300px; background-color: #3498DB; }
真實等高之 - flex
.parent{ display: flex; color: #efefef; width:100%; height: 200px; } .left, .right, .center { box-sizing: border-box; flex: 1; } .center { background-color: #2ECC71; } .left { background-color: #1ABC9C; } .right { background-color: #3498DB; }
<div class="layout parent"> <div class="left"><p>left</p> </div> <div class="center"> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> </div> <div class="right"><p>right</p></div> </div>
真實等高之 - grid
.parent{ display: grid; color: #efefef; width:100%; height: 200px; grid-template-columns: 1fr 1fr 1fr; } .left, .right, .center { box-sizing: border-box; } .center { background-color: #2ECC71; } .left { background-color: #1ABC9C; } .right { background-color: #3498DB; }
<div class="layout parent"> <div class="left"><p>left</p> </div> <div class="center"> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> </div> <div class="right"><p>right</p></div> </div>
真實等高之 - js
獲取所有元素中最高列,然后再去比對再進行修改
<div class="layout parent"> <div class="left"><p>left</p> </div> <div class="center"> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> <p>我是中間部分的內(nèi)容</p> </div> <div class="right"><p>right</p></div> </div>
.parent{ overflow: auto; color: #efefef; } .left, .right, .center { float: left; } .center { width: 60%; background-color: #2ECC71; } .left { width: 20%; background-color: #1ABC9C; } .right { width: 20%; background-color: #3498DB; }
// 獲取最高元素的高度 var nodeList = document.querySelectorAll(".parent > div"); var arr = [].slice.call(nodeList,0); var maxHeight = arr.map(function(item){ return item.offsetHeight }).sort(function(a, b){ return a - b; }).pop(); arr.map(function(item){ if(item.offsetHeight < maxHeight) { item.style.height = maxHeight + "px"; } });
如圖:
關(guān)于css有什么方式可以實現(xiàn)等高布局就分享到這里了,當(dāng)然并不止以上和大家分析的辦法,不過小編可以保證其準(zhǔn)確性是絕對沒問題的。希望以上內(nèi)容可以對大家有一定的參考價值,可以學(xué)以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。
網(wǎng)站欄目:css有什么方式可以實現(xiàn)等高布局-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://aaarwkj.com/article42/pidec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計公司、動態(tài)網(wǎng)站、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)