1 margin的重要性
margin作為CSS盒模型基本組成要素之一,是非常Basis的一個技術手段。
2 margin經常被用來做什么?
- 讓塊元素水平居中
- 讓元素之間留有舒適的空白
- 處理特殊的first或last【不懂】
- 一些布局
3 需要注意的地方
- margin折疊
- margin的百分比值
- margin的auto值
- margin和相對偏移top,right,botton,left的異同
- IE6浮動雙margin bug
- IE6浮動相鄰元素3px bug
keyword auto
auto是margin的可選值之一,一般用法是margin:0 auto;和margin:
auto;
為什么不論margin:auto還是margin:0 auto效果都是一樣的,都是讓元素水平居中了,但縱向并沒有任何變化。
大家都知道,margin是復合屬性,也就是說margin:auto,其實相當于margin:auto auto auto auto,四個值分別對應上右下左。
根據規范,margin-top:auto和margin-botton:auto,其計算值為0.這也就解釋了為什么margin:auto等同于margin:0 auto。
規范原文:
On the A edge and C edge, the used value of ‘auto’ is 0.
翻譯:如果場景是A和C,那么其auto計算值為0.
為什么auto能實現水平居中?
這是因為水平方向的auto,其計算值取決于可用空間(剩余空間)。
原文:On the A edge and C edge, the used value of ‘auto’ is 0.
翻譯:如果場景是B和D,那么其 auto 計算值取決于可用空間。