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

鼠标悬停CSS汉堡按钮时导航项不显示的问题解决求助

解决汉堡按钮悬停仅显示导航的问题

你之前的问题出在选择器层级错误:.menu-button和nav并非父子/后代关系,nav是header的直接子元素,和.menu-button所在的section是同级,所以直接用.menu-button:hover nav无法选中目标元素;而用header:hover范围又太大,会导致整个header区域悬停都触发导航显示。

正确解决方案:使用:has()伪类

通过:has()伪类,让header在包含的.menu-button被悬停或获得焦点时,控制nav的显示状态,精准限定触发条件。

替换你原来的这行代码:

:is(.menu-button:hover, .menu-button:focus-within) nav {
  display: block;
}

改为:

:is(header:has(.menu-button:hover), header:has(.menu-button:focus-within)) nav {
  display: block;
}

为什么这样有效?

:has()是CSS的父选择器,它能选中包含指定子元素的父元素。这里我们让header在.menu-button处于hover或focus-within状态时,才将nav设置为display: block,完美实现仅悬停按钮时显示导航的需求,同时保留键盘导航的可用性(focus-within处理键盘焦点情况)。

兼容性说明

:has()伪类目前已被Chrome、Firefox 121+、Safari等主流浏览器支持,完全可以用于生产环境。

完整修改后的CSS代码

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --HEADER-BGCOLOR: #333;
  --HEADER-COLOR: whitesmoke;
}

/*FONT STYLES*/
html {
  font-size: 1.5rem;
  font-family: 'Roboto', sans-serif;
}

body {
  min-height: 100vh;
  display: flex;
  flex-flow: column nowrap;
}

header {
  background-color: var(--HEADER-BGCOLOR);
  color: var(--HEADER-COLOR);
}

.header-title-line {
  padding: 0.25rem 0.5rem;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
}

/*BUTTON RESET*/
.menu-button {
  background-color: transparent;
  border: none;
  width: 48px;
  height: 48px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

/*HAMBURGER ICON*/
.menu-icon,
.menu-icon::before,
.menu-icon::after {
  background-color: var(--HEADER-COLOR);
  width: 40px;
  height: 5px;
  border-radius: 5px;
  position: absolute;
  transition: all 0.5s;
}

.menu-icon::before,
.menu-icon::after {
  content: "";
}

.menu-icon::before {
  transform: translate(-20px, -12px);
}

.menu-icon::after {
  transform: translate(-20px, 12px);
}

/*HAMBURGER ICON ANIMATION*/
:is(.menu-button:hover, .menu-button:focus-within) .menu-icon {
  background-color: transparent;
  transform: rotate(720deg);
}

:is(.menu-button:hover, .menu-button:focus-within) .menu-icon::before {
  transform: translateX(-20px) rotate(45deg);
}

:is(.menu-button:hover, .menu-button:focus-within) .menu-icon::after {
  transform: translateX(-20px) rotate(-45deg);
}

/* 修改后的导航显示控制 */
:is(header:has(.menu-button:hover), header:has(.menu-button:focus-within)) nav {
  display: block;
}

nav {
  display: none;
  background-color: var(--HEADER-BGCOLOR);
  transform-origin: top center;
  animation: showMenu 0.5s ease-in-out forwards;
}

@keyframes showMenu {
  0% {
    transform: scaleY(0);
  }
  80% {
    transform: scaleY(1.2);
  }
  100% {
    transform: scaleY(1);
  }
}

nav ul {
  list-style-type: none;
  display: flex;
  flex-flow: column nowrap;
}

nav li {
  padding: 0.5rem;
  border-top: 1px solid var(--HEADER-COLOR);
}

nav a {
  display: block;
  text-align: center;
  width: 80%;
  margin: auto;
}

nav a:any-link {
  color: var(--HEADER-COLOR);
  font-weight: bold;
  text-decoration: none;
}

nav a:hover,
nav a:focus {
  transform: scale(1.2);
  transition: all 0.3s;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 23:47:20