CSS 如何将 h1 居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21051578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I center an h1 in the body
提问by user3182981
I tried centering my h1 in the body tag like this:
我尝试将我的 h1 集中在 body 标签中,如下所示:
h1 {margin:0 auto}
But it doesn't center. How do I fix it?
但它不居中。我如何解决它?
回答by j08691
In this case:
在这种情况下:
h1 {
text-align:center;
}
The margin:auto rule is used when you set a width on the element, which you haven't done.
当您在元素上设置宽度时使用 margin:auto 规则,但您还没有这样做。
回答by Ceres
You can center it by setting the width.
您可以通过设置宽度将其居中。
<body>
<h1>My Title</h1>
</body>
CSS
CSS
h1 {
width:500px;
margin: 0 auto;
background: gray;
text-align: center;
}
回答by user3183134
-div style="margin:0 auto; text-align:left; width:80px;"-
sample
-div style="margin:0 auto; text-align:left; width:80px;"- 示例
change the width to change the position
改变宽度来改变位置
回答by timmyblog
Alternatively, you could try this:
或者,你可以试试这个:
h1 {
text-align: center;
margin: 0px auto;
display; block;
}