derek gulbranson

5Aug/0511

How To Use CSS in HTML Emails

When I started working on HTML emails I tried Googling for CSS and HTML email and it seemed the theme of the articles was to warn designers not to use CSS in email because it is not widely supported by email clients. Instead, they recommend using the tag that was deprecated in 1999.

So I think these other articles are just outdated. There was a time when CSS and email would have been a naive combo. Today, I have a hard time imagining anyone well-versed in CSS recommending tags. Since I have been using CSS in HTML emails for a while now, I thought I would put together some more current information from my current practices. Perhaps others also need to create HTML emails, but cringe at the site of tags.

Why HTML?

First, it should be noted that there are those who question the value of receiving email as anything other than text-only in fixed-width Courier with lines 80 characters wide. I suspect their frustration comes not from the fact that someone else selected the font, but that the capabilities of HTML email are frequently abused by spammers or used inappropriately by legitimate opt-in content providers.

Despite HTML’s misuse in email, the presentation of information is strongly linked to the reader’s ability to absorb, comprehend, and react to it. HTML combined with CSS can be used to allow creative and effective presentation via email, while degrading gracefully on the various presentation technologies the email may encounter.

The enlightened will realize that concise, useful, enjoyable content (that means text) is why recipients are interested in your email. If your content isn’t interesting on its own, then your readers probably aren’t interested in receiving it. Text is not intrinsically boring. Use CSS to clarify and enhance. To use a metaphor, the cake is more important than the frosting. Use the frosting to enhance the cake. But make sure they get the cake, even if the frosting gets a little smooshed.

Is CSS Supported?

If HTML is supported, then yes. In the beginning (by that, I mean when HTML support first appeared email clients), CSS was a fairly new and sparsely supported. Today, most modern HTML-capable email clients1 use the HTML rendering engine provided by the client’s operating system, all of which support CSS to some degree. Mobile devices generally degrade HTML to something their display is capable of displaying. Blackberries, for instance, are able to make use of heading tags and lists, but disregard CSS information.

Keep in mind that some readers may block HTML and/or images from emails. Newer versions of Outlook and some web-based email clients allow users to block images and/or HTML in email as both can apparently be used to breach the security of Microsoft products. Always include a plain text multipart-alternative for those that can’t deal with HTML for whatever reason. (Outlook, btw, includes a Plain Text alternative version for every HTML email it sends. That’s great accept it creates the plain text version by computer-guessing based on the HTML version. It’s usually quite ugly, certainly no Markdown. There is no way to change it. Just FYI.)

Determining Factors

The way I use CSS in email is much different than how I use it for web sites. The differences in my current approach stem largely from two concepts:

  • The intrinsic differences between emails and web pages.
  • The problems raised by the web-based email client.

Emails are Not Web Pages

There are many ways in which emails are not web pages, ranging from technical aspects to user experience, but the following technical differences determine my current CSS approach.

Web pages are based on a server and accessed from a remote location. They exist as single components of a larger family of pages. They are hopefully revised over time to keep content fresh.

Emails are atomic. They live their lives as complete entities, detached from a set location and frequently changing location several times. Once they have been sent, you no longer have access to them for changes, but other people do.

A much wider range of users and technologies create and manipulate emails. Some research indicates the most common email client, Outlook, has around a 40% share, with web-based email clients having a 20-30% market share. This is drastically different than Internet Explorer’s near 90% share of among web browsers. Simplicity and broader testing are required if design stability is a priority.

About External Links in Emails

So, since emails are atomic, theoretically I feel the email should contain all the data needed to display properly. In practice however, while I feel CSS should always be embedded in the email rather than linked to a remote style-sheet (AOL Mail doesn’t allow external stylesheets, btw), I still prefer to use external links for images. You can also refernce images attached to the email, but that doesn’t always degrade well when the email is forwarded. And, if attached to the email, the images will show up as useless attachments for recipients using non-HTML-capable clients.

Broken Images = Bad

