Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Black transparent overlay on image hover with only CSS?

Tags:

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; } 
like image 570
RobVious Avatar asked Aug 19 '13 20:08

RobVious


People also ask

How do you add a dark overlay in CSS?

You add padding to stretch the dark overlay. So in your actual example you may have to add or decrease the padding of the element with the dark overlay. Adding padding changes both the dark background AND the original background image. Any other suggestions?

How do you add an overlay to an image in CSS?

In short, CSS overlay effects are achieved by using the following: background-image and background CSS properties to add image and linear-gradient overlay effect. position:absolute , top , bottom , right , left CSS properties to control the position of overlay image or text.


2 Answers

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 HERE -- EXAMPLE 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; } 
like image 85
Josh Crozier Avatar answered Sep 18 '22 22:09

Josh Crozier


CSS3 filter

Although this feature is only implemented in webkit, and it doesn't have browser compatibility, but It's worth taking a look at:

.image img {     max-width: 100%;     max-height: 100%;     -webkit-transition: .2s all; }  .image img:hover {     -webkit-filter: brightness(50%); } 

JSFiddle Demo

References

  • https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html
  • http://www.html5rocks.com/en/tutorials/filters/understanding-css/
  • https://developer.mozilla.org/en-US/docs/Web/CSS/filter
  • http://davidwalsh.name/css-filters
  • http://net.tutsplus.com/tutorials/html-css-techniques/say-hello-to-css3-filters/

Similar topics on SO

  • How to Decrease Image Brightness in CSS
  • Convert an image to grayscale in HTML/CSS
  • Defined Edges With CSS3 Filter Blur
like image 21
Hashem Qolami Avatar answered Sep 19 '22 22:09

Hashem Qolami