The Best Way to Code Background Colors for HTML Email

Ever get so frustrated with coding HTML email that you start seeing red? In the past couple weeks we’ve heard from a handful of red-eyed users reporting Gmail tests that are seeing black by showing up in inboxes with a black background (when that wasn’t your intended result!)

After digging into some testing, I realized that this issue isn’t limited to Gmail—in fact it affects a number of different clients depending on how you apply background colors in your code. As with anything HTML email related, this can be super frustrating since standard web code won’t always work the way you anticipate when sent to an email client.

Read on to get the scoop and find the one fail safe way to code background colors for consistent results across the board.

How to code a background color? Let me count the ways…

Usually, background colors are applied to a few common HTML elements: <body> <table> <td> <div> and <a>. Depending on your experience and coding style, you might use one of six (yes, six) different ways to apply background colors to elements in your email:

  • Using the HTML bgcolor attribute with a 3-digit hexadecimal color code
  • Using the HTML bgcolor attribute with a 6-digit hexadecimal color code
  • Using the HTML bgcolor attribute with an RGB color value
  • Using the background-color CSS property with a 3-digit hexadecimal color code
  • Using the background-color CSS property with a 6-digit hexadecimal color code
  • Using the background-color CSS property with an RGB color value

Since we’re talking about HTML email here, I’m going to skip over any discussion of the pros, cons or merits to each method. Suffice to say there are a lot of variables here!

Three digit HEX codes in email

I discovered right away that using 3-digit, or shorthand, HEX colors are not a great idea. When 3-digit HEX codes are used in <table> and <td> tags, black (or sometimes blue!) backgrounds result, with no regard to the color specified:

Black Background in Gmail

RGB values in email

RGB values didn’t fare much better, resulting in a few lovely shades of green (despite specifying values for white and gray):

In some cases, RGB values used with the HTML bgcolor attribute didn’t show up at all.

The best way to code a background color

After all the tests were completed, the verdict was in. The most reliable way of coding background colors is to use the HTML bgcolor attribute with a 6-digit HEX code:

<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”600″ bgcolor=”#ffffff”>

<td bgcolor=”#ffffff”>

Of course, there are dozens of factors involved. Depending on the specific breakdown of the mail clients your audience is using, CSS background-color with a 6-digit HEX code is very reliable as well. The HTML bgcolor attribute simply has the best support across the board. While some methods work consistently in certain elements, I’d personally prefer to stick to one background color strategy rather than trying to keep specific background color quirks straight. For simplicity, old school HTML and 6-digit HEX codes are where it’s at.

Testing methodology

I ran the tests used as the basis for this article using Firefox 10, Chrome 16 and Internet Explorer 9 (both PC and Mac where appropriate). I manually checked Gmail (both old and new UI versions), Hotmail, AOL and Yahoo, and used Litmus’ Email Previews to check rendering on desktop and mobile email clients.

Tools for understanding your audience and testing display quirks

You can run your own tests to check rendering and display in 30+ email clients in just a few minutes with Email Previews from Litmus. Before testing, be sure you know which clients your subscribers use most often by running Email Analytics on your next campaign.

Sign up for a free 7-day trial of Litmus today!

Subscribe to our newsletter

Get all our best stuff in your inbox, just a few times a month. We'll never share your email address and you can opt out at any time.

  • http://twitter.com/bsisolak Brian Sisolak

    I don’t know if this is still the case, but a while back noticed that Hotmail ignores border colors defined as RGB. Using the hex value was fine. To get better precision we have moved all borders into 1px wide columns and rows to avoid one pixel problems in Gmail/IE7. It’s been a few months since I tested that so maybe that has changed as well.

    • http://www.litmus.com Justine, Litmus

      I think you’re right, Brian. I also recall some issues in the past with RGB colors and borders. I’ll add it to the list of things to test and do a write-up on!

    • Will Egan

      Borders as columns are not a good idea. Instead, you should use alternate colours between the content area and the background area. If you look at Apple’s email, they use a grey #fcfcfc on the background and then a plain white in the main content area. Columns are easy to break and in some browsers the true ‘width’ (the width=”what you define here”) willnot be honoured. Particularly on large screens.

  • http://twitter.com/evert_ Evert Albers

    Glad that you did the testing for me.

    • http://www.litmus.com Justine, Litmus

      Thanks, Evert! Let us know if there are any other topics you’d like to see us cover.

  • http://twitter.com/john0514 John Cobb

    “When 3-digit HEX codes are used … black (or sometimes blue!) backgrounds result, with no regard to the color specified:”

    I just ran in to this problem (I wish I had found this article sooner!), and doing some inspecting, it looks like certain clients (older versions of outlook in particular) either render the 3 digit hex code with 0s for the missing 3 digits, eg. #FFF becomes #0F0F0F (not quite black) or #000FFF (blue) instead of #FFFFFF.

    • http://www.litmus.com Justine, Litmus

      Nice find, John! That certainly explains the wacky behavior. 

  • http://twitter.com/silverthan Thanos Karavasilis

    Did anyone have issues with background-colour ignored in the style of a text link?

    • http://www.litmus.com Justine, Litmus

      Hi Thanos, I’ve not heard of this before. I would be sure that you’re including the style tag inline within the a tag. Some email clients strip out external/embedded CSS.

  • http://twitter.com/ForYou_nl ForYou.nl

    For my website, I wanna do some email promotion too. But what is the best background colors? I mean, it always can be possible that your text color is not shown white I guess.. The site is http://boogytalk.net.

    Anyone some advice about the colors – or make sure the white text always keep white and not turns out to become black?