[英]How to align heading <h1> in the center of the page
I want to center <h1>
or <div class="heading">
on the page. 我想在页面上将<h1>
或<div class="heading">
居中。 The only solution I have found is 我发现的唯一解决方案是
body { text-align: center; }
but I can't figure it out why this code doesn't work. 但是我无法弄清楚为什么这段代码不起作用。 Display: inline-block
is used because I want the border to wrap around my . Display: inline-block
是因为我希望边框围绕我的。
body { margin: 0; } .navbar { text-align: right; background: black; color: white; padding: 10px; } ul { list-style-type: none; padding: 5px; } li { display: inline-block; } .heading { border: 2px solid black; display: inline-block; text-align: center; }
<header> <nav class="navbar"> <ul> <li>home</li> <li>about</li> <li>contact</li> </ul> </nav> <div class="heading"> <h1>heading</h1> </div> </header>
Add this: 添加:
.heading {
text-align: center;
}
...and delete display: inline-block
from .heading
. ...并删除display: inline-block
.heading
display: inline-block
。 Instead, add this 相反,添加此
.heading h1 {
display: inline-block;
}
.heading
is the container of your h1
. .heading
是h1
的容器。 Both are by default 100% wide. 两者默认都是100%宽。 This centers the inline-block h1
inside the full-width .heading
这将内联块h1
置于全角.heading
The secret you are looking for is to use a block
-level element, and also set a margin: 0 auto
. 您寻找的秘密是使用block
级元素,并设置一个margin: 0 auto
。 This tells the block to centralise, much like a standard text-align: center
. 这告诉块集中,就像标准的text-align: center
。
.header {
display: block;
margin: 0 auto;
}
By default, block
-level elements occupy 100% of the width of their container, so you might also want to specify a width for the header. 默认情况下, block
级元素占据其容器宽度的100%,因此您可能还需要为标头指定宽度。 Alternatively, you can have the header automatically adjust to the size of the text by adding a container div that is set as in inline-block
, and moving the border to there: 另外,您可以通过添加设置为inline-block
的容器div并将边框移到此处来使标题自动调整为文本大小。
body { margin: 0; } .navbar { text-align: right; background: black; color: white; padding: 10px; } ul { list-style-type: none; padding: 5px; } li { display: inline-block; } .heading { display: block; margin: 0 auto; text-align: center; } .heading-wrapper { display: inline-block; border: 2px solid black; padding: 0 10px; }
<header> <nav class="navbar"> <ul> <li>home</li> <li>about</li> <li>contact</li> </ul> </nav> <div class="heading"> <div class="heading-wrapper"> <h1>heading</h1> </div> </div> </header>
This way, the header will stay centralised, and have the border automatically expand correctly to accommodate the header, no matter how much text there is. 这样,标题将保持居中状态,并且边框会自动正确扩展以容纳标题,而不管有多少文本。
Hope this helps! 希望这可以帮助! :) :)
a div displays block by default, so it's definitely important to declare if you want to display it otherwise. div默认情况下显示块,因此声明是否要显示它绝对很重要。
However, again, as in another post i saw earlier, you have no css for the containing parent, the header
, which would greatly assist you. 但是,再次,正如我在之前看到的另一篇文章中,对于包含父对象的header
,您没有css,这将对您有很大帮助。 You should apply any margin to be inherited to this, and there should be no need to apply a small width to your div. 您应该应用任何要继承的边距,并且不必对div应用较小的宽度。
body { margin: 0; } header{margin: 0 auto;} .navbar { text-align: right; background: black; color: white; padding: 10px; } ul { list-style-type: none; padding: 5px; } li { display: inline-block; } .heading { border: 2px solid black; /*display: block; - even if you leave this out, it will display as block*/ text-align: center; }
<header> <nav class="navbar"> <ul> <li>home</li> <li>about</li> <li>contact</li> </ul> </nav> <div class="heading"> <h1>heading</h1> </div> </header>
You can center it by using display: flex; justify-content;
您可以使用display: flex; justify-content;
将其居中display: flex; justify-content;
display: flex; justify-content;
on the parent element. 在父元素上。 Here is a great resource on centering things https://www.w3.org/Style/Examples/007/center.en.html 这是有关使内容居中的重要资源https://www.w3.org/Style/Examples/007/center.en.html
body { margin: 0; } .navbar { text-align: right; background: black; color: white; padding: 10px; } ul { list-style-type: none; padding: 5px; } li { display: inline-block; } .heading { display: flex; justify-content: center; }
<header> <nav class="navbar"> <ul> <li>home</li> <li>about</li> <li>contact</li> </ul> </nav> <div class="heading"> <h1>heading</h1> </div> </header>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.