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

2019-10-24,,

前面的话

等高布局是指子元素在父元素中高度相等的布局方式。等高布局的实现包括伪等高和真等高,伪等高只是看上去等高而已,真等高是实实在在的等高。本文将介绍边框模拟、负margin这两种伪等高以及table实现、absolute实现、flex实现和js判断这四种真等高布局

伪等高

边框模拟  

因为元素边框和元素高度始终是相同高度,用元素的边框颜色来伪装左右两个兄弟元素的背景色。然后将左右两个透明背景的元素使用absolute覆盖在中间元素的左右边框上,实现视觉上的等高效果

[注意]左右两侧元素高度不能大于中间元素高度,否则无法撑开容器高度

CSS Code复制内容到剪贴板
  1. <style>   
  2. body,p{margin: 0;}   
  3. .parent{   
  4.     positionrelative;   
  5. }   
  6. .center{   
  7.     box-sizing:border-box;   
  8.     padding: 0 20px;   
  9.     background-clipcontent-box;   
  10.     border-left210px solid lightblue;   
  11.     border-right310px solid lightgreen;   
  12. }   
  13. .left{   
  14.     positionabsolute;   
  15.     top: 0;   
  16.     left: 0;   
  17.     width200px;   
  18. }   
  19. .rightright{   
  20.     positionabsolute;   
  21.     top: 0;   
  22.     rightright: 0;   
  23.     width300px;   
  24. }   
  25. </style>  
XML/HTML Code复制内容到剪贴板
  1. <div class="parent" style="background-color: lightgrey;">  
  2.     <div class="left">  
  3.         <p>left</p>  
  4.     </div>     
  5.     <div class="center" style="background-color: pink;">  
  6.         <p>center</p>  
  7.         <p>center</p>  
  8.     </div>             
  9.     <div class="right">  
  10.         <p>right</p>  
  11.     </div>           
  12. </div>  

负margin  

因为背景是在padding区域显示的,设置一个大数值的padding-bottom,再设置相同数值的负的margin-bottom,使背景色铺满元素区域,又符合元素的盒模型的计算公式,实现视觉上的等高效果

[注意]如果页面中使用<a>锚点跳转时,将会隐藏部分文字信息

[注意]如果页面中的背景图片定位到底部,将会看不到背景图片

CSS Code复制内容到剪贴板
  1. <style>   
  2. body,p{margin: 0;}   
  3. .parent{   
  4.     overflowhidden;   
  5. }   
  6. .left,.centerWrap,.rightright{   
  7.     floatleft;   
  8.     width: 50%;   
  9.     padding-bottom9999px;   
  10.     margin-bottom: -9999px;   
  11. }   
  12. .center{   
  13.     margin: 0 20px;   
  14. }   
  15. .left,.rightright{   
  16.     width: 25%;   
  17. }   
  18. </style>  
XML/HTML Code复制内容到剪贴板
  1. <div class="parent" style="background-color: lightgrey;">  
  2.     <div class="left" style="background-color: lightblue;">  
  3.         <p>left</p>  
  4.     </div>     
  5.     <div class="centerWrap">  
  6.         <div class="center" style="background-color: pink;">  
  7.             <p>center</p>  
  8.             <p>center</p>  
  9.         </div>            
  10.     </div>  
  11.             
  12.     <div class="right" style="background-color: lightgreen;">  
  13.         <p>right</p>  
  14.     </div>           
  15. </div>  

真等高

table  

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

CSS Code复制内容到剪贴板
  1. <style>   
  2. body,p{margin: 0;}   
  3. .parent{   
  4.     display: table;   
  5.     width: 100%;   
  6.     table-layoutfixed;   
  7. }   
  8. .left,.centerWrap,.rightright{   
  9.     displaytable-cell;   
  10. }   
  11. .center{   
  12.     margin: 0 20px;   
  13. }   
  14. </style>  
XML/HTML Code复制内容到剪贴板
  1. <div class="parent" style="background-color: lightgrey;">  
  2.     <div class="left" style="background-color: lightblue;">  
  3.         <p>left</p>  
  4.     </div>     
  5.     <div class="centerWrap">  
  6.         <div class="center" style="background-color: pink;">  
  7.             <p>center</p>  
  8.             <p>center</p>  
  9.         </div>            
  10.     </div>    
  11.     <div class="right" style="background-color: lightgreen;">  
  12.         <p>right</p>  
  13.     </div>           
  14. </div>  

absolute  

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

