Sub topic 12
Supporting dark mode
This chapter assumes you can write table-based layouts, inline CSS, and an embedded style block with media queries, as covered in earlier chapters. It also assumes you know that many email clients strip or ignore parts of a style block.
How clients handle dark mode
Email clients do not agree on dark mode. They take one of three approaches:
- No change. The client shows your light design as written.
- Partial inversion. The client changes only colors that are very light or very dark, and leaves mid-tone colors alone.
- Full inversion. The client rewrites nearly every color, including ones you set deliberately, and ignores your dark-mode CSS.
Because of this, you can't fully control the result. Your goal is a template that stays readable under all three approaches.
Declaring and overriding colors
Two meta tags and a CSS property tell supporting clients that your template was designed for both schemes. A media query then overrides your inline light colors.
<meta name='color-scheme' content='light dark'>
<meta name='supported-color-schemes' content='light dark'>
<style>
:root { color-scheme: light dark; }
@media (prefers-color-scheme: dark) {
.bg { background-color: #121212 !important; }
.text { color: #f1f1f1 !important; }
}
</style>Your colors are set inline, so the !important flag is required. Without it, the inline value wins over the style block. Add the bg and text classes to the elements that carry those colors, next to their inline styles.
Apple Mail honors prefers-color-scheme. Some other clients ignore it and apply their own inversion instead. Outlook.com uses its own attribute selectors, [data-ogsc] for text color and [data-ogsb] for background color. You can target these as extra rules, but test them, because client behavior changes between versions.
Design choices that survive inversion
These choices help in every client, including those that ignore your CSS:
- Avoid pure white and pure black. Use something like
#f4f4f4and#222222. Mid-range colors are less likely to be flipped in a way that breaks contrast. - Give logos a safe edge. A dark logo on a transparent PNG can vanish on a dark background. Add a thin light outline or glow around the artwork, or use a logo with a mid-tone color that reads on both.
- Don't bake text into images. Text in an image can't be recolored and may become unreadable. Use live text where you can.
- Keep contrast high in both schemes. Check your light and dark color pairs against WCAG contrast guidance, which asks for at least 4.5:1 for body text.
- Set the background on the cell, not only the page. Put
bgcolorand abackground-colorstyle on your content cells. Clients that invert colors then act on a known value.
A common pattern is to swap an image for dark mode by using two images and showing one of them. This relies on prefers-color-scheme and display: none, so it only works in clients that support the media query. Treat it as an enhancement, and make sure the default image is acceptable by itself.
Mistakes to avoid
- Relying on the dark media query alone. Clients with full inversion never read it.
- Forgetting
!importanton overrides of inline styles. - Testing only in Apple Mail, which is the friendliest client for dark mode.
- Using a transparent logo with no contrast fallback.
Exercise
Your template sets text color inline as color:#222222. You add a dark-mode media query with .text { color: #f1f1f1 } but the text stays dark in Apple Mail's dark mode. What is the most likely cause, and how do you fix it?
Show answer
The inline style has higher priority than a rule in the style block. Add !important to the declaration, as in .text { color: #f1f1f1 !important; }, and make sure the element has the text class.