所以我创建了一个 gallery 页面来保存网站的图片,页面的图片似乎放在了我的导航栏的下拉框的前面.由于下拉框在图片后面,我无法点击它们来浏览其他页面.有没有一种方法可以让下拉框优先于图像?这样,我就可以像往常一样使用下拉框了.代码如下:

* {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
      }

      body {
        font-family: sans-serif;
        background-color: #788bc8;
        min-height: 100vh;
        overflow-x: hidden;
      }

      span {
        color: white;
      }

      .navbar {
        background-color: #d0f0c0;
        height: 80px;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 3%;
      }

      .logo {
        font-size: 45px;
      }

      .logo a {
        color: red;
        text-decoration: none;
      }

      .navbar ul {
        display: flex;
        list-style-type: none;
      }

      .navbar ul li {
        padding: 10px 25px;
        position: relative;
      }

      .navbar ul li a {
        color: black;
        text-decoration: none;
        font-size: 22px;

        transition: all 0.2s;
      }

      .fas {
        float: right;
        margin-left: 10px;
        padding-top: 3px;
      }

      .navbar ul li a:hover {
        color: #4dac62;
      }

      .navbar ul li :where(.dropdownda, .dropdownfa) ul {
        display: block;
        margin: 10px;
        text-align: center;
      }

      .navbar ul li :where(.dropdownda, .dropdownfa) ul li {
        width: 200px;
        padding: 10px;
      }

      .dropdownda,
      .dropdownfa {
        display: none;
        position: absolute;
        left: 0;
        top: 100%;
        background-color: #d0f0c0;
      }

      .navbar > ul > li:hover :where(.dropdownda, .dropdownfa) {
        display: block;
      }

      .main {
        position: relative;
        justify-content: center;
        align-items: center;
        margin-top: 10px;
        width: 100vw;
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 300px));
        column-gap: 10px;
        row-gap: 10px;
        padding: 10px 0px;
      }

      img {
        width: 300px;
        height: 300px;
        object-fit: cover;
        object-position: center;
      }
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Be Aware - First Response</title>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
    />
  </head>

  <body>
    <div class="navbar">
      <h1 class="logo">
        <a href="home.html">BE <span>AWARE</span></a>
      </h1>
      <ul>
        <li>
          <a href="#">Drug Advocacy <i class="fas fa-caret-down"></i></a>
          <div class="dropdownda">
            <ul>
              <li><a href="dAwareness.html">Drug Awareness</a></li>
              <li><a href="dTypes.html">Types of Drugs</a></li>
              <li><a href="aAndp.html">Abuse and Prevention</a></li>
            </ul>
          </div>
        </li>
        <li>
          <a href="#">First Aid <i class="fas fa-caret-down"></i></a>
          <div class="dropdownfa">
            <ul>
              <li><a href="faExplain.html">Explaining First Aid</a></li>
              <li><a href="faPurpose.html">Purpose of First Aid </a></li>
              <li><a href="faKit.html">First Aid Kit</a></li>
            </ul>
          </div>
        </li>
        <li><a href="cause.html">Our Cause</a></li>
        <li><a href="gallery.html">Gallery</a></li>
        <li><a href="login.html">Logout</a></li>
      </ul>
    </div>

    <div class="main">
      <img src="img1.png" alt="" />
      <img src="img2.png" alt="" />
      <img src="img3.jpg" alt="" />
      <img src="img4.png" alt="" />
    </div>
  </body>
</html>

我不确定图像是否会显示在代码片段中,但您可以注意到下拉框将位于图像应该位于的位置后面.我试过下拉框上的!important,但它什么都不起作用.

推荐答案

更轻松地仅为下拉菜单添加最大z索引值

 .dropdownda,
  .dropdownfa {
    display: none;
    position: absolute;
    left: 0;
    top: 100%;
    background-color: #d0f0c0;
    z-index:999; /* max value can be anything+*/
  }

