5

I have an simple image like this:

<img src="/assets/missing.png">

Now what I want is to be able to hover over the image and have a transparent black overlay appear with a big 'x' on it. Is it possible with CSS3 only or do I need Javascript? And if so how?


I sort of got it to work, but i have an issue that i can't seem to fix. Here's a screenshot:

Screenshot

As you can see there is a part on top missing. Here is my css:

.image {
  padding: 0px;
  display: inline-block;
  vertical-align: middle;
  max-height: 150px;
  max-width: 150px;
  margin: 15px;
  position:relative;
}

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

.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 0.5s;
  -webkit-transition: all 0.5s;
}

.image:hover:after {
  opacity:1;
}
NickEckhart
  • 458
  • 2
  • 8
  • 21
  • 2
    This is very possible with CSS :) Here is a duplicate question - [Black transparent overlay on image hover with only CSS?](http://stackoverflow.com/q/18322548/2930477) – misterManSam Oct 21 '14 at 07:23
  • 1
    @misterManSam Thanks it worked more or less but i still have an issue (check question edits). Any ideas where the problem is? – NickEckhart Oct 21 '14 at 08:11
  • Make sure that the max height and width match the image dimensions. That said, [I have optimised this version](http://jsfiddle.net/0b6tau0r/) by combining some answers. How does that work for you when you change the max image width and height for your image? – misterManSam Oct 21 '14 at 08:18
  • @misterManSam I figured it out, was just a padding issue. Thanks for the help :) Cheers – NickEckhart Oct 21 '14 at 08:22
  • No worries, just for the heck of it, [this version does not require a height and width](http://jsfiddle.net/0b6tau0r/1/). Sitenote: Please only accept an answer that was helpful to you... you don't need to accept any answer - especially if you received most guidance through that other question :) – misterManSam Oct 21 '14 at 08:25

2 Answers2

7

Take a look at following fiddle. I hope it can help. http://jsfiddle.net/somy_taheri/93y6hwjk/1/

<html>
    <div class="outer">
        <img src="http://placekitten.com/g/200/300">                    
            <div class="overlay">
                <p class="text">x</p>
            </div>
    </div>
</html>
<style>
.outer {
    position:relative;
    width: 200px;
    height: 300px;
}
.overlay {                      
    display: none;  
}

.outer:hover .overlay { 
    display: block;
    position: absolute;
    width: 100%;
    height: 100%;
    background: black;
    opacity: 0.7;
    top: 0;
}
.text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform:translate(-50%, -50%);
    color:white;
}
</style> 
Somi
  • 86
  • 2
0

Take a look at this.

HTML :

<div class="div">
    <div class='overlay'><p>X</p></div>
    <img src="http://fc02.deviantart.net/fs71/f/2012/157/c/a/watch_dogs___wallpaper__2_by_danielskrzypon-d52hvi1.png"/>
</div>

CSS:

.div{
    height:400px;
    width:400px;
    float:left;
    cursor:pointer;
}
img{
    height:400px;
    width:400px;
}
.overlay{
    height:400px;
    width:400px;
    position:absolute;
    background:rgba(0,153,255,.8);
    text-align:center;
    display:none;
}
.overlay p{
    color:white;
    line-height:400px;
    font-size:80px;
    margin:0;
}

Query:

$(document).ready(function(){
    $(".div").stop().mouseenter(function(){
        $(".overlay").fadeIn('slow');
    });
    $(".div").stop().mouseleave(function(){
        $(".overlay").fadeOut('slow');
    });
})

Take a look. JSFiddle : http://jsfiddle.net/u5ka7Lq4/19/

Faisal Ahmed
  • 91
  • 1
  • 1
  • 5