[英]Using 'auto' on block element 'h1'
h1, h2, etc
是塊元素
我正在嘗試將h1
居中,但margin: 0 auto
不起作用。
這使我感到困惑,因為根據CSS屬性中`auto`值的含義是什么。
具有
margin: 0 auto
的塊元素將增加左側和右側邊距,直到其沿視口的y軸居中。
<h1 style="margin: 0 auto">Is this centered?</h1>
我想念什么?
關於margin: auto
引用僅在您指定塊元素的寬度的情況下適用,因為默認情況下,塊元素將占用寬度的100%。 因此,在這種情況下,應使用css text-align
屬性。 ( text-align: center
)。
由於它們是塊元素,因此它們的寬度是100%,而不是文本的寬度。 您應該只使用text-align: center
。
<h1 style="text-align: center">Is this centered?</h1>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.