css 我无法在下拉菜单中正确使用z索引来正确显示溢出

5n0oy7gb  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(115)

我做了一个下拉菜单,在导航标签中有一堆名字。在标签之后,我放了一个主标签。由于这些项共享main的顶部边框,因此一些项会溢出导航并进入main的空间。现在我不想使用overflow:scroll。我希望项目仍然显示在悬停页面的主要方面。我试着使用z-index,但我不能让它工作。你们能看一下吗我对CSS和HTML很陌生。谢谢。你可以在下面找到我所有的代码。我还添加了一张照片。x1c 0d1x

<nav>
    <div id = "div">
      <ul id = "navlist">
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Elves</button>
            <div id="content">
              <a id="text" href = "#">Finwë</a>
              <br>
              <a id="text" href = "#">Fëanor</a>
              <br>
              <a id="text" href = "#">Finarfin</a>
              <br>
              <a id="text" href = "#">Fingolfin</a>
            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Dwarves</button>
            <div id="content">
              <a id="text" href = "#">Durin</a>
              <br>
              <a id="text" href = "#">Thror</a>
              <br>
              <a id="text" href = "#">Thrain</a>
              <br>
              <a id="text" href = "#">Thorin</a>
            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Humans</button>
            <div id="content">
              <a id="text" href = "#">
                Bëor</a>
              <br>
              <a id="text" href = "#">Haleth</a>
              <br>
              <a id="text" href = "#">Barahir</a>
              <br>
              <a id="text" href = "#">Beren</a>
              <br>
              <a id="text" href = "#">Aragorn</a>

            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Hobbits</button>
            <div id="content">
              <a id="text" href = "#">Bilbo</a>
              <br>
              <a id="text" href = "#">Frodo</a>
              <br>
              <a id="text" href = "#">Samwise</a>
              <br>
              <a id="text" href = "#">Merry</a>
              <br>
              <a id="text" href = "#">Pippin</a>
            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Sorcerers</button>
            <div id="content">
              <a id="text" href = "#">Gandalf</a>
              <br>
              <a id="text" href = "#">Saruman</a>
              <br>
              <a id="text" href = "#">Radagast</a>
              <br>
              <a id="text" href = "#">Alatar</a>
              <br>
              <a id="text" href = "#">Pallando</a>
            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Beasts</button>
            <div id="content">
              <a id="text" href = "#">Ungoliant</a>
              <br>
              <a id="text" href = "#">Huan</a>
              <br>
              <a id="text" href = "#">Carcharoth</a>
              <br>
              <a id="text" href = "#">Smaug</a>
            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Places</button>
            <div id="content">
              <a id="text" href = "#">Valinor</a>
              <br>
              <a id="text" href = "#">Utumno</a>
              <br>
              <a id="text" href = "#">Doriath</a>
              <br>
              <a id="text" href = "#">Gondolin</a>
              <br>
              <a id="text" href = "#">Angband</a>
            </div>
          </div>
        </li>
        <li class = "litem">
          <div id = "dropdown">
            <button id="button">Valar</button>
            <div id="content">
              <a id="text" href = "#">
                Manwë</a>
              <br>
              <a id="text" href = "#">Varda</a>
              <br>
              <a id="text" href = "#">Ulmo</a>
              <br>
              <a id="text" href = "#">Aulë</a>
              <br>
              <a id="text" href = "#">Melkor</a>
              <br>
              <a id="text" href = "#">Yavanna</a>
              <br>
              <a id="text" href = "#">Tulkas</a>
            </div>
          </div>
        </li>
    </div>
  </nav>
  <main>
  </main>
   



       @font-face {
  font-family: maldirenc;
  src: url(Fonts/Middleearth-ao6m.ttf);
}
* {
  margin: 0px;
  padding: 0px;
}

#ul {
  list-style-type: none;
  float: right;
}
#li {
  float: right;
  position: relative;
  top: 10px;
  right: 10px;
  padding: 10px;
}

#layout {
  width: 1510px;
  height: 1440px;
  border-style: solid;
  margin: auto;
  background-repeat: no-repeat;
  background-position: center;
}

#logo {
  width: 150px;
  position: relative;
  left: 15px;
  top: 15px;
}

header {
  height: 200px;
  width: 1510px;
  background-color: #f08c1e;
  opacity: 0.85;
}
nav {
  opacity: 0.85;
  height: 200px;
  background-color: #f08c1e;
  border: none;
  overflow: visible;
}
h1 {
  text-align: center;
  position: relative;
  bottom: 40px;
  left: 230px;
  display: inline;
  font-size: 90px;
  font-family: maldirenc;
  color: #d47d20;
}

a {
  text-decoration: none;
}

h1:hover {
  color: blue;
}
h1:active {
  color: indigo;
}

#navlist {
  list-style-type: none;
  background-color: #f08c1e;
  position: relative;
  display: inline-block;
}

#button {
  border: none;
  font-size: 25px;
  font-family: maldirenc;
  position: relative;

  background-color: #f08c1e;
  color: white;
}

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

#content {
  font-size: 20px;
  font-family: maldirenc;
  display: none;
  z-index: 1;
}

#dropdown:hover #content {
  display: block;
  background-color: #f08c1e;
  opacity: 0.9;
  z-index: 1;
}

#text {
  color: mintcream;
  opacity: 0.9;
  font-size: 17.5px;
  z-index: index 1;
}
#text:hover {
  color: #ff0000;
  opacity: 0.8;
  transform: translateY(-10px);
  transition: all 0.4 ease-in;
  transform: translateY(0px);
}
.litem {
  float: left;
  padding: 25px;
}

#div {
  position: relative;
  text-align: center;
  top: 5px;
}

