3 Steps to Create an Interactive Email Using CSS
Update: Here’s a tool to create this example: Rollover Image Builder for Email.
Do you find yourself trying to find new ways to engage your email audience with compelling and eye-catching content?
This example will show you, in three easy steps, how to do just that. By creating an email that will display an alternate view upon the mouseover of an image, users will have a much more interactive and enjoyable experience.
1. Table + Background Image
The first step is to create a table containing a single cell with the alternate image as the cell’s background.
<table cellpadding=0 cellspacing=0 border=0><tr> <td width=240 background="http://.../alternate-image.jpg"> </td></tr></table>
2. Link + Image
The next step involves adding the primary image wrapped within a link. Add the class “img-swap” to the link and set the style “display:block;” to the image.
<table cellpadding=0 cellspacing=0 border=0><tr> <td width=240 background="http://../alternate-image.jpg"> <a class="img-swap" href="http://../link"><img src="http://../primary-image.jpg" width="240" height="170" border="0" style="display:block;" alt="the product"></a> </td></tr></table>
3. Style + Hover
The third and final step is adding styles to the link and setting the :hover pseudo-class on the link that targets the image within it. By setting the height of the image to 0px when the cursor is over the link, the image is hidden and the background image of the table cell is displayed. The link’s display style is set to “block” so that the link retains its width and height even when the enclosed image is collapsed allowing the user to click on it.
Update: This article covers a neat trick to get this to work in Gmail webmail.
<style> .img-swap { display: block; width: 240px; height: 170px; } .img-swap:hover img { height: 0px; } </style>
See how it works in your inbox by sending yourself an example.
Client support
The effects will be visible for users on Yahoo! Mail, Outlook.com (aka Hotmail), AOL as well as Outlook 2003. Clients that don’t support embedded style tags such as Gmail will just not respond to the hover event. Although the interactive effect does not work on Outlook 2007 and above, they still render perfectly, which is a huge plus! A coming article will focus on how to leverage the same techniques for mobile clients.
I’m interested to see how others are using the :hover pseudo-class or any other method to create interactive content in email. Let me know in the comments or on Twitter if this is something you find interesting or are considering trying out!
This is great! I am redeveloping our emails for our campervan brands and this is a perfect way to include both our vehicle clearcut and cutaway images which we use site wide for all brands.
Thank you so much!
That is awesome Niven. I’d be interested to know how it goes!
I tested in Gmail, Yahoo, Hotmail, AOL and the interaction doesn’t work in Gmail and Outlook 2010. Looks great in other desktop clients like Yahoo , Hotmail and AOL. Amazing tip, thanks for sharing!
Unfortunately as you probably know Gmail doesn’t support embedded styles and hence pseudoclasses like :hover, and Outlook 2007+ is in a totally different planet, for now these techniques won’t work with them.
You’re welcome!
As this is using the hover element, this won’t work on mobile devices, correct? Which is a shame as such a high percent of users now open emails on mobile devices. Sure this is more like a nice to have feature than something which will effect how the email is viewed. But I think currently I’m leaning more towards developments in emails which are mobile friendly.
Hi Jaina,
Hover elements actually work on mobile devices, but sometimes differently. The example on this page wouldn’t work on mobile…
BUT this will:
http://freshinbox.com/blog/image-rollovers-for-mobile-and-non-mobile-email/
And this:
http://freshinbox.com/blog/image-carousel-in-email-part-1-mobile/
It is probably not possible to make the hover work in an email signature, since you can’t put the styles in the head then, can you? Or am I missing something?
You should be able to make it work by having the styles in the body.
Thank you Justin for the hard work. Sent a test via the tool. Doesn’t seem to work for AOL.
Apparently AOL doesn’t like the > as in x > y in the CSS. I made a tweak to the CSS and it works now :)
Looks like this is no longer working in Yahoo Mail Desktop – are you getting the same results? If so, do you have any ideas on solutions?