Home > other >  Displaying text on mouse hovering over image
Displaying text on mouse hovering over image

Time:09-21

I am trying to display a little copyright text on top of an image when the user hovers his mouse over it, using this example.

What am I missing that I do not see the text, only the fading effect?

<div class="list-group-item">
    <div class="row">
        <div class="col-12 col-md-4 vertical-center-col">
            <img class="img-thumbnail" src="media/images/EmployeeOne.jpg" alt="">
        </div>
        <div class="middle">
            <div class="text">Image Copyright</div>
        </div>
        <div class="col-12 col-md-8 mt-2 mt-md-0">
            <strong>EmployeeOne</strong>
        </div>
    </div>
</div>

This is my css file:

.card-img-top {
  min-height: 0.1px;
}

img.img-thumbnail {
  border: 1px solid #e4c9c9;
  width: 100%;
  height: auto;
  transition: 0.5s ease;
  backface-visibility: hidden;
}

.middle {
  transition: 0.5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

.text {
  background-color: #04aa6d;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}

.img-thumbnail:hover {
  opacity: 0.3;
}

.img-thumbnail:hover .middle {
  opacity: 1;
}

CodePudding user response:

Put your .middle div inside the same container as your image, then select it on hover by using adjacent sibling CSS selector

.card-img-top {
  min-height: 0.1px;
}

img.img-thumbnail {
  border: 1px solid #e4c9c9;
  width: 100%;
  height: auto;
  transition: 0.5s ease;
  backface-visibility: hidden;
}

.middle {
  transition: 0.5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
  z-index: 1;
  pointer-events:none;
}

.text {
  background-color: #04aa6d;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}

.img-thumbnail:hover {
  opacity: 0.3;
}

.img-thumbnail:hover .middle {
  opacity: 1;
}
<div class="list-group-item">
  <div class="row">
    <div class="col-12 col-md-4 vertical-center-col">
      <img class="img-thumbnail" src="https://www.gardendesign.com/pictures/images/675x529Max/site_3/helianthus-yellow-flower-pixabay_11863.jpg" alt="">
      <div class="middle">
        <div class="text">Image Copyright</div>
      </div>
    </div>
    <div class="col-12 col-md-8 mt-2 mt-md-0">
      <strong>Employee name</strong>
    </div>
  </div>
</div>

  • Related