Black transparent overlay on image hover with only CSS?

I’m trying to add a transparent black overlay to an image whenever the mouse is hovering over the image with only CSS. Is this possible? I tried this:

http://jsfiddle.net/Zf5am/565/

But I can’t get the div to show up.

<div class="image">
  <img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
  <div class="overlay" />
</div>
.image {
  position: relative;
  border: 1px solid black;
  width: 200px;
  height: 200px;
}
.image img {
  max-width: 100%;
  max-height: 100%;
}
.overlay {
  position: absolute;
  top: 0;
  left: 0;
  display: none;
  background-color: red;
z-index: 200;
}
.overlay:hover {
  display: block;
}

solution

I’d suggest using a pseudo element in place of the overlay element. Because pseudo elements can’t be added on enclosed img elements, you would still need to wrap the img element though.

LIVE EXAMPLE HEREEXAMPLE WITH TEXT

<div class="image">
  <img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

As for the CSS, set optional dimensions on the .image element, and relatively position it. If you are aiming for a responsive image, just omit the dimensions and this will still work (example). It’s just worth noting that the dimensions must be on the parent element as opposed to the img element itself, see.

.image {
  position: relative;
  width: 400px;
  height: 400px;
}

Give the child img element a width of 100% of the parent and add vertical-align:top to fix the default baseline alignment issues.

.image img {
  width: 100%;
  vertical-align: top;
}

As for the pseudo element, set a content value and absolutely position it relative to the .image element. A width/height of 100% will ensure that this works with varying img dimensions. If you want to transition the element, set an opacity of 0 and add the transition properties/values.

.image:after {
  content: '\A';
  position: absolute;
  width: 100%; height:100%;
  top:0; left:0;
  background:rgba(0,0,0,0.6);
  opacity: 0;
  transition: all 1s;
  -webkit-transition: all 1s;
}

Use an opacity of 1 when hovering over the pseudo element in order to facilitate the transition:

.image:hover:after {
  opacity: 1;
}

END RESULT HERE


If you want to add text on hover:

For the simplest approach, just add the text as the pseudo element’s content value:

EXAMPLE HERE

.image:after {
  content: 'Here is some text..';
  color: #fff;
  /* Other styling.. */
}

That should work in most instances; however, if you have more than one img element, you might not want the same text to appear on hover. You could therefore set the text in a data-* attribute and therefore have unique text for every img element.

EXAMPLE HERE

.image:after {
  content: attr(data-content);
  color: #fff;
}

With a content value of attr(data-content), the pseudo element adds the text from the .image element’s data-content attribute:

<div data-content="Text added on hover" class="image">
<img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

You can add some styling and do something like this:

EXAMPLE HERE

In the above example, the :after pseudo element serves as the black overlay, while the :before pseudo element is the caption/text. Since the elements are independent of each other, you can use separate styling for more optimal positioning.

.image:after, .image:before {
  position: absolute;
  opacity: 0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
}
.image:after {
  content: '\A';
  width: 100%; height:100%;
  top: 0; left:0;
  background:rgba(0,0,0,0.6);
}
.image:before {
  content: attr(data-content);
  width: 100%;
  color: #fff;
  z-index: 1;
  bottom: 0;
  padding: 4px 10px;
  text-align: center;
  background: #f00;
  box-sizing: border-box;
  -moz-box-sizing:border-box;
}
.image:hover:after, .image:hover:before {
  opacity: 1;
}