CSS Code复制内容到剪贴板
  1. <style>   
  2. body,p{margin: 0;}   
  3. .parent{   
  4.     positionrelative;   
  5.     height40px;   
  6. }   
  7. .left,.center,.rightright{   
  8.     positionabsolute;   
  9.     top: 0;   
  10.     bottombottom: 0;   
  11. }   
  12. .left{   
  13.     left: 0;   
  14.     width100px;   
  15. }   
  16. .center{   
  17.     left120px;   
  18.     rightright120px;   
  19. }   
  20. .rightright{   
  21.     width100px;   
  22.     rightright: 0;   
  23. }   
  24. </style>  
XML/HTML Code复制内容到剪贴板
  1. <div class="parent" style="background-color: lightgrey;">  
  2.     <div class="left" style="background-color: lightblue;">  
  3.         <p>left</p>  
  4.     </div>     
  5.     <div class="center" style="background-color: pink;">  
  6.         <p>center</p>  
  7.         <p>center</p>  
  8.     </div>             
  9.     <div class="right" style="background-color: lightgreen;">  
  10.         <p>right</p>  
  11.     </div>           
  12. </div>  

flex  

flex中的伸缩项目默认都拉伸为父元素的高度,也实现了等高效果

CSS Code复制内容到剪贴板
  1. <style>   
  2. body,p{margin: 0;}   
  3. .parent{   
  4.     display: flex;   
  5. }   
  6. .left,.center,.rightright{   
  7.     flex: 1;   
  8. }   
  9. .center{   
  10.     margin: 0 20px;   
  11. }   
  12. </style>  
XML/HTML Code复制内容到剪贴板
  1. <div class="parent" style="background-color: lightgrey;">  
  2.     <div class="left" style="background-color: lightblue;">  
  3.         <p>left</p>  
  4.     </div>     
  5.     <div class="center" style="background-color: pink;">  
  6.         <p>center</p>  
  7.         <p>center</p>  
  8.     </div>             
  9.     <div class="right" style="background-color: lightgreen;">  
  10.         <p>right</p>  
  11.     </div>           
  12. </div>  

js  

当子元素高度不同时,进行js判断,增加较低子元素的padding-bottom,使得各个子元素实现等高效果

CSS Code复制内容到剪贴板
  1. <style>   
  2. body,p{margin: 0;}   
  3. .parent{overflowhidden;}   
  4. .left,.center,.rightright{   
  5.     floatleft;   
  6.     width: 25%;   
  7. }       
  8. .center{   
  9.     width: 50%;   
  10.     padding: 0 20px;   
  11.     background-clipcontent-box;   
  12.     box-sizing: border-box;   
  13. }   
  14. </style>  
XML/HTML Code复制内容到剪贴板
  1. <div class="parent" id="parent" style="background-color: lightgrey;">  
  2.     <div class="left" style="background-color: lightblue;">  
  3.         <p>left</p>  
  4.     </div>     
  5.     <div class="center" style="background-color: pink;">  
  6.         <p>center</p>  
  7.         <p>center</p>  
  8.     </div>             
  9.     <div class="right" style="background-color: lightgreen;">  
  10.         <p>right</p>  
  11.     </div>           
  12. </div>  
JavaScript Code复制内容到剪贴板
  1. <script>   
  2. function getCSS(obj,style){   
  3.     if(window.getComputedStyle){   
  4.         return getComputedStyle(obj)[style];   
  5.     }   
  6.     return obj.currentStyle[style];   
  7. }   
  8. var oParent = document.getElementById('parent');   
  9. var oLeft = oParent.getElementsByTagName('div')[0];   
  10. var oCenter = oParent.getElementsByTagName('div')[1];   
  11. var oRight = oParent.getElementsByTagName('div')[2];   
  12. function eqHeight(obj1,obj2){   
  13.     var oDis = obj1.clientHeight - obj2.clientHeight;   
  14.     if(oDis > 0){   
  15.         obj2.style.paddingBottom = parseFloat(getCSS(obj2,'padding-bottom')) + oDis + 'px';   
  16.     }else{   
  17.         obj1.style.paddingBottom = parseFloat(getCSS(obj1,'padding-bottom')) +  Math.abs(oDis) + 'px';   
  18.     }   
  19. }   
  20. eqHeight(oLeft,oCenter);   
  21. eqHeight(oLeft,oRight);   
  22. </script>  

以上这篇浅析CSS等高布局的6种方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持北冥有鱼。

原文地址:http://www.cnblogs.com/xiaohuochai/archive/2016/05/04/5457127.html

《浅析CSS等高布局的6种方式.doc》

下载本文的Word格式文档,以方便收藏与打印。