* {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
      }

      body {
        font-family: sans-serif;
        background-color: #788bc8;
        min-height: 100vh;
        overflow-x: hidden;
      }

      span {
        color: white;
      }

      .navbar {
        background-color: #d0f0c0;
        height: 80px;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 3%;
      }

      .logo {
        font-size: 45px;
      }

      .logo a {
        color: red;
        text-decoration: none;
      }

      .navbar ul {
        display: flex;
        list-style-type: none;
      }

      .navbar ul li {
        padding: 10px 25px;
        position: relative;
      }

      .navbar ul li a {
        color: black;
        text-decoration: none;
        font-size: 22px;

        transition: all 0.2s;
      }

      .fas {
        float: right;
        margin-left: 10px;
        padding-top: 3px;
      }

      .navbar ul li a:hover {
        color: #4dac62;
      }

      .navbar ul li :where(.dropdownda, .dropdownfa) ul {
        display: block;
        margin: 10px;
        text-align: center;
      }

      .navbar ul li :where(.dropdownda, .dropdownfa) ul li {
        width: 200px;
        padding: 10px;
      }

      .dropdownda,
      .dropdownfa {
        display: none;
        position: absolute;
        left: 0;
        top: 100%;
        background-color: #d0f0c0;
        z-index:999;
      }

      .navbar > ul > li:hover :where(.dropdownda, .dropdownfa) {
        display: block;
      }

      .main {
        position: relative;
        justify-content: center;
        align-items: center;
        margin-top: 10px;
        width: 100vw;
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 300px));
        column-gap: 10px;
        row-gap: 10px;
        padding: 10px 0px;
      }

      img {
        width: 300px;
        height: 300px;
        object-fit: cover;
        object-position: center;
      }
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Be Aware - First Response</title>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
    />
  </head>

  <body>
    <div class="navbar">
      <h1 class="logo">
        <a href="home.html">BE <span>AWARE</span></a>
      </h1>
      <ul>
        <li>
          <a href="#">Drug Advocacy <i class="fas fa-caret-down"></i></a>
          <div class="dropdownda">
            <ul>
              <li><a href="dAwareness.html">Drug Awareness</a></li>
              <li><a href="dTypes.html">Types of Drugs</a></li>
              <li><a href="aAndp.html">Abuse and Prevention</a></li>
            </ul>
          </div>
        </li>
        <li>
          <a href="#">First Aid <i class="fas fa-caret-down"></i></a>
          <div class="dropdownfa">
            <ul>
              <li><a href="faExplain.html">Explaining First Aid</a></li>
              <li><a href="faPurpose.html">Purpose of First Aid </a></li>
              <li><a href="faKit.html">First Aid Kit</a></li>
            </ul>
          </div>
        </li>
        <li><a href="cause.html">Our Cause</a></li>
        <li><a href="gallery.html">Gallery</a></li>
        <li><a href="login.html">Logout</a></li>
      </ul>
    </div>

    <div class="main">
      <img src="img1.png" alt="" />
      <img src="img2.png" alt="" />
      <img src="img3.jpg" alt="" />
      <img src="img4.png" alt="" />
    </div>
  </body>
</html>

Html相关问答推荐

后续使用收件箱从网页表中提取值

放大缩小标题在外面的图像

如何在所有屏幕大小下使带有背景图像的伪元素作为标题的衬底?

SCSS动画错误:心脏在页面刷新时启动动画,原因是:Checked和:Not(:Checked) Select 器

如何用css在右侧创建多个半圆

如何将文本环绕心形(而不仅仅是圆形)?

如何在CSS中延迟触发2个转换?

静态DIV的标签,显示从窗体输入的值

如何使用css在响应图像后面添加形状?

Tailwind CSS 忽略我的元素的填充

如何修剪 flex: 1 内的垂直文本?

Angular中如何向单选按钮添加验证

如何将一个边框向下移动

如何在 flex 项目中忽略子元素的宽度

如何只为 Navbar 中的一个元素提供动画,其余元素没有使用 css 的动画

如何将两个平行 div 的页面内的表格居中?

ul li 右侧的 Bootstrap 加载微调器

th 内的 div 标签没有占据全高

如何让 backdrop-filter: blur() 效果在 iOS 上运行?

SVG 适用于 Safari,不适用于 Chrome