Display <div> or <span> over image on :hover in HTML

JavascriptWeb DevelopmentFront End Scripts

In order to allow div or span element to appear over an image when mouse hover over the image, this can be done with the help of .image:hover overlay,

To position .overlay element absolutely relative to the parent element we give height and width to 100% for all image sizes making parent element inline-block


<div class="image">
   <img src="..." />
   <div class="overlay">Content to be displayed on hover</div>


By hiding the .overlay element by default, we use the selector .image:hover .overlay to change the styling on hover. Due to the HTML structure, this works well because .overlay is a descendant element.
.image {
.overlay {
.image:hover .overlay {

   /* All other styling - see example */
   img {
Updated on 30-Jan-2020 06:16:08