Skip to content

Lesson 48 of 52Beyond the screen

HTML Email: Building Emails That Render Everywhere

Build email markup that survives Gmail, Apple Mail and classic Outlook: table layouts, inline styles, bulletproof buttons, dark mode and testing.

22 min read0/4 questions

Your team ships a new order receipt email. It looks perfect in the browser preview: a flexbox header, a two-column summary, a rounded button. A week later the support inbox fills with screenshots. In classic Outlook on Windows the columns are stacked and stretched across a 27-inch monitor, and the button is a plain blue link. In Gmail the bottom of the email is replaced by "[Message clipped] View entire message", and the footer links are gone. Email is the one place where the HTML you learned in this course meets rendering engines from twenty years ago and sanitisers that rewrite your code. This lesson is about building the markup itself. Sending it from a server (SMTP, providers, retries, bounces) is covered in Sending Email from a Backend in the Python Backend course.

Why email is different

A web page runs in a browser you can test, and the browser engines largely agree. An email is opened in whatever client the reader uses, and each client does three things to your HTML before showing it.

  1. Sanitises it. Every client removes JavaScript, forms (mostly), iframes and anything it considers risky. Webmail clients such as Gmail and Outlook.com also rewrite your CSS so it cannot leak into their own interface: class names get prefixes, unsupported properties are dropped, and some clients remove a whole <style> block if they cannot parse part of it.
  2. Renders it with its own engine. Apple Mail and iOS Mail use WebKit and support almost everything. Gmail and Outlook.com render inside a web page, limited by their sanitisers. Classic Outlook for Windows renders HTML with the Microsoft Word engine. The new Outlook for Windows is web-based, like Outlook.com, but organisations still run classic Outlook in large numbers, so you design for both.
  3. Changes it for the reader. Images may be blocked until the reader clicks "show images", dark mode may invert your colours, and mobile apps may rescale text.

That is why email developers still write table layouts and inline styles in 2026. Not out of nostalgia: tables are the one layout tool every engine, including Word, understands. Flexbox and grid work in Apple Mail and partly elsewhere, but classic Outlook ignores them, so a layout that depends on them collapses.

Gmail adds one more hard limit. If the HTML of a message is larger than about 102 KB, Gmail cuts it off and shows a "View entire message" link. Anything after the cut, often the unsubscribe link, the legal footer and the open-tracking pixel, is hidden. Bloated HTML from drag-and-drop builders and long inline style attributes is the usual cause, so keep an eye on the size of the compiled output.

What happens to your email markup
Your templateBuild stepMail serverClient sanitiserRendererMJML or React Emailinlined HTML + plain textmessage openedwhat is leftdark mode, scaling

You write components or a template language, not hand-rolled tables, in most teams.

1/5

The skeleton

Every reliable email starts from the same frame. Read it top to bottom once, then use the runnable receipt below to see it working.

receipt.html
<!doctype html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="x-apple-disable-message-reformatting">
  <meta name="color-scheme" content="light dark">
  <meta name="supported-color-schemes" content="light dark">
  <title>Your receipt for order 2048</title>
  <style>
    @media (max-width: 480px) {
      .stack { display: block !important; width: 100% !important; }
    }
  </style>
</head>
<body style="margin:0; padding:0; background:#f2f3f5;">
  <div style="display:none; max-height:0; overflow:hidden;">
    156.00 EUR paid. Your order ships in 2 days.
  </div>
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td align="center" style="padding:24px 8px;">
        <!--[if mso]><table role="presentation" width="600" align="center"><tr><td><![endif]-->
        <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"
               style="max-width:600px; background:#ffffff;">
          <!-- rows of content -->
        </table>
        <!--[if mso]></td></tr></table><![endif]-->
      </td>
    </tr>
  </table>
</body>
</html>

