DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS特效代碼
盒子模型的百分比,究竟是依據寬還是高?
編輯:CSS特效代碼     

這一直是我心裡一個模糊的問題:margin、padding、width、top 這些屬性寫成百分比時,參照的到底是父容器的寬度還是高度?動手把每個屬性都測了一遍,結論如下。

margin

父容器為 width:800px、height:600px,子元素設 margin:10%。

結論:margin-top 與 margin-bottom 都是依父容器寬度計算,800 × 10% = 80px,與高度無關。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型的定位問題</title> <style type="text/css"> *{ margin: 0; padding: 0; } .container{ width:800px; height:600px; margin:10px auto 0; border:1px solid #ccc; position: relative; } .div{ width:100px; height:50px; background-color:#ccc; border:1px dashed #000; list-style:none; } .div1{ margin:10%; } </style> </head> <body> <div class="container"> <div class="div1 div">margin</div> </div> </body> </html>

padding

同樣設 10%,padding-top 與 padding-bottom 解析出來都是 80px。

結論:padding 的百分比同樣依父容器寬度計算,上下左右皆然。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型的定位問題</title> <style type="text/css"> *{ margin: 0; padding: 0; } .container{ width:800px; height:600px; margin:10px auto 0; border:1px solid #ccc; position: relative; } .div{ width:100px; height:50px; background-color:#ccc; border:1px dashed #000; list-style:none; } .div2{ padding:10%; } </style> </head> <body> <div class="container"> <div class="div2 div">padding</div> </div> </body> </html>

position:absolute

設 top:10%; right:10%,解析後 top 為 60px、right 為 80px。

結論:absolute 定位下,top / bottom 依父容器高度計算,left / right 依父容器寬度計算。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型的定位問題</title> <style type="text/css"> *{ margin: 0; padding: 0; } .container{ width:800px; height:600px; margin:10px auto 0; border:1px solid #ccc; position: relative; } .div{ width:100px; height:50px; background-color:#ccc; border:1px dashed #000; list-style:none; } .div3{ position: absolute; top:10%; right:10%; } </style> </head> <body> <div class="container"> <div class="div3 div">absolute</div> </div> </body> </html>

position:relative

設 top:10%; left:10%,解析後 top 為 60px、left 為 80px。

結論:relative 定位規則與 absolute 相同,top 看高度,left 看寬度。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型的定位問題</title> <style type="text/css"> *{ margin: 0; padding: 0; } .container{ width:800px; height:600px; margin:10px auto 0; border:1px solid #ccc; position: relative; } .div{ width:100px; height:50px; background-color:#ccc; border:1px dashed #000; list-style:none; } .div4{ position: relative; top:10%; left:10%; } </style> </head> <body> <div class="container"> <div class="div4 div">relative</div> </div> </body> </html>

width 與 height

width 的百分比依父容器寬度,height 的百分比依父容器高度。(補充:height 的百分比要生效,父容器必須有明確高度。)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型的定位問題</title> <style type="text/css"> *{ margin: 0; padding: 0; } .container{ width:800px; height:600px; margin:10px auto 0; border:1px solid #ccc; position: relative; } .div{ width:100px; height:50px; background-color:#ccc; border:1px dashed #000; list-style:none; } .div5{ width:10%; height:10%; float:right; } </style> </head> <body> <div class="container"> <div class="div5 div">width和height</div> </div> </body> </html>

總結

看高度:height、top、bottom。

看寬度:width、left、right,以及 padding 與 margin 的全部四個方向。

這條規則解釋了很多日常困惑,例如為什麼在橫向長條容器上寫 padding-top: 10% 會得到意料之外的巨大留白——因為它是拿寬度算的。

線上範例:https://sandbox.runjs.cn/show/cckc8yek

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved