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.
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.
- 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. - 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.
- 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.
You write components or a template language, not hand-rolled tables, in most teams.
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.
<!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. langanddirtell screen readers which language and direction to use. Some webmail clients drop thehtmlelement, so many teams repeatlangon the outer wrapper too.x-apple-disable-message-reformattingstops 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 getsthcells.- 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 ignoresmax-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.
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>
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
widthandheightattributes on tables and images as well as CSS, since Word reads the attributes. - Put padding on table cells (
td), not ondivorp, which Word handles inconsistently. Resetpand 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.
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
divat 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 imageswidth,heightanddisplay: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 sayrole="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:
- No change. The email shows as designed.
- Partial inversion. The client darkens light backgrounds and lightens dark text, but leaves colours it judges already dark.
- 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.
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/alternativewith 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-Unsubscribeheaders. 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 each email client to how it treats your markup.
Put the steps for shipping a new email template in order.
- Tap the steps below in the right order.
- 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.