What each piece is for:

  • A doctype. Without one, clients that render in a browser drop into quirks mode and spacing changes. Older templates use the XHTML 1.0 Transitional doctype; <!doctype html> is fine today. What matters is having one.
  • lang and dir tell screen readers which language and direction to use. Some webmail clients drop the html element, so many teams repeat lang on the outer wrapper too.
  • x-apple-disable-message-reformatting stops Apple Mail from resizing your text on its own.
  • The outer table is 100% wide and paints the background. The inner table is the 600px content column. About 600px has been the standard email width for years: it fits a desktop reading pane and scales down on phones.
  • role="presentation" on every layout table. Without it, screen readers announce "table, 3 rows, 1 column" for each one, which is exactly the problem the Tables lesson warned about with layout tables. A real data table, such as the line items of a receipt, keeps its table semantics and gets th cells.
  • The <!--[if mso]> comments are conditional comments. Only Microsoft Office's engine reads what is inside them; every other client sees an ordinary HTML comment. Classic Outlook ignores max-width, so without this "ghost table" of fixed width 600, the content column would stretch across the whole window. That is the 27-inch monitor in the support ticket.
Predict the outputHTML

Classic Outlook for Windows opens this email full screen on a 1920px wide monitor. There are no conditional comments. Roughly how wide is the white content column?

<table role="presentation" width="100%" style="max-width:600px; background:#ffffff;">
<tr><td>Thanks for your order...</td></tr>
</table>
Need a nudge? Hints walk you through it.

Inline styles, with a style block as a bonus

Because some clients and contexts drop <style> blocks, the safe default is to put every style that matters in style attributes on the element. Nobody writes that by hand. You write normal CSS, and a build step called an inliner (Juice is the common library, and MJML, React Email and Maizzle do it for you) copies each rule into the matching elements.

Keep a <style> block in the head for things that cannot be inlined: media queries for phones, dark mode rules and hover states. Treat them as progressive enhancement. The email must still read correctly if the block is removed, and the !important in the media query is there because it has to beat the inline styles.

A few rules that save hours:

  • Use the HTML width and height attributes on tables and images as well as CSS, since Word reads the attributes.
  • Put padding on table cells (td), not on div or p, which Word handles inconsistently. Reset p and heading margins inline (margin:0 0 16px).
  • Stick to web-safe font stacks (Arial, Helvetica, sans-serif). Custom web fonts load in Apple Mail and a few others and fall back everywhere else, so pick a stack that looks right without them.
  • Write colours as six-digit hex. Shorthand and newer colour functions are not safe everywhere.
  • Check a support table such as caniemail.com before using any property you are unsure of. It is the email equivalent of caniuse.

Bulletproof buttons

A button made from an image disappears when images are blocked. A <button> element does nothing in email. A "bulletproof" button is a real link inside a table cell that has the background colour:

<table role="presentation" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td bgcolor="#1a56db" style="border-radius:6px;">
      <a href="https://shop.example.com/orders/2048"
         style="display:inline-block; padding:12px 24px; font-family:Arial, Helvetica, sans-serif;
                font-size:16px; line-height:20px; font-weight:bold; color:#ffffff; text-decoration:none;">
        View your order
      </a>
    </td>
  </tr>
</table>

The cell's bgcolor gives classic Outlook a coloured block even though Word ignores padding on links. In Outlook the clickable area is only the text, while everywhere else the padded link fills the whole button. Rounded corners are lost in Word; if a design truly needs them there, teams add VML (Microsoft's old vector markup) inside conditional comments, and tools like MJML generate it for you. Use absolute https:// URLs for every link and image, because there is no base URL in an inbox.

A receipt you can test at phone width

The preview below holds the email in an inner frame so you can switch its width between 600px and 375px, like a phone. The email itself lives in the <template>. Edit it and watch both widths.

