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

响应式Flex导航中Logo无法左移的技术求助

解决导航菜单小屏Logo居中问题

问题根源

在宽度小于1100px的媒体查询中,你给header设置了flex-direction: column和text-align: center,这会强制子元素垂直排列并全局居中,直接导致Logo无法左对齐。同时汉堡按钮的绝对定位逻辑没问题,但header的flex布局设置是核心矛盾点。

修复方案

修改媒体查询中的header样式,调整flex布局方向与对齐规则,确保Logo左对齐、汉堡按钮右侧垂直居中,下拉菜单紧贴header下方:

/* ---- BREAKPOINTS ---- */
@media (max-width: 1100px) {
  header {
    display: flex;
    /* 取消垂直排列,保持横向布局 */
    flex-direction: row;
    /* 移除全局居中对齐,避免影响Logo */
    /* text-align: center; */
    position: relative;
    /* 让子元素靠左排列 */
    justify-content: flex-start;
    align-items: center;
  }
  .toggle-button {
    display: block;
    /* 调整按钮垂直居中,避免固定top值错位 */
    top: 50%;
    transform: translateY(-50%);
  }
  nav {
    display: none;
    width: 100%;
    border-top: 1px solid white;
    padding: 20px 0;
    /* 下拉菜单紧贴header下方,避免挤压布局 */
    position: absolute;
    top: 100%;
    left: 0;
    background-color: #212223;
  }
  nav ul li {
    padding: 15px 0;
    width: 100%;
  }
  nav ul {
    text-align: center;
  }
  nav.show {
    display: block;
  }
}

额外优化

  1. 移除全局header的text-align: center,避免影响大屏布局的默认对齐
  2. 汉堡按钮通过transform: translateY(-50%)实现精准垂直居中
  3. 下拉菜单添加背景色,防止与下方页面内容重叠

完整修改后代码

CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: Arial, Helvetica, sans-serif;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 0 60px;
  background-color: #212223;
  position: relative;
}

.logo img {
  width: 190px;
}

nav ul li {
  display: inline-block;
  padding: 5px 25px;
}

nav ul li a {
  text-decoration: none;
  display: inline-block;
  color: white;
  font-weight: 600;
  font-size: 20px;
}

nav ul li a:hover {
  color: rgb(174, 17, 174);
}

.toggle-button {
  width: 35px;
  position: absolute;
  right: 80px;
  top: 50%;
  transform: translateY(-50%);
  display: none;
  cursor: pointer;
}

.toggle-button span {
  display: block;
  width: 100%;
  height: 2px;
  background-color: white;
  margin-bottom: 8px;
}

.bookatable {
  color: #fff;
  background-color: #555556;
  border-radius: 6px;
  text-transform: uppercase;
  font-weight: 600;
  font-size: 18px;
  line-height: 1;
  text-align: center;
  padding: 9px 6px 8px 8px;
  transition-duration: 0.6s;
  transition-timing-function: ease-in-out;
  letter-spacing: -.3px;
  cursor: pointer;
}

.bookatable:hover {
  background-color: #909092;
  color: white;
}

/* ---- BREAKPOINTS ---- */
@media (max-width: 1100px) {
  header {
    display: flex;
    flex-direction: row;
    position: relative;
    justify-content: flex-start;
    align-items: center;
  }
  .toggle-button {
    display: block;
  }
  nav {
    display: none;
    width: 100%;
    border-top: 1px solid white;
    padding: 20px 0;
    position: absolute;
    top: 100%;
    left: 0;
    background-color: #212223;
  }
  nav ul li {
    padding: 15px 0;
    width: 100%;
  }
  nav ul {
    text-align: center;
  }
  nav.show {
    display: block;
  }
}

HTML(无需修改)

<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>

<header class="header">
  <div class="toggle-button" onclick="myFunction();">
    <span></span>
    <span></span>
    <span></span>
  </div>
  <div class="logo">
    <a href="index.html"><img src="https://i.sstatic.net/y0nIA.png" alt="Logo"></a>
  </div>
  <nav id="nav">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Menu</a></li>
      <li><a href="#">About Us</a></li>
      <li><a href="#">Contact Us</a></li>
      <li><a href="#" class="bookatable">Book A Table</a></li>
    </ul>
  </nav>
</header>

<script type="text/javascript">
  function myFunction() {
    var navbar = document.getElementById('nav');
    navbar.classList.toggle('show');
  }
</script>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 09:16:13