HTML/CSS 中的垂直对齐 Div

发布于 2024-10-06 02:14:36 字数 745 浏览 0 评论 0原文

我正在尝试创建一个由 h1nav 元素组成的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秉烛思 2024-10-13 02:14:36

尽可能干净:

<style>
    header > * { display: inline-block; }
    nav li { display: inline; }
</style>

直接 header 后代现在是内联块,即它们不会将周围的内容推到其下方,但它们仍然可以利用 marginpadding 属性作为块。

As clean as it can get:

<style>
    header > * { display: inline-block; }
    nav li { display: inline; }
</style>

Direct header descendants are now inline blocks, i.e. they don't push surrounding content to flow beneath them, yet they can still utilize the margin and padding property as blocks.

━╋う一瞬間旳綻放 2024-10-13 02:14:36

可以通过多种不同的方式来实现。边距是为定位而设计的,但如果您不想使用边距或填充,则可以使用绝对定位:

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:

<header>
  <h1>Web site Header</h1>
  <nav>
    <ul>
    <li>one</li>
    <li>two</li>
    <li>three</li>
    </ul>
  </nav>
</header>

这依赖于了解标题的高度,并将标题和导航设置为相同的高度/边距。

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:

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:

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文