HTML & CSS
<p> <button id="w600">600px (desktop)</button> <button id="w375">375px (phone)</button></p><iframe id="inbox" title="Email preview" style="width:600px; max-width:100%; height:540px; border:1px solid #bbb;"></iframe> <template id="email"><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><style> body { margin: 0; } @media (max-width: 480px) { .stack { display: block !important; width: 100% !important; } .pad { padding-left: 16px !important; padding-right: 16px !important; } }</style><div style="display:none; max-height:0; overflow:hidden;">156.00 EUR paid. Your order ships in 2 days.</div><table role="presentation" lang="en" width="100%" cellpadding="0" cellspacing="0" border="0" style="background:#f2f3f5;"> <tr> <td align="center" style="padding:24px 8px;"> <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:600px; background:#ffffff;"> <tr> <td class="pad" style="padding:24px 32px 8px; font-family:Arial, Helvetica, sans-serif;"> <h1 style="margin:0 0 8px; font-size:24px; line-height:30px; color:#111111;">Thanks for your order</h1> <p style="margin:0; font-size:16px; line-height:24px; color:#333333;">Order 2048, paid on 22 October.</p> </td> </tr> <tr> <td class="pad" style="padding:16px 32px; font-family:Arial, Helvetica, sans-serif;"> <table width="100%" cellpadding="0" cellspacing="0" border="0" style="font-size:16px; line-height:24px; color:#333333;"> <caption style="text-align:left; font-weight:bold; padding-bottom:8px; color:#111111;">Items</caption> <tr><th scope="col" align="left" style="border-bottom:1px solid #dddddd;">Item</th><th scope="col" align="right" style="border-bottom:1px solid #dddddd;">Price</th></tr> <tr><td style="padding:6px 0;">Desk lamp</td><td align="right">120.00</td></tr> <tr><td style="padding:6px 0;">Bulbs (3)</td><td align="right">36.00</td></tr> <tr><th scope="row" align="left" style="border-top:1px solid #dddddd;">Total</th><td align="right" style="border-top:1px solid #dddddd; font-weight:bold;">156.00 EUR</td></tr> </table> </td> </tr> <tr> <td class="pad" style="padding:8px 32px; font-family:Arial, Helvetica, sans-serif; font-size:15px; line-height:22px; color:#333333;"> <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td class="stack" width="50%" valign="top" style="padding:8px 0;"><strong>Ships to</strong><br>Ana Silva<br>Rua Augusta 10, Lisbon</td> <td class="stack" width="50%" valign="top" style="padding:8px 0;"><strong>Paid with</strong><br>Visa ending 4242</td> </tr> </table> </td> </tr> <tr> <td class="pad" style="padding:16px 32px 24px;"> <table role="presentation" cellpadding="0" cellspacing="0" border="0"> <tr> <td bgcolor="#1a56db" style="border-radius:6px;"> <a href="https://shop.example.com/orders/2048" style="display:inline-block; padding:12px 24px; font-family:Arial, Helvetica, sans-serif; font-size:16px; line-height:20px; font-weight:bold; color:#ffffff; text-decoration:none;">View your order</a> </td> </tr> </table> </td> </tr> <tr> <td class="pad" style="padding:16px 32px; font-family:Arial, Helvetica, sans-serif; font-size:13px; line-height:20px; color:#555555; border-top:1px solid #eeeeee;"> You are getting this email because you placed an order at Example Shop, 1 Market Street, Lisbon. </td> </tr> </table> </td> </tr></table></template> <script> const frame = document.getElementById("inbox"); frame.srcdoc = "<!doctype html>" + document.getElementById("email").innerHTML; document.getElementById("w600").onclick = () => { frame.style.width = "600px"; }; document.getElementById("w375").onclick = () => { frame.style.width = "375px"; };</script>

Preview

Switch to 375px. The "Ships to" and "Paid with" cells stack, because the media query turns the td cells into blocks, and the side padding shrinks. Now delete the whole <style> block, as a strict client might, and switch again. The email is cramped on the phone but still complete and readable. That is the bar: the style block improves it, the inline styles carry it. Finally, change the button's bgcolor to a light grey and see how quickly white text loses contrast.

Accessibility and content

Email accessibility uses the same rules as the web, under harsher conditions.

  • Hidden preheader text. The first text in the body becomes the preview line in the inbox list. Without a preheader, readers see "View in browser" or the alt text of your logo. The hidden div at the top sets it on purpose.
  • Real text, not text in images. An image of a sale banner is unreadable when images are blocked, cannot be resized, and is invisible to screen readers. Outlook desktop blocks external images by default in many setups. Put the message in live text and use images as decoration.
  • Alt text on every image, describing what the image says, and alt="" on purely decorative ones. Give images width, height and display:block (inline images leave a small gap under them in some clients), and use PNG or JPEG. SVG support in email is poor.
  • Semantic headings (h1, h2) inside table cells still work for screen reader navigation, as long as the layout tables say role="presentation".
  • Readable sizes. At least 14px for body text, 16px is better on phones, with contrast that meets the WCAG ratios from Accessibility basics. Link text should say where it goes ("View your order"), not "click here".

