Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a transparent overlay on image [duplicate]

Tags:

html

css

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;
}
like image 873
NickEckhart Avatar asked Mar 18 '23 03:03

NickEckhart


1 Answers

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> 
like image 53
Somi Avatar answered Mar 26 '23 02:03

Somi