[英]Vertical-Align Divs in HTML/CSS
I'm trying to create a header
consisting of an h1
and a nav
element. 我正在尝试创建由
h1
和nav
元素组成的header
。 I want the bottom of the nav
to be aligned with the bottom of the h1
. 我希望
nav
的底部与h1
的底部对齐。
Here's what I've tried: 这是我尝试过的:
<header>
<h1>Web site Header</h1>
<nav>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</nav>
</header>
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! 谢谢!
As clean as it can get: 尽可能干净:
<style>
header > * { display: inline-block; }
nav li { display: inline; }
</style>
Direct header
descendants are now inline blocks, ie they don't push surrounding content to flow beneath them, yet they can still utilize the margin
and padding
property as blocks. 现在,直接
header
后代是内联块,即它们不推动周围的内容在其下流动,但它们仍可以将margin
和padding
属性用作块。
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: CSS:
header
{
display: block;
height: 300px;
width: 100%;
}
h1
{
float: left;
margin: 0;
height: 32px;
}
nav
{
display: block;
height: 32px;
position: relative;
}
nav ul
{
bottom: 0;
list-style: none;
margin: 0;
padding: 0;
position: absolute;
right: 0;
}
nav ul li
{
display: inline-block;
}
HTML: HTML:
<header>
<h1>Web site Header</h1>
<nav>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</nav>
</header>
This relies on knowing the height of the header, and setting both the header and the nav to the same heights/margins. 这取决于了解标头的高度,并将标头和导航设置为相同的高度/边距。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.