div+css實現幾種經典布局的詳解


一、左右兩側,左側固定寬度200px,右側自適應占滿

 

[html] 
 
  1.     <div class="divBox">  
  2.         <div class="left"></div>  
  3.         <div class="right"></div>  
  4.     </div>  
[css] 
 
  1.          
  2.         .divBox{  
  3.             height: 500px;  
  4.         }  
  5.         .left{  
  6.             float: left;  
  7.             width: 200px;  
  8.             height: 100%; 
  9.         }  
  10.         .right{  
  11.             margin-left: 200px;  
  12.             height: 100%;  
  13.         }  

這個實現起來比較的簡單,左側的div給浮動,右側的divmargin-left使其從左側div右側開始展現,加背景顏色方便觀察。

 

 

二、左中右三列,左右個200px固定,中間自適應占滿

[html]
 
  1.     <div class="divBox">  
  2.         <div class="left"></div>  
  3.         <div class="right"></div>  
  4.         <div class="center"></div>  
  5.     </div>  
[css] 
 
  1.  .divBox{  
  2.     height: 500px;  
  3. }  
  4. .left{  
  5.     float: left;  
  6.     width: 200px;  
  7.     height: 100%;  
  8. }  
  9. .center{  
  10.     margin: 0 200px;
  11.     height: 500px;  
  12. }  
  13. .right{   
  14.     float: right;  
  15.     width: 200px;  
  16.     height: 100%;  
  17. }  

 

 

三、上中下三行,頭部200px高,底部200px高,中間自適應占滿

[html] 
  1. <div class="divBox">  
  2.     <div class="top"></div>  
  3.     <div class="center"></div>  
  4.     <div class="bottom"></div>  
  5. </div>  
[css] 
  1.         .divBox{  
  2.             width: 100%;  
  3.         }  
  4.         .top{  
  5.             width: 100%;  
  6.             height: 200px;  
  7.             position: absolute;  
  8.             top: 0;  
  9.         }  
  10.         .center{  
  11.             width: 100%;  
  12.             position: absolute;  
  13.             top: 200px;  
  14.             bottom: 200px;  
  15.         }  
  16.         .bottom{  
  17.             width: 100%;  
  18.             height: 200px;  
  19.             position: absolute;  
  20.             bottom: 0;  
  21.         }  

 

 

這里用到了絕對定位,把上面的和下面的分別設置top:0,bottom:0 固定在上下兩端,中間的距離上下200px即可。

 

四、上下兩部分,底下這個固定高度200px,如果上面的內容少,那么這個footer就固定在底部,如果內容多,就把footer擠着往下走

 

[html] 
  1.     <div class="divBox">  
  2.         <div class="content"></div>  
  3.         <div class="footer"></div>  
  4.     </div>  
[css] 
 
  1.         html{  
  2.             height: 100%;  
  3.         }  
  4.         body{  
  5.             min-height: 100%;  
  6.             position: relative;  
  7.         }  
  8.         .content{  
  9.             width: 100%;  
  10.             padding-bottom: 200px;  
  11.         }  
  12.         .footer{  
  13.             width: 100%;  
  14.             height: 200px;  
  15.             position: absolute;  
  16.             bottom: 0;  
  17.         }  


固定footer在底部和把foorter往下擠着走都比較容易實現,但是合到一起,就不好弄了吧,其實也不難,更改content的高度,就可以看到效果了

 

必要的設置就是html要有高度,body的最小高度要有,footer是依照body進行絕對定位的,

了解了這些就不難實現了。

 

這些只是實現經典布局的一些方法,還有其他的方法,這里就不一一列出了。


免責聲明!

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



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