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

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

  發(fā)布時間:2016-05-04 15:20:00   作者:佚名   我要評論
下面小編就為大家?guī)硪黄獪\析CSS等高布局的6種方式。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前面的話

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

偽等高

邊框模擬  

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

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

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區(qū)域顯示的,設置一個大數(shù)值的padding-bottom,再設置相同數(shù)值的負的margin-bottom,使背景色鋪滿元素區(qū)域,又符合元素的盒模型的計算公式,實現(xiàn)視覺上的等高效果

[注意]如果頁面中使用<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;使得所有子元素的高度都和父元素的高度相同,實現(xiàn)等高效果

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中的伸縮項目默認都拉伸為父元素的高度,也實現(xiàn)了等高效果

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,使得各個子元素實現(xiàn)等高效果

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設置多列等高布局的方法示例

    這篇文章主要介紹了css設置多列等高布局的方法示例的相關資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-21
  • 前端應該掌握的CSS實現(xiàn)多列等高布局技巧

    我們在寫頁面的時候,有的時候會遇到多欄布局,這篇文章主要介紹了前端應該掌握的CSS實現(xiàn)多列等高布局技巧,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟
    2018-06-05
  • 利用CSS3的flexbox實現(xiàn)水平垂直居中與三列等高布局

    這篇文章給大家介紹了三個小節(jié)的內容,其中包括關于css3中flexbox需要掌握的概念、flexbox實現(xiàn)水平垂直居中對齊和三列等高自適應頁腳區(qū)域黏附底部的布局,有需要的可以參考
    2016-09-12
  • 用CSS實現(xiàn)三列DIV等高布局以傳達更好的視覺效果

    頁面布局中經(jīng)常遇到等高布局的情況,為了得到更好的視覺效果,就要實現(xiàn)DIV等高布局的效果。以最普遍的DIV三列布局來說
    2014-09-03
  • 多列等高的CSS實現(xiàn)代碼

    這種情況下就需要兩列的高度保持一致了,左邊高度增加,右邊也跟著增加,右邊高度增加,左邊同樣也要增加,否則就會出現(xiàn)“斷層”的效果,接下來將介紹多列等高的實現(xiàn)方法,
    2012-12-03
  • CSS 三欄等高布局實現(xiàn)方法

    CSS網(wǎng)頁布局實例:三欄等高布局.
    2009-08-29
  • CSS實例:CSS實現(xiàn)的等高網(wǎng)頁布局

    網(wǎng)頁制作Webjx文章簡介:為了讓網(wǎng)頁更美觀、協(xié)調,有的時候需要用到左右等到布局,傳統(tǒng)的等高布局是用 javascript 實現(xiàn)的,現(xiàn)在來看看 silence 發(fā)明的真正的 CSS 實現(xiàn)的等
    2009-04-02
  • CSS實例:三列等高布局-CSS教程-網(wǎng)頁制作-網(wǎng)頁教學網(wǎng)

      三列等高CSS布局的一個實例,   修改國外的一個demo,   兼容到了IE5.5 和標準的瀏覽器OperaFirefoxSafari。   不過hack太多,不是很喜歡這樣做。 全部代碼如下
    2008-10-17
  • css多種方式實現(xiàn)等高布局的示例代碼

    本文講的等高布局是在不手動設置元素高度的情況下,使用純css實現(xiàn)各個元素高度都相當?shù)男Ч?,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看
    2019-01-09

最新評論