html - 在 nav 元素内垂直对齐 div

标签 html css

是否可以在 div 的(垂直)中间显示菜单项?

我看了 10 多个关于居中的视频,看了大约 20 个在线“教程”。每个人都使用 jQuery/Bootstrap 和内置类。我已经手动重新创建了每个效果,但我无法让菜单项 div 降到中间。

https://jsfiddle.net/2zwxL14q/

HTML:

<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <nav>
    <h1 style="height: 75px; width: 200px; background: #f00;">
      <a href="/">
        LOGO
      </a>
    </h1>
    <div id="navelements">
      <ul>
        <li><a href="#">Home</a>
        <li><a href="#">About Us</a>
        <li><a href="#">Contact</a>
      </ul>
    </div>
  </nav>

</body>
</html>

CSS:

nav {
  background-color: #13293d;
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  padding: 10px;
  text-align: right;
  width: 100%;
  z-index: 100;
}

nav h1 {
  float: left;
}

#navelements ul {
  list-style: none;
}

#navelements ul li {
  display: inline-block;
}

#navelements ul li a {
  color: #fff;
  font-size: 1em;
  font-weight: 800;
  padding: 5px 10px;
  text-decoration: none;
  text-transform: uppercase;
}

最佳答案

nav这个样式

display: flex;
justify-content: space-between;
align-items: center;

那么你的 nav 将如你所料完全位于中间

nav {
  background-color: #13293d;
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  padding: 10px;
  text-align: right;
  width: 100%;
  z-index: 100;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

nav h1 {
  float: left;
}

#navelements ul {
  list-style: none;
}

#navelements ul li {
  display: inline-block;
}

#navelements ul li a {
  color: #fff;
  font-size: 1em;
  font-weight: 800;
  padding: 5px 10px;
  text-decoration: none;
  text-transform: uppercase;
}
 <nav>
    <h1 style="height: 75px; width: 200px; background: #f00;">
      <a href="/">
        LOGO
      </a>
    </h1>
    <div id="navelements">
      <ul>
        <li><a href="#">Home</a>
        <li><a href="#">About Us</a>
        <li><a href="#">Contact</a>
      </ul>
    </div>
  </nav>

关于html - 在 nav 元素内垂直对齐 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432265/

相关文章:

javascript - 在 ReactangerlySetInnerHTML() 中使用 JSX 表达式渲染 HTML 字符串

css - 设置菜单样式

javascript - classlist.toggle 不适用于 fa-check

javascript - jQuery blur() 不听 $(this)

html - CSS如何把标题放在边框前面?

css - 为具有 border-radius 的元素添加边框

html - 导航菜单中的居中对齐文本

javascript - 如何设置整体Grid缩略图高度相等?

python - 从特定网站使用 xpath 提取 HTML 标记内容

html - 向左浮动 - 向右浮动仅在 IE9 中中断?