Build responsive emails

Build responsive emails

  • Knowledge needed: HTML, CSS
  • Requires: Text editor, web browser, media query capable mobile device
  • Project time: 1 hour

Email is a neglected area when it comes to responsive design, but it doesn’t have to be. Elliot Ross walks you through making an HTML email mobile friendly

This article first appeared in issue 232 of .net magazine – the world's best-selling magazine for web designers and developers.

In this tutorial we’ll look at bringing some of the web’s mobile techniques to HTML email. User take-up of mobile email is massive: some email campaigns get more mobile opens than desktop, yet many marketing emails render poorly on mobile devices. Mobile support on the web has never been better – so if you’re sending emails that drive customers to the web, it makes sense to design mobile friendly emails too.

The design challenges for mobile email are similar to those on the web: we have a small screen, a touch interface and (sometimes) users are out and about – so the design approach is similar. We have one extra challenge, in that many older mobile email clients lack proper support for media queries.

With this in mind, our best approach is to design a campaign that works well on both desktops and mobiles, and then use media queries where they’re supported to add an extra layer of optimisation for smartphones. So first, let’s look at how we can make a regular email campaign more mobile friendly:

Make the message clear

If there’s one trend that happens time and again in the process of making an email campaign it’s this: marketing departments love writing tons of copy, and designers hate cramming it into the layout. In the past we’ve tried breaking up copy and pulling sections out as highlights, but mobile gives us the best argument yet for short copy: people don’t have time for lots of text, and we don’t have space. Even a user sitting on a train may still have lots of emails to get through. We need to cut the waffle, and make it easy and quick to read.

Keeping content simple and designing in a single column is an easy win to improve mobile support

It’s possible with CSS to remove some of your desktop content on a mobile device, but we should only be using this for tweaking design elements, not editing the message. If you can’t get your message across in a 320px wide device, it’s hard to see how adding content for desktop users will solve this.

Screen types and sizes

As with designing for the mobile web and for apps, we’re dealing with a touchscreen. It’s worth remembering the infamous finger measurement from Apple’s Human Interface Guidelines: the minimum size for a clickable area should be 22x44 pixels. You should make (at least) the main call to action links nice and large, and easy to select on a touchscreen. Also ensure links are spaced out so they aren’t mis-tapped: a 22-pixel minimum is again advisable.

The portrait iPhone screen size is 320 pixels wide; most desktop emails are 600px to 700px. We want something that works in both, so leaving responsive techniques aside this means reducing copy, then making the text size larger.

The customer journey

It’s important to get the email right, but we also need to consider where users go next. After clicking on from the email, is the landing page optimised for smaller screens? Can they perform the same call to action on a mobile device? That’s out of the scope of this tutorial, but it’s something to think about.

An easy way to make a desktop design that will also look OK on a mobile is to use a simple layout: keep everything in one column, and the content short. That gets you some of the way, and it’ll give us a decent backup where media queries aren’t supported (notably on Blackberrys and in the Android Gmail app). But let’s look at how we can use media queries to add better support.

As with all email code, there are retro things going on. If you’re used to super clean semantic web code, you may want to put your safety goggles on.

Campaign Monitor has extensive resources on responsive email code

Let’s get started

We’ll start off by setting up some of our styles and adding the viewport meta tag. As you may know, style declarations in the header are sometimes ignored by desktop email clients, and therefore you’ll see advice not to use them. We’re only using them to control our mobile formatting here, so we’re OK.

  1.   <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  2.     <style type="text/css">
  3.       @media screen and (max-width: 480px) {
  4.         table[class=emailwrapto100pc], img[class=emailwrapto100pc]
  5. {width:100% !important; height:auto !important;}
  6.       }
  7.     </style>
  8. </head>
  9. <body style="padding:0; margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:
  10. 100%; background-color:#ffffff;" bgcolor="#ffffff">

