Brett Kessler Brett Kessler - 1 month ago 12
CSS Question

text disappears when mouse moved

When I move my mouse over the image the text does pop up, but when I move my mouse after that it disappears. I have no idea why that is happening, and it's really frustrating. Does anyone have any clue as to what might be happening here?



.hvr-bounce-to-right {
display: inline-block;
vertical-align: middle;
-webkit-transform: perspective(1px) translateZ(0);
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px transparent;
position: relative;
-webkit-transition-property: color;
transition-property: color;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
}
.imgWrap {
position: relative;
}
.imgDescription {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
color: #fff;
visibility: hidden;
opacity: 0;
}
.imgWrap:hover .imgDescription {
visibility: visible;
opacity: 1;
}
.hvr-bounce-to-right:before {
content: "";
position: absolute;
z-index: 1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #2D2D2D;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transform-origin: 0 50%;
transform-origin: 0 50%;
-webkit-transition-property: transform;
transition-property: transform;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
-webkit-transform: scaleX(1);
transform: scaleX(1);
-webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="cows.css" rel="stylesheet" type="text/css">
</head>

<body>
<h1>Hello</h1>
<div class="hvr-bounce-to-right">
<div class="imgWrap">
<p class="imgDescription">This is some hovertext</p>
<img class="cow" src="cow.jpg" alt="cow">
</div>
</div>
</body>

</html>





CSS Document



@charset "utf-8";

/* CSS Document */

.hvr-bounce-to-right {
display: inline-block;
vertical-align: middle;
-webkit-transform: perspective(1px) translateZ(0);
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px transparent;
position: relative;
-webkit-transition-property: color;
transition-property: color;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
}
.imgWrap {
position: relative;
}
.imgDescription {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
color: #fff;
visibility: hidden;
opacity: 0;
/*remove comment if you want a gradual transition between states
-webkit-transition: visibility opacity 0.2s;
*/
}
.imgWrap:hover .imgDescription {
visibility: visible;
opacity: 1;
}
.hvr-bounce-to-right:before {
content: "";
position: absolute;
z-index: 1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #2D2D2D;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transform-origin: 0 50%;
transform-origin: 0 50%;
-webkit-transition-property: transform;
transition-property: transform;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
-webkit-transform: scaleX(1);
transform: scaleX(1);
-webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}




Answer

The problem is your selector.

Instead of this:

.imgWrap:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}

Use this:

.hvr-bounce-to-right:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}

Code Snippet:

.hvr-bounce-to-right {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px transparent;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.imgWrap {
  position: relative;
}
.imgWrap img {
  vertical-align: middle;
}
.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  visibility: hidden;
  opacity: 0;
  /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */
}
.hvr-bounce-to-right:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}
.hvr-bounce-to-right:before {
  content: "";
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #2D2D2D;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  opacity: 0.1;
}
.hvr-bounce-to-right:hover,
.hvr-bounce-to-right:focus,
.hvr-bounce-to-right:active {
  color: white;
}
.hvr-bounce-to-right:hover:before,
.hvr-bounce-to-right:focus:before,
.hvr-bounce-to-right:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}
<h1>Hello</h1>
<div class="hvr-bounce-to-right">
  <div class="imgWrap">
    <p class="imgDescription">This is some hovertext</p>
    <img class="cow" src="http://www.placebacon.net/400/200?image=0" alt="cow">
  </div>
</div>


Explanation:

Because you are using a pseudoelement ::before as an overlay and giving it a value of 1 in its z-index property, it will cover the other elements in the stack context of your parent element .hvr-bounce-to-right.

You could see it this way. When you hover .hvr-bounce-to-right element, you can't get pass through the pseudoelement, so you are not really hovering .imgWrap, but the pseudoelement.

Since .hvr-bounce-to-right is the one being hovered, you can use that as your selector and solve the issue.


Notes:

  • To remove the unwanted space under the <img> element, use vertical-align: middle;.

This comment:

 /*remove comment if you want a gradual transition between states
  -webkit-transition: visibility opacity 0.2s;
  */

is wrong, you cannot use transition with properties like visibility which have a binary setting. It just switches between two states (visible/hidden), not between values like opacity, which can go from 0 to 1. The correct value there is:

transition: opacity 0.2s;