浅析CSS等高布局的6种方式

2020-05-06 09:20:25易采站长站整理

真等高

table  

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

CSS Code复制内容到剪贴板

<style>   
body,p{margin: 0;}   
.parent{   
    display: table;   
    width: 100%;   
    table-layout: fixed;   
}   
.left,.centerWrap,.rightright{   
    display: table-cell;   
}   
.center{   
    margin: 0 20px;   
}   
</style>  

XML/HTML Code复制内容到剪贴板

<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;使得所有子元素的高度都和父元素的高度相同,实现等高效果

CSS Code复制内容到剪贴板

<style>   
body,p{margin: 0;}   
.parent{   
    position: relative;   
    height: 40px;   
}   
.left,.center,.rightright{   
    position: absolute;   
    top: 0;   
    bottombottom: 0;   
}   
.left{   
    left: 0;