[小筆記] margin:0 auto 水平置中技巧
當我們希望區塊水平置中時常會用到 margin :0 auto 來調整,
有時候可能會出現下了語法,卻沒有呈現的問題,
可以注意以下幾點:
-
元素必須是區塊級(block-level),例如:display: block 或 display: table
-
元素不可以是浮動元素(float)
-
元素不可以是絕對定位(absolute)、固定定位(fixed)
這裡有個例外:當設定為 left: 0 right: 0 時 -
元素必須設定 width
當寬度為 auto 時,雖然 margin 會作用,但因為 auto width 優先於 auto margin,因此 auto margin 會被歸零
參考資料StackOverFlow討論串