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

如何让Hamburger菜单展开时覆盖整个视口?

解决纯CSS汉堡菜单全屏展开问题

要实现菜单展开后填满整个屏幕并覆盖下方内容,核心是通过固定定位让菜单脱离文档流,同时设置合适的尺寸和背景。以下是具体修改方案:

关键修改点

  • 让header固定在顶部,确保菜单展开时不会被页面内容挤下去
  • 菜单展开时设置为全屏固定定位,覆盖下方内容
  • 调整菜单内元素的布局,适配全屏显示

修改后的完整代码

CSS 代码

.header {
  width: 100%;
  z-index: 3;
  background-color: #202124;
  position: fixed; /* 固定header在顶部 */
  top: 0;
  left: 0;
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.header li {
  color: #ffffff;
  font-weight: 700;
  font-size: 22px;
  padding: 15px 20px; /* 增加内边距,提升点击区域和视觉效果 */
}

.header ul a {
  display: block;
  text-decoration: none;
  color: inherit; /* 继承li的颜色 */
}

.header ul a:hover {
  color: #BDBDBD;
}

.header .logo {
  float: left;
  display: block;
  font-size: 1.5em;
  padding: 10px 20px;
  color: #fff; /* 给logo加颜色,避免默认链接色 */
}

.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;
}

.header .menu-icon {
  padding: 28px 20px;
  position: relative;
  float: right;
  cursor: pointer;
}

.header .menu-icon .nav-icon {
  background: #cccccc;
  display: block;
  height: 2px;
  width: 18px;
  position: relative;
  transition: background .2s ease-out;
}

.header .menu-icon .nav-icon:before {
  background: #cccccc;
  content: "";
  display: block;
  height: 100%;
  width: 100%;
  position: absolute;
  transition: all .2s ease-out;
  top: 5px;
}

.header .menu-icon .nav-icon:after {
  background: #cccccc;
  content: "";
  display: block;
  height: 100%;
  width: 100%;
  position: absolute;
  transition: all .2s ease-out;
  top: -5px;
}

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked~.menu {
  max-height: unset; /* 取消max-height限制 */
  position: fixed; /* 固定菜单位置 */
  top: 100%; /* 从header底部开始 */
  left: 0;
  width: 100%;
  min-height: calc(100vh - 60px); /* 占满剩余视口高度(假设header高度60px) */
  background-color: #202124; /* 和header同背景,覆盖下方内容 */
}

.header .menu-btn:checked~.menu-icon .nav-icon {
  background: transparent;
}

.header .menu-btn:checked~.menu-icon .nav-icon:before {
  transform: rotate(-45deg);
  top: 0;
}

.header .menu-btn:checked~.menu-icon .nav-icon::after {
  transform: rotate(45deg);
  top: 0;
}

@media (min-width:48em) {
  .header li {
    float: left;
    padding: 0; /* 桌面端恢复原padding */
  }
  .header li a {
    padding: 20px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
    position: static; /* 桌面端取消固定定位 */
    min-height: auto;
  }
  .header .menu-icon {
    display: none;
  }
}

HTML 代码

<header class="header">
  <a href="/" class="logo"><span class="logo-yellow">L</span><span class="logo-green">K</span></a>
  
  <input class="menu-btn" type="checkbox" id="menu-btn">
  <label class="menu-icon" for="menu-btn"><span class="nav-icon"></span></label>
  
  <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Browse</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</header>

说明

  • 给.header添加position: fixed,让它固定在视口顶部,避免页面滚动时菜单移位
  • 菜单展开时,通过position: fixed让菜单脱离文档流,top:100%确保它从header下方开始,min-height: calc(100vh - 60px)让它占满剩余屏幕高度(如果header高度变化,调整这个数值即可)
  • 给展开的菜单设置和header相同的背景色,完全覆盖下方页面内容
  • 桌面端媒体查询中恢复菜单的默认布局,不影响大屏显示效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 12:13:18