html - 使用子链接时保持父链接悬停

标签 html css

我在设置下拉菜单时遇到了麻烦。只要我使用子菜单,父链接就需要处于悬停状态。

/* Dropdown Button */

.dropbtn {
  background-color: transparent;
  color: #585558;
  padding: 16px;
  font-size: 1em;
  border: none;
  cursor: pointer;
  font-weight: bold;
}


/* The container <div> - needed to position the dropdown content */

.dropdown {
  position: relative;
  display: inline-block;
}


/* Dropdown Content (Hidden by Default) */

.dropdown-content {
  display: none;
  position: absolute;
  min-width: 160px;
  z-index: 1;
  padding-top: -16px;
}


/* Links inside the dropdown */

.dropdown-content a {
  color: #585558;
  padding: 6px 16px;
  text-decoration: none;
  display: block;
  font-weight: bold;
}

.fb:hover {
  color: #3b5998;
}

.ig:hover {
  color: #fb3958;
}

.sc:hover {
  color: #fffc00;
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}

.dropbtn:hover>a {
  color: #ef5350;
}

.backslash {
  color: #8e8e8e;
  font-weight: bold;
}
<div class="dropdown">
  <button class="dropbtn">Sociale medier</button>
  <div class="dropdown-content">
    <a class="fb" target="_blank" href="#">Facebook</a>
    <a class="ig" target="_blank" href="#">Instagram</a>
    <a class="sc" target="_blank" href="#">Snapchat</a>
  </div>
</div>

如何在使用子链接时让父链接保持悬停状态?

最佳答案

你只需要添加:hover 状态。

.dropdown:hover .dropbtn {
  background: aqua;
}

/* Dropdown Button */

.dropbtn {
  background-color: transparent;
  color: #585558;
  padding: 16px;
  font-size: 1em;
  border: none;
  cursor: pointer;
  font-weight: bold;
}


/* The container <div> - needed to position the dropdown content */

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown:hover .dropbtn {
  background: aqua;
}


/* Dropdown Content (Hidden by Default) */

.dropdown-content {
  display: none;
  position: absolute;
  min-width: 160px;
  z-index: 1;
  padding-top: -16px;
}


/* Links inside the dropdown */

.dropdown-content a {
  color: #585558;
  padding: 6px 16px;
  text-decoration: none;
  display: block;
  font-weight: bold;
}

.fb:hover {
  color: #3b5998;
}

.ig:hover {
  color: #fb3958;
}

.sc:hover {
  color: #fffc00;
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}

.dropbtn:hover>a {
  color: #ef5350;
}

.backslash {
  color: #8e8e8e;
  font-weight: bold;
}
<div class="dropdown">
  <button class="dropbtn">Sociale medier</button>
  <div class="dropdown-content">
    <a class="fb" target="_blank" href="#">Facebook</a>
    <a class="ig" target="_blank" href="#">Instagram</a>
    <a class="sc" target="_blank" href="#">Snapchat</a>
  </div>
</div>

关于html - 使用子链接时保持父链接悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348073/

相关文章:

html - 超大屏幕 div 溢出

CSS 中的 HTML 表单标签样式不适用于 html 中的标签

javascript - 有什么方法可以从浏览器运行服务器或点对点?

html - 使用 CSS 设计列表样式

html - 如何减小占用太多空间的 HTML 元素的大小-

javascript - 使 slider 旋转中的形状不可点击

javascript - 你能在 HTML 标签上同时使用 async 和 defer 属性吗?

html - 使 CSS 下拉列表出现在 div 上

jquery - 不断在表格底部添加空间

css - 进度条 Div 样式