main {
  border-style: solid;
  height: 750px;
  opacity: 0.85;
  background-color: #f08c1e;
  z-index: -1;
}
nukf8bse

nukf8bse1#

z-index只适用于定位元素(任何与position: static不同的元素),通过添加position: relative并关闭ul标签,我得到了这个结果

@font-face {
  font-family: maldirenc;
  src: url(Fonts/Middleearth-ao6m.ttf);
}

* {
  margin: 0px;
  padding: 0px;
}

#ul {
  list-style-type: none;
  float: right;
}

#li {
  float: right;
  position: relative;
  top: 10px;
  right: 10px;
  padding: 10px;
}

#layout {
  width: 1510px;
  height: 1440px;
  border-style: solid;
  margin: auto;
  background-repeat: no-repeat;
  background-position: center;
}

#logo {
  width: 150px;
  position: relative;
  left: 15px;
  top: 15px;
}

header {
  height: 200px;
  width: 1510px;
  background-color: #f08c1e;
  opacity: 0.85;
}

nav {
  opacity: 0.85;
  height: 200px;
  background-color: #f08c1e;
  border: none;
  overflow: visible;
}

h1 {
  text-align: center;
  position: relative;
  bottom: 40px;
  left: 230px;
  display: inline;
  font-size: 90px;
  font-family: maldirenc;
  color: #d47d20;
}

a {
  text-decoration: none;
}

h1:hover {
  color: blue;
}

h1:active {
  color: indigo;
}

#navlist {
  list-style-type: none;
  background-color: #f08c1e;
  position: relative;
  display: inline-block;
}

#button {
  border: none;
  font-size: 25px;
  font-family: maldirenc;
  position: relative;
  background-color: #f08c1e;
  color: white;
}

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

#content {
  font-size: 20px;
  font-family: maldirenc;
  display: none;
  z-index: 1;
}

#dropdown:hover #content {
  display: block;
  background-color: #f08c1e;
  opacity: 0.9;
  z-index: 1;
}

#text {
  color: mintcream;
  opacity: 0.9;
  font-size: 17.5px;
  z-index: index 1;
}

#text:hover {
  color: #ff0000;
  opacity: 0.8;
  transform: translateY(-10px);
  transition: all 0.4 ease-in;
  transform: translateY(0px);
}

.litem {
  float: left;
  padding: 25px;
}

#div {
  position: relative;
  text-align: center;
  top: 5px;
}

main {
  position: relative;
  border-style: solid;
  height: 750px;
  opacity: 0.85;
  background-color: #f08c1e;
  z-index: -1;
}
<nav>
  <div id="div">
    <ul id="navlist">
      <li class="litem">
        <div id="dropdown">
          <button id="button">Elves</button>
          <div id="content">
            <a id="text" href="#">Finwë</a>
            <br>
            <a id="text" href="#">Fëanor</a>
            <br>
            <a id="text" href="#">Finarfin</a>
            <br>
            <a id="text" href="#">Fingolfin</a>
          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Dwarves</button>
          <div id="content">
            <a id="text" href="#">Durin</a>
            <br>
            <a id="text" href="#">Thror</a>
            <br>
            <a id="text" href="#">Thrain</a>
            <br>
            <a id="text" href="#">Thorin</a>
          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Humans</button>
          <div id="content">
            <a id="text" href="#">
                Bëor</a>
            <br>
            <a id="text" href="#">Haleth</a>
            <br>
            <a id="text" href="#">Barahir</a>
            <br>
            <a id="text" href="#">Beren</a>
            <br>
            <a id="text" href="#">Aragorn</a>

          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Hobbits</button>
          <div id="content">
            <a id="text" href="#">Bilbo</a>
            <br>
            <a id="text" href="#">Frodo</a>
            <br>
            <a id="text" href="#">Samwise</a>
            <br>
            <a id="text" href="#">Merry</a>
            <br>
            <a id="text" href="#">Pippin</a>
          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Sorcerers</button>
          <div id="content">
            <a id="text" href="#">Gandalf</a>
            <br>
            <a id="text" href="#">Saruman</a>
            <br>
            <a id="text" href="#">Radagast</a>
            <br>
            <a id="text" href="#">Alatar</a>
            <br>
            <a id="text" href="#">Pallando</a>
          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Beasts</button>
          <div id="content">
            <a id="text" href="#">Ungoliant</a>
            <br>
            <a id="text" href="#">Huan</a>
            <br>
            <a id="text" href="#">Carcharoth</a>
            <br>
            <a id="text" href="#">Smaug</a>
          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Places</button>
          <div id="content">
            <a id="text" href="#">Valinor</a>
            <br>
            <a id="text" href="#">Utumno</a>
            <br>
            <a id="text" href="#">Doriath</a>
            <br>
            <a id="text" href="#">Gondolin</a>
            <br>
            <a id="text" href="#">Angband</a>
          </div>
        </div>
      </li>
      <li class="litem">
        <div id="dropdown">
          <button id="button">Valar</button>
          <div id="content">
            <a id="text" href="#">
                Manwë</a>
            <br>
            <a id="text" href="#">Varda</a>
            <br>
            <a id="text" href="#">Ulmo</a>
            <br>
            <a id="text" href="#">Aulë</a>
            <br>
            <a id="text" href="#">Melkor</a>
            <br>
            <a id="text" href="#">Yavanna</a>
            <br>
            <a id="text" href="#">Tulkas</a>
          </div>
        </div>
      </li>
    </ul>
  </div>
</nav>
<main>
</main>

这是你想要的结果吗如果是这种情况,请考虑在nav中使用z-index,而不是与position: stickyfixedabsolute沿着使用正值。

相关问题