HTML/CSS 中的垂直对齐 Div
我正在尝试创建一个由 h1
和 nav
元素组成的 header
。我希望 nav
的底部与 h1
的底部对齐。
这是我尝试过的:
HTML
<header>
<h1>Web site Header</h1>
<nav>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</nav>
</header>
CSS
header {vertical-align: bottom; width: 100%; height: 300px;}
h1 {vertical-align: bottom; float: left;}
nav {vertical-align: bottom; float: right;}
nav ul li {display: inline;}
我可以通过为元素设置精确的边距来做到这一点,但我认为这会更干净(如果可能的话)。关于如何修复它/是否可以完成有什么建议吗?
谢谢!
I'm trying to create a header
consisting of an h1
and a nav
element. I want the bottom of the nav
to be aligned with the bottom of the h1
.
Here's what I've tried:
HTML
<header>
<h1>Web site Header</h1>
<nav>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</nav>
</header>
CSS
header {vertical-align: bottom; width: 100%; height: 300px;}
h1 {vertical-align: bottom; float: left;}
nav {vertical-align: bottom; float: right;}
nav ul li {display: inline;}
I could do this by setting exact margins for my elements, but I thought this would be cleaner (if it's possible). Any advice on how to fix it/if it can be done?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽可能干净:
直接
header
后代现在是内联块,即它们不会将周围的内容推到其下方,但它们仍然可以利用margin
和padding
属性作为块。As clean as it can get:
Direct
header
descendants are now inline blocks, i.e. they don't push surrounding content to flow beneath them, yet they can still utilize themargin
andpadding
property as blocks.可以通过多种不同的方式来实现。边距是为定位而设计的,但如果您不想使用边距或填充,则可以使用绝对定位:
CSS:
HTML:
这依赖于了解标题的高度,并将标题和导航设置为相同的高度/边距。
It's possible to do in many different ways. Margins are designed for positioning, but if you'd rather not use margins or padding, you can use absolute positioning:
CSS:
HTML:
This relies on knowing the height of the header, and setting both the header and the nav to the same heights/margins.