html - 链接的 z-index 问题

标签 html css z-index

我有两个 div,一个 header 和一个 sectionsection 始终占据整个屏幕而 header 只是一小部分。

在我的部分中,我有一个链接,您可以在下面的代码中看到:

* {
  margin: 0;
  padding: 0;
}
#header {
  width: 100%;
  background: green;
  height: 50px;
  z-index: 1;
}
#section {
  background: yellow;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  float: left;
  z-index: -1;
<div id="header">This is my header</div>
<div id="section">
  <br>
  <br>
  <br>
  <br><a href="#">Go to the link</a>
</div>

我需要在我的 header 中使用 z-index 因为如果我不使用,它就不会出现,因为 section 涵盖了整个屏幕。

可以看DEMO HERE

在将代码放入 JSFiddle 之前,我的问题是我无法单击 部分 内的链接,但将代码放入 JSFiddle 后,链接可以正常工作。

为什么会发生这种情况?以及如何在我的网站上解决这个问题?

最佳答案

你不需要z-indexposition ,你可以使用height:100%#section以及body , html

或者如果您愿意,可以使用calc()#section通过使用height:calc(100% - 50px)

body,
html {
  height: 100%;
  margin: 0
}
#header {
  width: 100%;
  background: green;
  height: 50px;
}
#section {
  background: yellow;
  height: 100% /* or -  height:calc(100% - 50px) */
}
<div id="header">
  This is my header!
</div>

<div id="section">
  <a href="#">Go to the link</a>
</div>

请注意,您有 z-index#header没有被定位

关于html - 链接的 z-index 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722688/

相关文章:

jQuery 和 Z-Index 重置

css - 可以在 its::after/::before 伪元素之前制作 div 的背景吗?

html - 堆叠 div 的相对和绝对位置

javascript - 类型错误 : Cannot read property 'id' of undefined

javascript - 如何下载查询需要JS处理的html页面?

jquery - 如何为 Masonry 创建良好的打印样式

javascript - 如何将网页上的特定元素沙盒化?

html - 网格元素未使用 minmax() 函数正确包装

javascript - jquery 移动搜索按钮

javascript - 当同级节点处于事件状态时,将非事件节点返回到其原始顺序