在悬停时在div内显示文本在css中不起作用

时间:2018-11-05 13:09:43

标签: html css html5 css3 hover

body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  background: url("assets/images/bg.jpg")no-repeat top;
  background-size: cover;
  background-attachment: fixed;
}

.category-container {
  width: 87%;
  margin: 3em auto;
}

.category-container h2 {
  margin: 2% auto;
  background-color: #fc0321;
  color: #fff;
  text-align: center;
  text-shadow: 2px 2px 4px #000;
  font-weight: 400;
  padding: 1%;
  width: 15%;
}

.thumb {
  width: 100%;
  height: 47em;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-around;
  align-content: space-around;
}

.thumb div {
  position: relative;
}

.thumb div::after {
  content: '';
  position: absolute;
}

.thumb div img {
  width: 36.8em;
  height: 22em;
}

.thumb div h3 {
  position: absolute;
  top: 50%;
  left: 50%;
  color: #fff;
  font-weight: 400;
  opacity: 0;
  transform: translate(-50%, -50%);
  transition: opacity ease-out 250ms;
}

.thumb div:hover .thumb div h3 {
  opacity: 1;
}
<section class="category-container">
  <h2>Category</h2>
  <div class="thumb">
    <div><img src="assets/images/header1.jpg" />
      <h3>Click to open</h3>
    </div>
    <div><img src="assets/images/header2.jpg" />
      <h3>Click to open</h3>
    </div>
    <div><img src="assets/images/header3.jpg" />
      <h3>Click to open</h3>
    </div>
    <div><img src="assets/images/header4.jpeg" />
      <h3>Click to open</h3>
    </div>
  </div>
</section>

我正在尝试将css悬停应用于具有div类的div元素中的div元素。我想使悬停时文本的不透明度为1。但这是行不通的。为什么不起作用?请帮我解决这个问题。有人可以帮我解决这个问题吗?我的代码有什么错误吗?如果我有任何错误,请提供帮助。

1 个答案:

答案 0 :(得分:0)

我认为您应该更改为

    .thumb div h3:hover{
  opacity: 1;
}

因为要悬停图像和文本,所以悬停应该在“ h3”后面。变成了.thumb div h3:hover