The decision to use externally linked images also means that if readers access the email while offline, they may see broken images. Broken images are a bad thing to associate with your brand. One possible work around is to use background images instead of the image tag. Background images will not show a broken image icon if they do not load, allowing for more graceful degradation.

The Web-based Email Problem

If you target only the stand-alone email clients, those that pass HTML rendering off to the a web browser or operating system, using CSS in email is pretty much the same as using it in a normal web page. It’s when you start to consider web-based email clients that the challenges of CSS in HTML email become more apparent.

Too Many Bodies

In web-based email, the application has to make some decisions about how to display the email. One problem it faces is what to do with information that is delivered outside of the tag. Since the web-based email client’s interface must send its own set of , and tags to the browser, sending the second set from the HTML email would yield invalid HTML.

Hey, Where’d Everything Go?

Another possible problem for web-based email clients and CSS is that since CSS can affect the entire page, allowing your CSS in the email raises the possibility that your CSS will be affecting their web-based interface. For instance, if you create a CSS rule to absolutely position all

elements, you could conceivably render your recipient’s email interface unusable. More likely you will end up just changing some colors and sizes. I’ve seen this happen with Excite’s web-mail.

So my strategy for CSS in HTML email is largely dictated by the needs of these web-based email clients. These clients are drastically diverse, numerous and changing. My approach is based on a lot of trial and error and testing.

The Approach: Inline CSS

Gasp. The horror. Inline CSS? Are you high? How is inline CSS any better than reverting to the tag hacks of yesteryear?

In case you’re new to CSS, CSS can be included via a few methods: via styles in a
...

Welcome to Excite Mail

... Excite Home ...

Word Permeates It’s Ugly HTML Tentacles Via Outlook

Some people associate CSS with the irradication of tables from HTML. Thanks to the incessant innovation from the folks at Microsoft regarding MSWord’s HTML abilities, there’s still a place for tables in the world of HTML email. Outlook provides users with the option of using Word as their email editor. Word has a strange, turn-of-the-century take on HTML and seems to barf on typical

layout structure. If this option is enabled, Word will cause any
tags forwarded or edited in Outlook to become fairly mangled. Generally this means using tags for layout structure instead of
’s as is typical with web-page CSS. Thankfully tags accept CSS just like anything else, and Word seems to find that much more palatable.

Outlook uses an undocumented, non-standard HTML attribute, nosend, when deciding whether to attach copies of the images or keep the external reference when forwarding emails. Including the attribute nosend=”1” will keep Outlook from attaching the image to the email when it’s forwarded, e.g. .

CSS Shortcuts

Be careful with inline CSS shortcuts. Web-based email client will disregard whole rules, so some aspect of the shortcut rule is not supported, none of it will be applied. For instance, you’ll probably want to use a separate background-color rule when you use a background shortcut rule with an image url. Gmail turns off the entire rule if it includes an image, so your entire background rule will be discarded including the color.

Further Random Tips

  • You can generally see what your email will look like in Eudora by using something like Firefox’s Web Developer Plugin to disable CSS. If you’re using tables and CSS, Eudora also ditches the tables so it won’t look exactly the same, but it will still give you a good idea.

And of course there are the many other special HTML rendering bugs and tweaks not specific to email that you should to know and watch our for. Hopefully this information gives you a better understanding of the email-specific things to watch out for. If you have questions or need help with an email feel free to post comments. And I'd love to hear about your current best practices too.

1 There’s a noted gap in my experience with Lotus Notes and AOL email. The research I’ve found shows Lotus Notes having ~10% share of email clients, and I have never had a chance to evaluate its HTML rendering capabilities. The only info I’ve found on Lotus Notes is Tom Granger’s Secrets of HTML Email. If anyone has any insight for me, please don’t hesitate to post a comment.

Update:
Here's another decent "article":http://www.campaignmonitor.com/blog/archives/2005/08/optimizing_css_1.html on the same topic. His approach seems pretty similar.

