Skip to content

Instantly share code, notes, and snippets.

@dotMastaz
Created November 10, 2016 09:35
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 dotMastaz/39b1dd214c33fe1d2b87d882223b4d32 to your computer and use it in GitHub Desktop.
Save dotMastaz/39b1dd214c33fe1d2b87d882223b4d32 to your computer and use it in GitHub Desktop.
Responsive Rollover Images for Email
<!DOCTYPE>
<html>
<head>
<style>
.rollover:hover > img,
* [summary=rollover]:hover > img{
max-height: 0px !important;
}
.rollover:hover > div img,
* [summary=rollover]:hover > div img{
max-height: none !important;
}
</style>
</head>
<body>
<a class="rollover" summary="rollover" href="http://store.nike.com/us/en_us/pd/flyknit-lunar-3-running-shoe/pid-10252949/pgid-1561757">
<img src="http://freshinbox.com/examples/image-swap/images/shoe-main2.jpg" width="500" style="display:block;width:100%;" alt="Nike FlyKnit 3" border=0>
<div style="mso-hide:all;">
<img src="http://freshinbox.com/examples/image-swap/images/shoe-alt2.jpg" style="max-height:0px;display:block;width:100%;" width="500" alt="" border=0>
</div>
</a>
</body>
</html>
.rollover:hover .main{
max-height: 0px !important;
}
.rollover:hover .alt{
max-height: none !important;
}
<!-- To hide the alternative image from Outlook, we wrap it with a div and use the mso-hide:all Microsoft Office style. -->
<a class="rollover" href="http://server/the-url">
<img class="main" src="http://server/main-image.jpg" width="500" style="display:block;width:100%;">
<div style="mso-hide:all;">
<img class="alt" src="http://server/alt-image.jpg" width="500" style="display:block;width:100%;">
</div>
</a>
<a class="rollover" href="http://server/the-url">
<img class="main" src="http://server/main-image.jpg" width="500" style="display:block;width:100%;">
<img class="alt" src="http://server/alt-image.jpg" width="500" style="max-height:0px;display:block;width:100%;">
</a>
/*
On mobile - specifically iOS - because the image is wrapped in a link, when a user taps on an image, both the hover and click even fires simultaneously. The rollover image will appear for a split second before the link opens in the browser.
Since this may be jarring to the user, we can disable the rollover state in mobile by using this media query
*/
@media screen and (max-device-width:1024px) {
.rollover:hover > img,
* [summary=rollover]:hover > img{
max-height: none !important;
}
.rollover:hover > div img,
* [summary=rollover]:hover > div img{
max-height: 0px !important;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment