[CSS] 為什么margin:0 auto;可以讓塊級(jí)元素水平居中

https://www.w3.org/TR/CSS2/visudet.html#blockwidth

10.3.3 Block-level, non-replaced elements in normal flow

The following constraints must hold among the used values of the other properties:

'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block

If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the following rules, treated as zero.

If all of the above have a computed value other than 'auto', the values are said to be "over-constrained" and one of the used values will have to be different from its computed value. If the 'direction' property of the containing block has the value 'ltr', the specified value of 'margin-right' is ignored and the value is calculated so as to make the equality true. If the value of 'direction' is 'rtl', this happens to 'margin-left' instead.

If there is exactly one value specified as 'auto', its used value follows from the equality.

If 'width' is set to 'auto', any other 'auto' values become '0' and 'width' follows from the resulting equality.

If both 'margin-left' and 'margin-right' are 'auto', their used values are equal. This horizontally centers the element with respect to the edges of the containing block.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,118評(píng)論 0 23
  • 說(shuō)起來(lái)這幾天都干了什么,很羞澀,沒(méi)錯(cuò),我又追劇了。每次看完一部劇我都會(huì)默默的告訴自己這是自己追的最后一部劇,因?yàn)槲?..
    包逗豆DoubleD閱讀 227評(píng)論 0 0
  • 早起,匆匆洗把老臉,帶上急不可待的小樂(lè)樂(lè),出發(fā)。 樂(lè)樂(lè)一撒開(kāi),歡蹦亂跳,賺來(lái)那么多那么多眼球。估計(jì)小主大腦細(xì)胞得多...
    婁婁姐閱讀 299評(píng)論 0 1
  • 清清盧氏 演唱:張靜(星光大道CCTV簽約歌手) 作詞:怡人 盧木鳥(niǎo)(詞作家) 作曲編曲:姜山 (青年作曲家) 后...
    華狐發(fā)布閱讀 872評(píng)論 0 0

友情鏈接更多精彩內(nèi)容