Dark mode

Clients handle dark mode in three different ways, and you cannot choose which one your reader gets:

  1. No change. The email shows as designed.
  2. Partial inversion. The client darkens light backgrounds and lightens dark text, but leaves colours it judges already dark.
  3. Full inversion. The client inverts almost every colour, including some you would rather keep.

The color-scheme meta tags tell clients that respect them that you have designed both themes. Clients such as Apple Mail then apply your own @media (prefers-color-scheme: dark) rules from the style block instead of inverting. Gmail's apps ignore those rules and apply their own changes. So: support the media query where it works, and design the light version so it survives an automatic inversion.

The classic failure is a logo: a transparent PNG with dark lettering becomes dark-on-dark when the background is inverted. Give the logo a solid background of its own, or a thin light outline around the dark shapes. Test both themes on a real phone before shipping.

Quick check
A marketing email puts the whole sale message in one large image with alt text 'banner'. Which problem does this NOT cause?
Need a nudge? A hint rules out a wrong answer.

Tooling and testing

Hand-writing nested tables does not scale across a team. Most teams use a framework that compiles friendlier code into email-safe HTML:

  • MJML: a markup language (mj-section, mj-column, mj-button) that compiles to tables, conditional comments and VML. A safe default.
  • React Email or jsx-email: write emails as React components, render them to HTML strings in your backend or build. A good fit if your team already writes React.
  • Maizzle: Tailwind-style utility classes, then inlined and cleaned for email.

Whichever you pick, test in real clients before a template goes live. Services such as Litmus and Email on Acid render your email in dozens of clients and show screenshots, including classic Outlook and dark modes. For local development, catch outgoing mail in a fake inbox like Mailpit, as Sending Email from a Backend shows. Then send a final test to real Gmail, Outlook and Apple Mail accounts. Snapshot the compiled HTML in your test suite so a change to a shared component that bloats every email past Gmail's clipping limit is caught in code review.

What the backend has to add

Some parts of a good email are not markup, but you should know they exist so you can ask for them:

  • A plain-text alternative. The message is sent as multipart/alternative with an HTML part and a text part. Some readers prefer text, some devices only show text, and a missing text part is a mild spam signal. Most email frameworks generate one from your template; read it, because generated text is often messy.
  • Unsubscribe. Marketing email needs a visible unsubscribe link, and the backend adds List-Unsubscribe headers. Since February 2024, Gmail and Yahoo require bulk senders (more than about 5,000 messages a day to Gmail) to support one-click unsubscribe as defined in RFC 8058, along with authenticated sending. Transactional mail like receipts does not need an unsubscribe link, but should say why the reader received it.
  • Avoiding spammy patterns. Image-only emails, link text that shows one domain while the link goes to another, URL shorteners and shouting subject lines all hurt delivery.
Match the pairs0/4 matched

Match each email client to how it treats your markup.

Choose an item, then its match on the other side.
Put it in order5 steps

Put the steps for shipping a new email template in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
layout table
A table used only to position content, marked role='presentation' so screen readers do not announce it as data.
inlining
A build step that copies CSS rules into style attributes on each element, because some clients drop style blocks.
conditional comment
An HTML comment starting with [if mso] whose contents only Microsoft Outlook's Word engine reads.
bulletproof button
A link inside a coloured table cell, so the button still shows without images and in Outlook.
preheader
Hidden text at the top of the body that clients show as the preview line in the inbox.
Gmail clipping
Gmail hides everything after roughly 102 KB of HTML behind a 'View entire message' link.
multipart/alternative
A MIME message carrying the same content as HTML and as plain text; the client shows the one it prefers.