You can see we’re using slightly odd CSS. We have to use attribute selectors to specify our mobile CSS, otherwise Yahoo! Mail will apply the media queries on a desktop. This means we have to separately specify each element a class might apply to. If your style isn’t applied properly, this is usually the first place to look. We’ll give the classes slightly obscure names too: some of the obvious ones are used by webmail clients, and we don’t want style conflicts with them.

On the body tag we’ve set padding and margin at 0, otherwise we’ll get a white border around the email in iOS mail. We’ve disabled automatic text size adjust: some mobile email clients automatically bump up the text size to make it more readable, but it can break the design. We still need to make sure we set the font size appropriately ourselves (above 12pt; 14pt is good).

We’ve just got one style declaration at the moment, which we can use to override a specified width and make it 100% wide instead. The height:auto bit is important to ensure that images scale in proportion.

Let’s create a 640 pixel wide table to wrap our content in.

  1. <table width="640" border="0" cellspacing="0" cellpadding="0"
  2. class="emailwrapto100pc">
  3.   <tr>
  4.     <td>
  5.       <!-- our content will go in here -->
  6.       <img height="200" width="640" border="0" src="hero.jpg"
  7. style="display: block;" class="emailwrapto100pc" />
  8.       <span class="emailbodytext" style="font-size:12px; fontfamily:
  9. Arial, Helvetica, sans-serif; line-height:18px;">Lorem ipsum dolor sit
  10. amet, consectetur adipiscing elit.</span>
  11.     </td>
  12.   </tr>

We’ve put an image in there too. This will resize to 100% of our device width, and the height will adapt accordingly. On a portrait iPhone it’ll be 320x100px: as we’re effectively resizing the image by half, we’re making it Retina-ready.

Wherever we need to, we’ll nest tables inside this master table at 100% wide, so they’ll adapt on their own.

Text should wrap automatically, but we might want to bump up font size on mobiles, so let’s add some styles for that. <p> tags can inherit formatting in webmail clients, so we’ve used spans.

  1. span[class=emailbodytext],a[class=emailbodytext]{font-size:16px !important; line-height:21px !important;}

So we’ve managed to control text and image formatting when email’s displayed in a mobile client – let’s try something more challenging.

MailChimp has a great guide to the mobile email landscape

Floating columns

In our design we may have content that’s split into two columns; say two news articles. It would work better on a mobile if we stacked these into one long column. Just as we’d do on the web, we’ll float the columns with some CSS.

  1. td[class=emailcolsplit]{width:100%!important; float:left!important;}

With this code we’ve forced out the width to 100%, then floated the <td> that contains our content. It’s a lot easier to float a right hand column underneath a left hand one, but it is possible to do the reverse.

  1. <table border="0" cellspacing="0" cellpadding="0" width="640"
  2. class="emailwrapto100pc">
  3.   <tr>
  4.     <td class="emailcolsplit" align="left" valign="top" width="320"
  5. style="padding-left:10px;padding-right:10px;">
  6.       <img height="100" width="300" border="0" src="article1.jpg"
  7. style="display: block;" class="emailwrapto100pc" />
  8.       <span class="emailh2" style="font-size:14px; font-family:Arial,
  9. Helvetica, sans-serif;">Headline One</span><br />
  10.       <span class="emailbodytext" style="font-size:12px; font-family:Arial,
  11. Helvetica, sans-serif;">Lorem ipsum dolor sit amet, consectetur adipiscing
  12. elit.</span>
  13.     </td>
  14.     <td class="emailcolsplit" align="left" valign="top" width="320"
  15. style="padding-left:10px;padding-right:10px;">
  16.       <img height="100" width="300" border="0" src="article2.jpg"
  17. style="display: block;" class="emailwrapto100pc" />
  18.       <span class="emailh2" style="font-size:14px; font-family:Arial,
  19. Helvetica, sans-serif;">Headline Two</span><br />
  20.       <span class="emailbodytext" style="font-size:12px; font-family:Arial,
  21. Helvetica, sans-serif;">Lorem ipsum dolor sit amet, consectetur adipiscing
  22. elit.</span>
  23.     </td>
  24.   </tr>

