图片悬停不显示显示div

时间:2021-01-28 12:28:53

标签: html css

我必须在 html 中创建一个图像,并添加 css 悬停以在用户将鼠标悬停在图像上方时显示一个 div。

我尝试将 image:hover 添加到我的 css 中,但它似乎不起作用,我不明白为什么。

这是我的 html

<div class="col-lg-6 col-sm-6">
  <div class="image">
    <img class="img-fluid" src="images/example.png" alt="Website" />
    <div class="overlay">
      <a class="portfolio-box" href="http://example.com" target="_blank">
        <div class="portfolio-box-caption">
          <div class="project-name">Example</div>
        </div>
      </a>
    </div>
  </div>
</div>

这是我的 css

.overlay {
  display: none;
}

.image {
  position: relative;
  display: inline-block;
}

.image:hover .overlay {
  position: absolute;
  display: block;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  bottom: 0;
  text-align: center;
  opacity: 0;
  color: #fff;
  background: #f86d27;
  transition: opacity 0.25s ease;
  text-align: center;
}

1 个答案:

答案 0 :(得分:1)

正如在评论中提到的,您必须删除 opacity:0;。它将使您的悬停放置样式不可见。

.overlay {
  display: none;
}

.image {
  position: relative;
  display: inline-block;
}

.image:hover .overlay {
  position: absolute;
  display: block;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  bottom: 0;
  text-align: center;
  color: #fff;
  background: #f86d27;
  transition: opacity 0.25s ease;
  text-align: center;
}
<div class="col-lg-6 col-sm-6">
  <div class="image">
    <img class="img-fluid" src="images/example.png" alt="Website" />
    <div class="overlay">
      <a class="portfolio-box" href="http://example.com" target="_blank">
        <div class="portfolio-box-caption">
          <div class="project-name">Example</div>
        </div>
      </a>
    </div>
  </div>
</div>

相关问题