Run the code snippet so see an css answer:
.image-container {
padding: 5px;
border-radius: 5px;
border: 2px solid cornflowerblue;
display: inline-block;
}
.image-container .headline {
font-weight: bolder;
}
.image-container .headline ~ img {
display: none;
}
.image-container .headline:hover ~ img {
display: block;
}
<p>You can create this using css only
<br/>Here is an example:</p>
<div class="image-container">
<a class="headline">Your hover text here</a>
<img src="http://rmfr-colorado.org/userfiles/704/images/kittens.jpg" />
</div>
How does it work?
In the css.
.image-container .headline ~ img
This selects first these classes: the .image-container, then its .headline then the ~ is a sibling selector. So its selects all the <img>
tags that are next to the <a>
tag.
So this:
.image-container .headline ~ img {
display: none;
}
hides the image so its not displayed.
And this:
.image-container .headline:hover ~ img {
display: block;
}
Displays the image when you hover the image.
More fancy animation:
.image-container {
padding: 5px;
border-radius: 5px;
border: 2px solid cornflowerblue;
display: inline-block;
}
.image-container .headline {
font-weight: bolder;
position: absolute;
}
.image-container .headline ~ img {
position: relative;
top: 1em;
transition: transform 1s;
transform: scale(0);
}
.image-container .headline:hover ~ img {
transform: scale(1);
}
<p>You can create this using css only
<br/>Here is an example:</p>
<div class="image-container">
<a class="headline">Your hover text here</a>
<img src="http://rmfr-colorado.org/userfiles/704/images/kittens.jpg" />
</div>