Filed under: Design, Email Leave a comment
Comments (11) Trackbacks (0)
  1. Hi!
    Nice article, but i have a question:
    I am using Outlook XP and i have a signature containing images. When i create the email i see everything is fine. I send it and the recipient will see the images. After he replied to mine, and i replied again to his email, outlook changes the path to the image in the source code from something like “cid:875401010@13092005-20BD” to an absolute file path on my pc.
    I use a relative url for the images and everything (images+html) is in the same folder.
    Does somebody have an idea why it is doing something or if there is asolution?
    Thanks in advance!

    Adrian

  2. Sorry:

    so the consequences are that the images aren’t displayed anymore.

  3. Adrian, have you tried using the nosend=”1″ attribute on the img tag? Although it’s undocumented, I believe the purpose of that attribute is to prevent what’s happening to your images. Aside from that, I think it would be more stable to use absolute URLs. Whenever I have to include images in an email, I always upload the images to a web server and use the absolute URL to the image on the server. This also precludes the need to include “attachments” with each outgoing email. Absolute URLs combined with the nosend attribute should make the images pretty stable.

    I’m assuming you are comfortable with editing the HTML of your signature. And just FYI, the HMTL for your signature can be found at C:/Documents & Settings/*your user id*/Application Data/Microsoft/Signatures.

  4. If you’re worried about your images not showing up in your html email, you should embed them as inline attachments with Content-IDs and reference them in the >img< tags with the cid: syntax.

    With all the privacy scares and security threats, more clients are showing up with external images blocked anyway, so the privilage of using image GETs to track your users is going to disappear soon. That being said, The remaining priority would be (like you said) to make sure that the presentation of the message doesn’t suffer. Embedding all related content is the only proper way to do that.

    Although tables are deprecated as far as the cutting edge coder is concerned, they are far from being dropped by the browser developers or the W3C for that matter.

    Some of us still remember the days when the font tag wasn’t even supported by all browsers… ok.. i’m going to stop now.. i’m starting to show my age. lol

  5. Unfortunatelly CSS in HTML emails are used to set tracking bugs. Thats why Google Mail cuts off all dirty stuff from it.

  6. How do you get CSS to work in Google mail?

  7. Use inline CSS. Only a very limited number of inline CSS works in Gmail, font colors and sizes mostly.

  8. After 100 good emails (file – new) with an image in the signature… I always have one bad signature email…

    File
    New
    =>
    and then I can see the image being reduced to 10px * 10px

    anyone?

    By The Way: After a reboot everything is back normal… for 99 times :-)

  9. Make sure you have an explicit width and height on your image element. Don’t let it auto.

  10. I can’t do that, I am working for a company as a webdesigner, and the signature image is not always the same… after some time we change our signature picture (depending on new marketing actions)

  11. A good article but it never helped me.
    How do you put a background image when google and hotmail strips the background image attributes from your inline css?
    Also it strips out the position:absolute.
    I used this to workaround the background issue.
    used a container div without any position attirbute but set it to the width and hieght of the background image. Then I put in a div at the end of the all the divs which contained the background image.(as an image not a background) and postioned it absolutely at top 0 and left 0. I set the z-index to -100.
    This is turning into a big story.
    Lets cut it short. I put in the heading with a margin 200. But that started giving weird problems. I did not give any postion attribute and the it did not work(obviously).
    All I am trying to say that there is not way to make a mailer that has a background image with divs.
    I had to use old school tables and slick up the image.
    There was a gradient under the text I had to change it to solid color. So that I could add text(otherwise it would not be html just an image, not wonder we some proffessional companies just send you 1 image as the mailer and use imagemaps to define the links.)
    I can send you the mailer if you want, I maybe be doing something wrong too.
    But I tried everything I knew. 3 days spent the last one spent on tables(nested tables to prevent them from breaking)
    BTW hotmail ever strips the border colour tags from inline css.


Leave a comment

(required)

No trackbacks yet.