We have two columns that now stack into one on a mobile device. If there’s a table cell between two columns you’d like to hide, you can: use display:none.

Hiding content

If you’ve designed with mobile in mind from the outset, you shouldn’t need to hide much content, but there may be bits of page furniture you’d like to remove. We can use display:none to do this; here’s the CSS:

  1. table[class=emailnomob],td[class=emailnomob],img[class=emailnomob] {display:none !important;}

It’s easy enough to hide desktop content on a mobile device; less so to do the reverse: display:none is ignored by Gmail on the web, for example, so mobile content will still show on some desktop platforms. I’ve heard you can add a 1-pixel transparent GIF, expand it with a media query and whack a background image behind it, but you wouldn’t admit to that in public, would you?

On an iPad the video in this newsletter from the Times plays directly from the email

Mobile friendly buttons

A nicer business is to make navigation links more friendly for mobile users. Many email campaigns have nav bars, which are hard to use on mobile devices but still important, so we should be careful about just taking them out. Instead, let’s change them into buttons:

  1. <table border="0" cellspacing="0" cellpadding="0" width="100%">
  2.   <tr>
  3.     <td align="center"><a href="#" class="emailmobbutton" style="fontsize:
  4. 14px; font-family:Arial, Helvetica, sans-serif;">Our Website</a>
  5. <span class="emailnomob"> | </span>
  6. <a href="#" class="emailmobbutton" style="font-size:14px; font-family:Arial,
  7. Helvetica, sans-serif;">Products</a>
  8. <span class="emailnomob"> | </span>
  9. <a href="#" class="emailmobbutton" style="font-size:14px; font-family:Arial,
  10. Helvetica, sans-serif;">Contact Us</a></td>
  11.   </tr>

