html - 悬停后保持下拉菜单打开(CSS)

标签 html css drop-down-menu menu hover

我创建了一个水平菜单,当您将鼠标悬停在一个元素上时,会出现一个下拉菜单。这一切都很好。但是,当您离开菜单项(使用下拉菜单)时,下拉菜单会消失。我知道这是因为你不再悬停它,但我该如何解决这个问题?注意:我不想要它正下方的下拉菜单,我希望菜单项和下拉菜单之间有一个合理的间隙(正如我现在所拥有的那样)。谢谢。

HTML

<header id="header">
    <div class="container">
        <a href="#top-anchor"><div id="logo"></div></a>
        <nav class="header-menu">
            <a href="index.html" class="header-menu-item">ABOUT</a>
                <div class="about-dropdown">
                    <a href="index.html#core-services-anchor">CORE SERVICES</a>
                    <a href="index.html#atandl-anchor">AT&amp;L</a>
                    <a href="index.html#hseq-anchor">HSEQ</a>
                    <a href="index.html#clients-anchor">CLIENTS</a>
                    <a href="index.html#contact-anchor">CONTACT</a>
                </div>
            <a href="services.html" class="header-menu-item">SERVICES</a>
            <a href="facilities.html" class="header-menu-item">FACILITIES</a>
            <a href="#map-anchor" class="header-menu-item">CONTACT</a>
        </nav>
        <div id="hamburger"></div>
        <!--<div id="box-shadow-menu"></div>-->
    </div>
</header>

CSS

#header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100px;
  user-select: none;
  display: block;
  transition: all 0.8s;
  line-height: 100px;
  z-index: 1000;
  transform: translateX(0);
  backface-visibility: hidden;
  margin: 0;
}

header .container {
  width: 1440px;
  height: 100px;
  border-bottom: 0.75px solid rgba(255,255,255,0.1);
}

#logo {
  width: 55px;
  height: 55px;
  float: left;
  margin-top: 27px;
  background-image: url(../images/logo_white.png);
  background-size: cover;
}

nav.header-menu {
  float: right;
  height: 96px;
  vertical-align: middle;
  padding-top: 1px;
}

.header-menu-item {
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  font-weight: 800;
  margin-left: 20px;
  text-decoration: none;
  color: #ffffff;
  line-height: 96px;
  letter-spacing: 0.5px;
  transition: 0.55s;
}

.toggle {
  opacity: 0.3;
}

.current {
  border-bottom: 2px solid #fff;
  padding-bottom: 40px;
}

.about-dropdown {
  position: absolute;
  background-color: #fff;
  min-width: 160px;
  box-shadow: 0 0 4px 3px rgba(0,0,0,0.1);
  z-index: 3000;
  margin-top: 35px;
  margin-left: -35px;
  border-radius: 3px;
  display: none;
  transition: 0.8s;
}

.about-dropdown a {
  display: block;
  text-decoration: none;
  padding: 0px 28px;
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  font-weight: 800;
  margin: 0;
  line-height: 50px;
  border-bottom: 1px solid rgba(0,0,0,0.1);
}

.header-menu-item:hover + .about-dropdown {
  display: block;
}

Drop Down Menu

最佳答案

在“a”标签上,在悬停时为其添加高度或填充底部。您的“a”标签可能需要绝对定位,这样它的高度就不会影响标题的高度。

类似下面的内容

.about-dropdown a:hover {
    padding-bottom: 30px; /*height dependent on the gap you want to fill*/
    position: absolute; 
}

关于html - 悬停后保持下拉菜单打开(CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42154599/

相关文章:

html - 网站中的异常填充

html - 下拉菜单不适用于 MasterPage

javascript - 客户端存储数据的最佳方式

android - href ="tel:"和免费电话号码 (0800) 可变设备行为

javascript - 带有 CSS 的 jQuery 自定义数据属性

Python 在 Selenium Web 浏览器中选择最后一个下拉选项

flutter - 提示不适用于 Flutter 中的 DropdownButtonFormField

javascript - 在电子邮件中显示 &lt;script&gt; 标签

html - svg inside scroll div 有一个底部排水沟

html - 我正在尝试为我正在构建的社交媒体网站制作一个 HTML 图标。如何创建一个独立的图标?