With powerful features of CSS we can show content on mouseover using CSS and we don’t need javascript. Here I have provided two examples, first example contains no transition and second example has CSS transition effect while hovering. You can see these examples in demo here.
Here I am writing the quick idea behind this. The simple html is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!-- NO TRANSITION HOVER --> <div class="container"> <div id="hover-me"> <span>Hover Me</span> <div id="hover-content"> This content is visible only when you mouse hover the parent div and it has no transition. </div> </div> </div> <!-- TRANSITION & HOVER --> <div class="container"> <div id="transition-hover"> <span>Transition Hover</span> <div id="transition-hover-content"> This content is visible only when you mouse hover the parent div and it has transition effect. </div> </div> </div> |
Here we has used container
class as fixed height div
to maintain position of other content because hovering may force them to slightly shift from their position.
And CSS to show hidden content without transition:
1 2 3 4 5 6 7 | #hover-content { display:none; } #hover-me:hover #hover-content { display:block; } |
Or CSS to apply transition to show content on mouseover:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #transition-hover-content { opacity:0; -webkit-transition:.5s; -moz-transition:.5s; -o-transition:.5s; -ms-transition:.5s; transition:.5s; } #transition-hover:hover #transition-hover-content { opacity:1; } |
You can apply any style you wish in this way. The only thing to keep in mind is that the element you wish to affect is a child (or immediate sibling with the + selector) of the element you are hovering.
Brilliant solution to a common challenge. Thanks!