欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析CSS等高布局的6種方式

  發(fā)布時(shí)間:2016-05-04 15:20:00   作者:佚名   我要評(píng)論
下面小編就為大家?guī)?lái)一篇淺析CSS等高布局的6種方式。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

前面的話

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

偽等高

邊框模擬  

因?yàn)樵剡吙蚝驮馗叨仁冀K是相同高度,用元素的邊框顏色來(lái)偽裝左右兩個(gè)兄弟元素的背景色。然后將左右兩個(gè)透明背景的元素使用absolute覆蓋在中間元素的左右邊框上,實(shí)現(xiàn)視覺(jué)上的等高效果

[注意]左右兩側(cè)元素高度不能大于中間元素高度,否則無(wú)法撐開容器高度

CSS Code復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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>  

負(fù)margin  

因?yàn)楸尘笆窃趐adding區(qū)域顯示的,設(shè)置一個(gè)大數(shù)值的padding-bottom,再設(shè)置相同數(shù)值的負(fù)的margin-bottom,使背景色鋪滿元素區(qū)域,又符合元素的盒模型的計(jì)算公式,實(shí)現(xiàn)視覺(jué)上的等高效果

[注意]如果頁(yè)面中使用<a>錨點(diǎn)跳轉(zhuǎn)時(shí),將會(huì)隱藏部分文字信息

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

CSS Code復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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元素默認(rèn)就是等高的

CSS Code復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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  

設(shè)置子元素的top:0;bottom:0;使得所有子元素的高度都和父元素的高度相同,實(shí)現(xiàn)等高效果

CSS Code復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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中的伸縮項(xiàng)目默認(rèn)都拉伸為父元素的高度,也實(shí)現(xiàn)了等高效果

CSS Code復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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  

當(dāng)子元素高度不同時(shí),進(jìn)行js判斷,增加較低子元素的padding-bottom,使得各個(gè)子元素實(shí)現(xiàn)等高效果

CSS Code復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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復(fù)制內(nèi)容到剪貼板
  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種方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

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

相關(guān)文章

最新評(píng)論