html - 按钮没有出现在图像前面

标签 html css

我已经放置了我的按钮,但我希​​望 int 位于图像之上,但出于某种原因 z-index: 1; 无法正常工作。我还尝试了每个 position 属性以查看它是否有效,但没有任何变化,我尝试将 z-index 添加到下面的所有 css 代码中,但没有任何效果我搜索了互联网,包括 YouTube,并尝试了那里的方法,但似乎没有任何效果。

我的 HTML 代码如下:

a .play {
  background: steelblue;
  border-radius: 50% / 10%;
  color: #FFFFFF;
  font-size: 2em;
  height: 3em;
  margin-top: -350px;
  float: right;
  margin-right: 10px;
  margin-top: 25px;
  padding: 0;
  position: relative;
  z-index: 1;
  text-align: center;
  text-indent: 0.1em;
  transition: all 150ms ease-out;
  width: 4em;
}

a .play:hover {
  background: #ff0000;
}

a .play::before {
  background: inherit;
  border-radius: 5% / 50%;
  bottom: 9%;
  content: "";
  left: -5%;
  position: absolute;
  right: -5%;
  top: 9%;
}

a .play::after {
  border-style: solid;
  border-width: 1em 0 1em 1.732em;
  border-color: transparent transparent transparent rgba(255, 255, 255, 0.75);
  content: ' ';
  font-size: 0.75em;
  height: 0;
  margin: -1em 0 0 -0.75em;
  top: 50%;
  position: absolute;
  width: 0;
}
<div id="index-gallery">
  <div class="item">
    <img src="img/after.jpg" alt="" width="300px" />
    <p>My Caption here</p>
    <a href="#">
      <div class="play"></div>
    </a>
  </div>
  <div class="item2">
    <img src="img/after.jpg" alt="" width="300px" />
    <p>My Caption here</p>
    <a href="#">
      <div class="play2"></div>
    </a>
  </div>
  <div class="item3">
    <img src="img/after.jpg" alt="" width="300px" />
    <p>My Caption here</p>
    <a href="#">
      <div class="play3"></div>
    </a>
  </div>
  <div class="item4">
    <img src="img/after.jpg" alt="" width="300px" />
    <p>My Caption here</p>
    <a href="#">
      <div class="play4"></div>
    </a>
  </div>
</div>

Button not appearing on top of image

最佳答案

试试这个:

div.item {
    position: relative;
}

a.button-wrapper {
    display: block;
    height: 100px;
    position: absolute;
    top: 16px;
    left: 80px;
}

a .play {
    background: steelblue;
    border-radius: 50% / 10%;
    color: #FFFFFF;
    font-size: 2em;
    height: 3em;
    float: left;
    margin-right: 10px;
    margin-top: 0;
    padding: 0;
    position: relative;
    z-index: 1;
    text-align: center;
    text-indent: 0.1em;
    transition: all 150ms ease-out;
    width: 4em;
}

a .play:hover {
    background: #ff0000;
}

a .play::before {
    background: inherit;
    border-radius: 5% / 50%;
    bottom: 9%;
    content: "";
    left: -5%;
    position: absolute;
    right: -5%;
    top: 9%;
}

a .play::after {
    border-style: solid;
    border-width: 1em 0 1em 1.732em;
    border-color: transparent transparent transparent rgba(255, 255, 255, 0.75);
    content: ' ';
    font-size: 0.75em;
    height: 0;
    margin: -1em 0 0 -0.75em;
    top: 50%;
    position: absolute;
    width: 0;
}
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
  <div id="index-gallery">
    <div class="item">
      <img src="https://via.placeholder.com/350x150" alt="" width="300px" />
      <p>My Caption here</p>
      <a href="#" class="button-wrapper">
        <div class="play"></div>
      </a>
    </div>
    <div class="item">
      <img src="https://via.placeholder.com/350x150" alt="" width="300px" />
      <p>My Caption here</p>
      <a href="#" class="button-wrapper">
        <div class="play"></div>
      </a>
    </div>
    <div class="item">
      <img src="https://via.placeholder.com/350x150" alt="" width="300px" />
      <p>My Caption here</p>
      <a href="#" class="button-wrapper">
        <div class="play"></div>
      </a>
    </div>
    <div class="item">
      <img src="https://via.placeholder.com/350x150" alt="" width="300px" />
      <p>My Caption here</p>
      <a href="#" class="button-wrapper">
        <div class="play"></div>
      </a>
    </div>
</body>
</html>

关于html - 按钮没有出现在图像前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52809088/

相关文章:

html - 垂直和水平对齐 div 内的 div 与 :after selector

css - 多个背景图片定位

javascript - 在 css 工作表上用数组格式化段落

html - 需要帮助居中导航栏

html - 在 HTML 表格中单独编辑 TD 的宽度

html - 绝对位置,底部 0,firefox 不工作

css - 如何在带有网格的 bootstrap 3 行中水平和垂直居中内容?

javascript - 如何将页面状态和CSS保存到服务器? (绘图应用程序)

html - 具有最小宽度,宽度%的响应式布局没有避免间隙的简单方法吗?

css - Sharepoint Designer 2010、CSS 和母版页