Reputation: 189
I am new to css and am trying to implement a functionality on my image which is - when someone hovers over the image with the mouse a 'Read More' button will be revealed. Here is the code I have so far -
HTML-
<div class="container">
<img src="./images/pairingo.png">
<div class="button"><a href="#">READ MORE</a></div>
</div>
CSS-
.container{
position: relative;
margin-top: 20px;
}
.container .button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:40%;
height:40%
}
img:hover .button{
opacity:1;
}
This doesnot seem to work. Maybe I am doing something wrong on the hover part? Any help will be appreciated thanks.
Upvotes: 1
Views: 385
Reputation: 7066
Here you go:
Set the
container
stop
andleft
property to make your button visible wherever you need.
.container{
position: relative;
margin-top: 20px;
}
.container .button {
position: absolute;
top: 10%;
left: 20%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:80px;
height:80px;
}
.container:hover .button{
opacity:1;
}
<div class="container">
<img src="http://placekitten.com/301/301">
<div class="button"><a href="#">READ MORE</a></div>
</div>
Upvotes: 1
Reputation: 713
You need to add hover css to .container class. Because img is child of .container class and .button is not child of img.
Try this.
.container{
position: relative;
margin-top: 20px;
width: 300px;
height: 300px;
}
.container .button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:100%;
}
.container:hover .button{
opacity:1;
}
<div class="container">
<img src="https://placehold.it/300x300/ccc/666?text=" class="img-responsive" alt="">
<div class="button"><a href="#">READ MORE</a></div>
</div>
Upvotes: 1