![](/img/trans.png)
[英]Width of website suddenly overflows when using text-align: center; and display: inline-block;
[英]text align:center is not doing well with width and display: inline-block
您需要将h1
的父级设置为text-align:center
body { text-align: center; } h1 { background: lightgreen; font-family: sans-serif; width: 200px; display: inline-block; }
<h1>Some text</h1>
/*without display: inline-block;*/ .h1{ text-align: center !important; font-family: sans-serif; width: 200px; margin: auto; } /*with display: inline-block;*/ .with_div{ text-align: center; } .with_div h1{ text-align: center !important; font-family: sans-serif; width: 200px; display: inline-block; }
<!-- without display: inline-block; --> <h1 class="h1">hello</h1> <!-- with display: inline-block --> <div class="with_div"> <h1>hello</h1> </div>
您可以使用以下CSS来实现此目标,而不管它是否位于div中。 与其他一些相似,但是它还包含margin:auto; 即使将h1显示为行内或块内,它也将居中。
body{
text-align: center;
}
h1{
text-align: center !important;
font-family: sans-serif;
width: 200px;
margin: auto;
display: inline-block;
}
没有任何包装div。 CSS Flex
h1{
display:flex;
justify-content:center;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.