Skip to content

Instantly share code, notes, and snippets.

@jaredlt
Last active June 18, 2020 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredlt/27024b8a84bd9a16daa3c416c2bdfa9b to your computer and use it in GitHub Desktop.
Save jaredlt/27024b8a84bd9a16daa3c416c2bdfa9b to your computer and use it in GitHub Desktop.

Change src image on hover (without white flash)

The following CSS and HTML will change a src image on hover, without any white flash effect from the next image loading. It does this by loading both images, setting the position to relative so they sit on top of each other, and configuring the z-index to control which image is visible.

// CSS

.product-photo-hover {
  display:block;
  position:relative
}
.product-photo-hover > img {
  background:#fff
}
.product-photo-hover > img:not(:only-child):nth-child(1) { 
  position:absolute;
  z-index:3
}
.product-photo-hover:hover>img:nth-child(1){z-index:1}
.product-photo-hover>img:nth-child(2){position:relative;z-index:2}
<!-- HTML -->

<a class="product-photo-hover" title="" href="#">
   <img src="https://example.com/default-image.jpg" /> 
   <img src="https://example.com/hover-image.jpg" />
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment