You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML/CSS:带中间图片的居中horizontal navbar链接对齐问题

Fixing Vertical Alignment in Your Horizontal Navbar

Hey there! Let’s get those navigation links properly centered vertically alongside your logo. The core issue with your current code is that the float-based layout and conflicting padding/vertical-align settings are preventing proper alignment. Here’s a clean, modern solution using Flexbox (the easiest way to handle alignment like this):

Modified Code

CSS

* { box-sizing: border-box; }

/* Replace your existing nav-related styles with this */
.nav {
  background-color: #364d63;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
  width: 100%;
  padding: 0;
  display: flex;
  align-items: center; /* This handles vertical centering for all items */
}

.nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  width: 100%;
}

.nav li {
  flex: 1; /* Makes each link section take equal width */
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Give the logo section double the width to match your original col-4 setup */
.nav li:nth-child(3) {
  flex: 2;
}

.nav a {
  color: white;
  font-size: 18px;
  line-height: 25px;
  text-decoration: none;
  padding: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

#logo-navbar-middle {
  width: 200px;
  display: block;
}

HTML

<header>
  <ul class="nav">
    <li><a href="gruppen.html">Gruppen</a></li>
    <li><a href="shows.html">Shows</a></li>
    <li><a href="index.html"><img id="logo-navbar-middle" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/32877/logo-thing.png" alt="Logo"></a></li>
    <li><a href="geschichte.html">Geschichte</a></li>
    <li><a href="bilder.html">Bilder</a></li>
  </ul>
</header>

Why This Works

  • Flexbox Layout: By using display: flex on the .nav and .nav ul, we eliminate the need for floats (which break vertical alignment). The align-items: center property instantly centers all items vertically.
  • Proportional Widths: The flex: 1 and flex: 2 values replicate your original col-2/col-4 width ratio without the messy float constraints.
  • Cleaner Padding: We moved padding directly to the <a> tags to maintain spacing without disrupting alignment.
  • Removed Redundant Classes: The center and col-* classes are no longer needed since Flexbox handles centering and width distribution.

If you prefer to stick with your original inline-block approach instead of Flexbox, here’s a quick fix for that too:

.nav {
  overflow: hidden;
  background-color: #364d63;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
  width: 100%;
  padding: 0;
  line-height: 80px; /* Match this to your logo's height */
}

.nav li {
  display: inline-block;
  vertical-align: middle;
  width: 16.66%;
}

.nav li:nth-child(3) {
  width: 33.33%;
}

.nav a {
  color: white;
  font-size: 18px;
  text-decoration: none;
  display: inline-block;
  padding: 0 15px;
}

[class*="col-"] {
  float: none;
  padding: 0;
}

Either solution will get your links perfectly aligned with the logo. Let me know if you need further tweaks!

内容的提问来源于stack exchange,提问作者MineLenny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:12:08