【问题标题】:Fixed Menu doesn't work with current dropdown menu固定菜单不适用于当前下拉菜单
【发布时间】:2017-08-15 20:47:49
【问题描述】:

当您将鼠标悬停在“治疗”上时,我创建了这个带有下拉功能的菜单。但是当我应用“位置:固定;”在 CSS 的容器类中,下拉菜单不再起作用。菜单转到固定位置,但下拉功能在固定位置不起作用。我想用 CSS 解决它,有什么建议吗?

.container {
  overflow: hidden;
  background-color: rgba(48, 48, 48, 0.9);
  font-family: Helvetica, sans-serif;
  width: 100%;
  position: fixed;
  margin-top: 0;
  margin-left: 0;
}

.container a {
  float: right;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: right;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
}

.container a:hover,
.dropdown:hover .dropbtn {
  background-color: #ff008f;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: rgba(255, 255, 255, 0.98);
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #f9e9ff;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" type="text/css" href="UCL.css">

  <title>Home</title>

  <div class="stripes">

    <div class="container">
      <a href="contact-us.html" target="_top">Contact Us</a>
      <a href="products-gifts.html" target="_top">Products & 
Gifts</a>
      <div class="dropdown">
        <button class="dropbtn">Treatments</button>
        <div class="dropdown-content">
          <a href="body-contouring.html" target="_top">Body 
Contouring</a>
          <a href="cellulite.html" target="_top">Cellulite</a>
          <a href="laser-hair-reduction.html" target="_top">Laser Hair Reduction</a>
          <a href="laser-peels.html" target="_top">Laser 
Peels</a>
          <a href="led.html" target="_top">LED</a>
          <a href="photofacial.html" target="_top>" Photofacial & Photobody</a>
            <a href="spider-veins.html" target="_top">Spider 
Veins</a>
        </div>
      </div>
      <a href="home.html" target="_top">Home</a>
    </div>

</head>

【问题讨论】:

  • 与您的具体问题无关,但您提供的 HTML 存在一些问题:(1) 您不应该在 中包含任何 HTML 内容 - 这是为元数据保留的 (meta标签,link 标签,title 标签)。您的内容应该在 中。 (2) 你最后缺少一个关闭 &lt;/div&gt; 来关闭你的 .stripes 容器。

标签: html css


【解决方案1】:

.container 元素上的overflow: hidden 阻止了下拉菜单的显示,而不是position: fixed。通过隐藏溢出,您可以防止显示超出.container 尺寸的元素(例如下拉菜单)。

.container {
  /* overflow: hidden; */
  background-color: rgba(48, 48, 48, 0.9);
  font-family: Helvetica, sans-serif;
  width: 100%;
  position: fixed;
  margin-top: 0;
  margin-left: 0;
}

.container a {
  float: right;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: right;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
}

.container a:hover,
.dropdown:hover .dropbtn {
  background-color: #ff008f;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: rgba(255, 255, 255, 0.98);
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #f9e9ff;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="stripes">
  <div class="container">
    <a href="contact-us.html" target="_top">Contact Us</a>
    <a href="products-gifts.html" target="_top">Products & 
Gifts</a>
    <div class="dropdown">
      <button class="dropbtn">Treatments</button>
      <div class="dropdown-content">
        <a href="body-contouring.html" target="_top">Body 
Contouring</a>
        <a href="cellulite.html" target="_top">Cellulite</a>
        <a href="laser-hair-reduction.html" target="_top">Laser Hair Reduction</a>
        <a href="laser-peels.html" target="_top">Laser 
Peels</a>
        <a href="led.html" target="_top">LED</a>
        <a href="photofacial.html" target="_top>" Photofacial & Photobody</a>
          <a href="spider-veins.html" target="_top">Spider 
Veins</a>
      </div>
    </div>
    <a href="home.html" target="_top">Home</a>
  </div>
</div>

【讨论】:

  • 这是有道理的,这是我唯一忽略改变的事情。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 2015-07-09
相关资源
最近更新 更多