CSS等高布局的7種方式


前面的話

  等高布局是指子元素在父元素中高度相等的布局方式。等高布局的實現包括偽等高和真等高,偽等高只是看上去等高而已,真等高是實實在在的等高。本文將介紹邊框模擬、負margin這兩種偽等高以及table實現、absolute實現、flex實現、grid實現和js判斷這五種真等高布局

 

偽等高

邊框模擬

  因為元素邊框和元素高度始終是相同高度,用元素的邊框顏色來偽裝左右兩個兄弟元素的背景色。然后將左右兩個透明背景的元素使用absolute覆蓋在中間元素的左右邊框上,實現視覺上的等高效果

  [注意]左右兩側元素的內容高度不能大於中間元素內容高度,否則無法撐開容器高度

<style>
body,p{margin: 0;}
.parent{
    position: relative;
}
.center{
    box-sizing:border-box;
    padding: 0 20px;
    background-clip: content-box;
    border-left: 210px solid lightblue;
    border-right: 310px solid lightgreen;
}
.left{
    position: absolute;
    top: 0;
    left: 0;
    width: 200px;
}
.right{
    position: absolute;
    top: 0;
    right: 0;
    width: 300px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right">
        <p>right</p>
    </div>        
</div>

 

負margin

  因為背景是在padding區域顯示的,設置一個大數值的padding-bottom,再設置相同數值的負的margin-bottom,使背景色鋪滿元素區域,又符合元素的盒模型的計算公式,實現視覺上的等高效果

  [注意]如果頁面中使用<a>錨點跳轉時,將會隱藏部分文字信息

  [注意]如果頁面中的背景圖片定位到底部,將會看不到背景圖片

<style>
body,p{margin: 0;}
.parent{
    overflow: hidden;
}
.left,.centerWrap,.right{
    float: left;
    width: 50%;
    padding-bottom: 9999px;
    margin-bottom: -9999px;
}
.center{
    margin: 0 20px;
}
.left,.right{
    width: 25%;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>         
    </div>
         
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

 

真等高

table

  table元素中的table-cell元素默認就是等高的

<style>
body,p{margin: 0;}
.parent{
    display: table;
    width: 100%;
    table-layout: fixed;
}
.left,.centerWrap,.right{
    display: table-cell;
}
.center{
    margin: 0 20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>         
    </div> 
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

 

absolute

  設置子元素的top:0;bottom:0;使得所有子元素的高度都和父元素的高度相同,實現等高效果

<style>
body,p{margin: 0;}
.parent{
    position: relative;
    height: 40px;
}
.left,.center,.right{
    position: absolute;
    top: 0;
    bottom: 0;
}
.left{
    left: 0;
    width: 100px;
}
.center{
    left: 120px;
    right: 120px;
}
.right{
    width: 100px;
    right: 0;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

 

flex

  flex中的伸縮項目默認都拉伸為父元素的高度,也實現了等高效果

<style>
body,p{margin: 0;}
.parent{
    display: flex;
}
.left,.center,.right{
    flex: 1;
}
.center{
    margin: 0 20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

 

grid

<style>
body,p{margin: 0;}
.parent{
    display: grid;
    grid-auto-flow: column;
    grid-gap:20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

 

js

  當子元素高度不同時,進行js判斷,增加較低子元素的padding-bottom,使得各個子元素實現等高效果

<style>
body,p{margin: 0;}
.parent{overflow: hidden;}
.left,.center,.right{
    float: left;
    width: 25%;
}    
.center{
    width: 50%;
    padding: 0 20px;
    background-clip: content-box;
    box-sizing: border-box;
}
</style>
<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>
<script>
function getCSS(obj,style){
    if(window.getComputedStyle){
        return getComputedStyle(obj)[style];
    }
    return obj.currentStyle[style];
}
var oParent = document.getElementById('parent');
var oLeft = oParent.getElementsByTagName('div')[0];
var oCenter = oParent.getElementsByTagName('div')[1];
var oRight = oParent.getElementsByTagName('div')[2];
function eqHeight(obj1,obj2){
    var oDis = obj1.clientHeight - obj2.clientHeight;
    if(oDis > 0){
        obj2.style.paddingBottom = parseFloat(getCSS(obj2,'padding-bottom')) + oDis + 'px';
    }else{
        obj1.style.paddingBottom = parseFloat(getCSS(obj1,'padding-bottom')) +  Math.abs(oDis) + 'px';
    }
}
eqHeight(oLeft,oCenter);
eqHeight(oLeft,oRight);
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM