html - 为什么我的元素上的 z-index 不起作用?

标签 html css z-index

我设置了我的代码,所以我在底部有主图,在顶部有叠加层,叠加层中有文本和按钮。我也有带 z-index 的导航栏,但出于某种原因,覆盖层中的简历按钮不起作用。

HTML

    <div id="header">
        <a href="index.html"><div id="leftHeader">
        <img src="assets/logo2.jpg" alt="Logo" style="width:65px;height:65px">
        <h1>Amanda Farrington</h1>
    </div>
        <div id="nav">

      <ul>
        <li><a href="about.html">About</a></li>
        <li><a href="#workJump">Work</a></li>
        <li><a href="contact.html">Contact</a></li>
        <li><a href="notes.html">Notes</a></li>
      </ul>
        </div>
    </div>



    <div id="hero">
        <div id="heroImage">
        <img src="assets/trees.jpg" alt="trees" style="width:100%;height:10%">
        </div>

        <div id="overlay">
        <h2>Amanda Farrington</h2>
        <h3>Graphic Artist | Web Designer</h3>
    <a href="assets/resume.pdf" class="down">View Resume</a>                


        </div>
    </div>

CSS

#header {
  color: #D7DADB;
  font-family: 'Roboto', sans-serif;
  font-weight: 300;
  font-size : 15px;
  text-align: left;
  width: 100%;
  padding-left: 3em;
  position: relative;
  height: 15%;
  box-sizing: border-box;
  padding-top: 1em;

}


#header img
{
    float: left;
    padding-left: 3em;
}

h1{
width: 9em;
float: left;
padding-left: 0.5em;
color: #45CCCC;
padding-bottom: 1px;
}

#nav {
  width: 50%;
  margin:0;
  padding:0;
  text-align: right;
  color: red;
  font-size:20px;
  float: right;
  padding-right: 2em;
  z-index: 99;

}

#nav ul {
  padding: 1px;
}

#nav li {
  display: inline;
  padding: 38px;
}

#nav li a {
  color: #2C3E50;
  text-decoration: none;
}

#nav li a:hover {
  color: #45CCCC;
}

/*----------hero image styles-------------*/
#hero{
    padding-top: 25em;
    width: 100%;
    height: 30em;
    position: relative;
    z-index: -1;
}

#heroImage
{
    top: 9%;
    width: 100%;
    z-index: 1;
    position: absolute;

}

#overlay{
    width: 34em;
    top: -15%;
    margin-left: 30%;
    z-index: 2;
    position: relative;
    clear: left;


}

h2{
    width: 100%;
    position: relative;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    font-size: 60px;
    float: center;
    color: white;
    opacity: 1.0;
    text-shadow: 2px 2px 3px #000000;
    text-align: center;
}

h3{
    width: 100%;
    position: relative;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    font-size: 30px;
    color: #e5e5e5;
    opacity: 1.0;
    text-shadow: 2px 3px 2px #000000;
    text-align: center;

}

a.down{
    z-index: 100;
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    text-decoration: none;
    color: #181b1e;
    background: #45CCCC;
    position: relative;
    padding: 0.6em 0.2em;
    font-size: 1.2em;
    -webkit-border-radius: 6px;
    width: 30%;
    position: relative;
    display: block;
    margin-left: auto;
    margin-right: auto;
    text-align: center;

}

a.down:hover{
    text-decoration: underline;
    color: white;

}

最佳答案

因为 z-index 仅适用于NOT 设置为position: static 的元素。请记住,每个元素都默认设置为 position:static

尝试将元素设置为 position:absolute;relative

还有所有其他类型的定位,如 position:fixedposition:sticky

关于html - 为什么我的元素上的 z-index 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719411/

相关文章:

css - 移动设备宽度 100%

html - 如何在 div 之间设置不同的 z-Index?

html - localStorage eventHandler 未被调用

html - 是否可以使固定位置的 div 响应?

html - 如果子链接或孙链接使用 CSS 处于事件状态,则将父链接设置为事件

javascript - 验证时,下拉框应该是彩色的,而不是警告框

javascript - 如何根据光滑的轮播宽度有条件地添加下一个和上一个箭头?

css - 使用图像居中并填充 div 而不会失真

html - 如何在图像上覆盖 <p> 中的文本

html - 表格中覆盖 div 的适当缩放