按钮未出现在图像的前面

时间:2018-10-15 02:45:04

标签: 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

2 个答案:

答案 0 :(得分:1)

尝试一下:

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>

答案 1 :(得分:0)

据我了解,您希望在<p><a>标签后面显示图片。我可以看到的最简单的方法是让div包裹这些标签(例如您的item类),然后在CSS中为其应用一个background-image:url(<enter image url here>)属性。

例如:

HTML:

<div class='item'>
    <p>My Caption here</p>
    <a href='#'>
        <div class='play'>
    </a>
</div>

CSS:

.item {
    background-image : url('./example-image.png');
}

insert 
other 
css 
code  
here
...