And our CSS:

  1. a[class=emailmobbutton]{display:block !important;font-size:14px !important; font-weight:bold !important; padding:6px 4px 8px 4px !important; line-height: 18px !important; background:#dddddd !important; border-radius:5px !important; margin:10px auto;width:80%; text-align:center; color:#111; text-decoration: none; text-shadow:#fff 1px 0 0 ;}

We’re removing the pipes that separate the links, then applying CSS to turn them into big mobile friendly buttons. We’ve set the button at 80% wide, so it should centre align, but still expand for larger mobile screens. Applying the class to the anchor tag means we can expand the linked area, so the whole button links through. We’re using CSS3 to add the likes of border-radius and text-shadow; support is patchy in some email clients, but those that support media queries also support this, so we won’t run up against those issues here.

Tablets

It’s easy to assume desktop email will work fine on a tablet: most emails are designed 600-700 pixels wide, so that’ll fit on a tablet screen. But tablets have a touch interface, so there’s optimisation we can add if we have lots of users.

  1. @media screen and (min-width: 480px) and (max-width: 1024px) {
  2.   table[class=emailwrapto100pc], img[class=emailwrapto100pc]{width:100%
  3. !important; height:auto !important;}
  4.   span[class=emailbodytext],a[class=emailbodytext]{font-size:16px !important;
  5. line-height:21px !important;}
  6.   a[class=emailmobbutton]{display:block !important;font-size:14px !important;
  7. font-weight:bold !important; padding:6px 4px 8px 4px !important; lineheight:
  8. 18px !important; background:#dddddd !important; border-radius:5px
  9. !important; margin:10px auto;width:70%; text-align:center; color:#111; textdecoration:
  10. none; text-shadow:#fff 1px 0 0 ;}
  11. }
This email from British Airways displays a mobile-friendly design when opened on an iPhone

In this media query, we’re still resizing some of the content, and making our links into nice big buttons, but we’ve taken out the code to stack columns, because we have more room for that on a tablet.

So we’ve looked at some of the main techniques - most of the mobile design challenges can be solved by combining these until you get the result you’re after. But what other challenges do we have?

Some email sending platforms will automatically take style information from the header and make it inline. That’s useful in the most part, but here it can break the media queries. The good platforms will give you some control over whether that happens or not, but you might want to give their support a call if you’re having problems getting it through the tool. I’ve found that as a last resort, taking the style tag out of the header and putting it between the first <table> and <tr> tags can help get around this also (yes, I know…).

Testing

As usual when coding emails, it’s recommended you test your campaigns thoroughly in the various clients before you do your final send. Litmus and Email On Acid are invaluable for previewing your code in different email clients, and similar functionality is available through the likes of Campaign Monitor and MailChimp. Fractal will also help you write email compatible code, and then test it. Of course, nothing beats a suite of actual devices, if you can get hold of them.
 

Discover 10 simple but effect email newsletter designs at Creative Bloq.

23 comments

Comment: 1

Hi Elliott,

Thanks for sharing the details for creating a responsive email newsletter.

Can you tell me what email apps you tested this code in? I'm especially interested in support for Outlook. Other responsive email solutions I've found, for example, the Zurb templates, don't provide support for Outlook 2003, 2007, or 2010, which I need to support for all my clients..

Comment: 2

One service I found to be helpful was Litmus. As different version of Outlook interpret differently. Some using Word to render the code, while other versions use Internet Explorer. Definitely be careful with background images as they are problematic across different versions of Outlook. Hope this helps.

Comment: 3

Hey there

We tested this in all versions of Outlook (2000-2013 + .com), it's supported insofar as the 'desktop' version displays in Outlook, but you can't go resizing outlook windows and making it responsive. That said, I wrote this in the middle of last year and there's a couple improvements we could make to the code - responsive email is very new and theres's still stuff being ironed out!

I'm travelling today but I'll try to make some updates for the end of the week (or if I can't get any updates here they'll be on my twitter)

thanks, e

Comment: 4

+1 for both Litmus and Email on Acid (plus live email clients if you can) for testing your code, as with all email code, it can get quite fiddly so it's always best to check your final code as much as possible

Comment: 5

The concept of sending an email that isn't using table layouts is a gamble at the best of times but the vast majority of major email clients will strip your media query code out if it hasn't already been torn out by the mail exchange.

We try so hard to refine email marketing and wave the banner for massive results - but it doesn't it hasn't for years, it is a marketing communication tool that just refuses to die. It's like the end of Terminator only doesn't look cool and hasn't survived the test of time.

The other glaring issue here is that this practice in itself doesn't work with many of the email marketing platforms that are out there either so you could be spending hours if not days tweaking and faffing with this only to find what gets sent isn't close to resembling it.

Comment: 6

The responsive part of this design works perfectly on an iPhone which is the largest, most likely, demographic for making converstions online through a phone.

While this email renders fine in Gmail and Outlook* (the header stops a little short from the right, but no big deal, it's Outlook), the images do not show up at all when viewing gmail through my android (Galaxy S 2). I download the images and they never render, and the 2 column layout does not collapse.

Any idea what's problematic/missing that the gmail for android app does not like?

(Great article by the way).

Comment: 7

Hey Nate

thanks for taking a look. The outlook stuff should be ok, the idea is we get it to work like a normal desktop email there, so that might just be some padding or something causing that.

On the S2, I've since done some testing on this and found that bumping the min-width up on the media query can help for the native email client. Unfortunately, Gmail app for both Android and iPhone strips out the style tag and therefore our media query completely. There's not much we can do at the moment about that - it tends to display the 'desktop' version of the email instead, so the best thing is to make that mobile friendly too in terms of button size, copy size etc.

Comment: 8

I have found that calling an external stylesheet that's hosted on your server (much like the images would be) can sometimes help for people having problems with their media queries being stripped out.

I use click dimensions/Microsofts CRM to send out most emails at work and it would sometimes strip all media query styles or replace the media query with "@media unknown" until I used a tag. As far as Gmail on the android, I still haven't found a concrete way of dealing with it's rendering issues as far as media queries.

Comment: 9

Responsive design for email is like working in 1999 with techniques from 2012... it's a very strange and backwards process. But we only have the out of touch email clients to thank for that.

Comment: 10

Does it work cross platform, outlook or entourage? safari or IE?

Comment: 11

I don't think you should only play to retina screens on mobiles. If you're using jpegs you can make the files a lot smaller using JPEGmini.

Comment: 12

Also I change the layout when the screen width is < 580 which is a 600px - columns. Because anything less than 580 is going to mean half your email is hidden anyway.

Comment: 13

Becs - totally agree - the last 6 months or so I've been building to 640px wide, then having a media query < 640, as it's just a lot easier. That quite handily makes full width 640 images into retina images by default. I've been playing around with that new responsive image technique where you make massive images at low quality, it seems to work ok so far (but only for jpgs)

Russel - the best thing we can do in email is get a 'desktop' version to work for things like Outlook, Entourage + browsers, then when it's viewed on mobile devices we can start using media queries. So yes, it's supported on desktop in the same way any email is, but it's not responsive in that environment.

Comment: 14

I've also used the 1.5x trick. Making the images 1.5x bigger rather than twice as big (in conjunction with lossless compression) makes images look pretty sharp on retina devices and saves some filesize.

Comment: 15

Is there a way to swap images? So "Image A" appears in the desktop view, but for mobile it gets swapped with another image to "Image B"?

Would my CSS in the @media section look like this:

img[class="swap"] {
background-image: (ImageB.gif) !important;
}

And would the html look like this:

Thanks in advance!

Comment: 16

Somehow the html part didn't make it...here it goes again:

Comment: 17

One more time:
<img src="ImageA.gif" width="400" height="200" border="0" style="display; block" class="swap" />

Comment: 18

This is really cool.
Thanks for the tutorial and source code.

Comment: 19

Does anyone know if Thunderbird will honor Media Queries? I have to send my emails using Thunderbird for the HTML feature. I have tried to use Media Queries and when i test the emails do not "respond". I also tested the code in this tutorial but it still didn't work.

Comment: 20

I see that the layout does work in Thunderbird. However what is not working is column 2 floating below column 1 on iPhone 5.

I do not have anything else to test on but have tried in several different situation and the two columns appear side by side filling 00% of the width and showing uneven widths.

I tried applying clears and display: inline-block; but nothing really worked.

Thoughts?

Comment: 21

I took the code in the demo, copy/pasted into Constant Contact and tested by sending email to Litmus. The only handheld that shows a single column is the Blackberry 5OS. Android and iphone have the 2 column shrunken to fit the window. The reason I used the demo code exactly is that I used that as a base and made changes to accommodate my design. I wasn't seeing the single column on handheld so reverted to the source to see if that works. And it isn't. Any idea why that's happening? These are the test results: https://litmus.com/pub/7effc69

Comment: 22

Surely using php to detect a mobile browser and sending different html based on that is the only way to ensure consistency with Microsoft clients.

Comment: 23

This code worked perfectly for me, rendering brilliantly on all iPhones.

The only issue I came across was in Outlook.com on IE 9. Using the query above: @media screen and (max-width: 480px). Outlook rendered the mobile version, which look pretty terrible on a desktop.

However it's an easy fix, just change max width to max-device-width to resolve! Renders perfectly in Outlook.com now.
August issue on sale now!

The Week in Web Design

Sign up to our 'Week in Web Design' newsletter!

Hosting Directory
.net digital edition
Treat yourself to our geeky merchandise!
site stat collection