Sub topic 12
Supporting dark mode
This chapter assumes you can write the table-based, inline-CSS layout from the earlier chapters. It also assumes you know what a media query is and that email clients differ in what CSS they support. Dark mode adds one more rendering difference on top of those.
What dark mode does to an email
Dark mode is a system or app setting that shows light interfaces with dark backgrounds and light text. Your email's colors are chosen by you, but the client may change them. Clients fall into three groups:
- No change. The client shows your colors as written. Your light design stays light on a dark interface. Some desktop and webmail clients behave this way.
- Partial inversion. The client changes only colors that look light. A near-white background becomes dark gray and dark text becomes light. Colors it judges to be already dark or saturated are left alone.
- Full inversion. The client forces nearly every background and text color toward the opposite, whatever you declared. Some Gmail and Outlook apps do this on certain platforms.
The groups matter because your template is not in control in groups 2 and 3. You can only prepare it to survive them, and you can opt in to full control where a client honors prefers-color-scheme. Exact behavior changes between client versions and platforms, so treat the groups as a model and confirm with real tests.
Declaring that you support both schemes
Start by telling the client your template was designed for both schemes. Two meta tags and one CSS property do this:
<meta name='color-scheme' content='light dark'>
<meta name='supported-color-schemes' content='light dark'>
<style>
:root { color-scheme: light dark; }
</style>The values mean "light is my default, dark is also acceptable." Clients that read these tags, such as Apple Mail, may skip their own automatic inversion and let your CSS decide. Clients that ignore them continue with their own rules.
A side effect to know about: color-scheme: light dark can also change the default colors of unstyled elements, such as form controls and the page background, to the dark scheme. Since you set backgrounds and text colors explicitly on table cells, this seldom matters. It can matter for any element you left unstyled, so check them.
Overriding colors with prefers-color-scheme
The media query prefers-color-scheme: dark matches when the reader's system is in dark mode. Because your regular colors are inline, the overrides need !important to win:
<style>
@media (prefers-color-scheme: dark) {
.bg { background-color: #121212 !important; }
.card { background-color: #1e1e1e !important; }
.text { color: #f1f1f1 !important; }
.muted { color: #b8b8b8 !important; }
a { color: #8ab4ff !important; }
}
</style>Then give your elements the matching classes while keeping the light inline values:
<body class='bg' style='margin:0;padding:0;background-color:#f4f4f4;'>
<table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td align='center' class='card' bgcolor='#ffffff' style='background-color:#ffffff;padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>
<span class='text'>Your order has shipped.</span>
</td>
</tr>
</table>
</body>Every color that needs a dark counterpart must have a class. A common mistake is to restyle the text and forget the cell background, which gives dark text on a dark background.
Support is the main limit. Apple Mail, iOS Mail, and some Outlook versions on Mac honor the media query. Gmail does not support prefers-color-scheme in its own rendering, so your overrides are ignored there and Gmail applies its own adjustment. Never make a message depend on the media query alone.
Outlook.com and its attribute selectors
Outlook.com and some Outlook apps rewrite colors by adding attributes to elements instead of using the media query. Text color changes are marked with data-ogsc and background changes with data-ogsb. You can target these attributes to choose the replacement colors yourself:
<style>
[data-ogsb] .card { background-color: #1e1e1e !important; }
[data-ogsc] .text { color: #f1f1f1 !important; }
[data-ogsc] a { color: #8ab4ff !important; }
</style>Keep these rules in a separate <style> block or separate rule list from the media query. If one selector in a comma-separated list is invalid to a client, the client may drop the whole rule. Splitting them keeps one unsupported selector from taking down the others.
Choosing colors that survive inversion
Because you cannot control every client, pick colors that invert gracefully.
- Avoid pure white and pure black. Using
#ffffffbackgrounds and#000000text invites the strongest inversion. Slightly off values such as#fafafaand#222222are often treated more gently, and look better in dark mode too. - Keep contrast high in both schemes. Check your dark palette against a contrast target of at least 4.5:1 for body text. Light gray text on dark gray fails easily.
- Brand colors need a dark variant. A deep navy heading is unreadable on a dark background. Define a lighter tint for dark mode.
- Do not rely on color alone. A red "error" and green "success" label should also have words or icons.
Buttons are a good example. A bulletproof button has a filled cell with white text. That pairing often survives inversion, because partial-inversion clients usually leave saturated fills alone. Test it anyway, since a client may still shift the fill and darken the label.
Images in dark mode
Images are not recolored by the client in most cases, so they cause the most visible problems.
Transparent logos
A dark logo saved as a transparent PNG disappears on a dark background. Options:
- Use a logo with a light version of its colors that works on both backgrounds.
- Add a thin white outline or glow in the image so it reads on dark.
- Place the logo on a solid colored panel that stays the same in both modes.
Swapping images
Where the media query is supported, you can ship two versions and show only one. The dark version starts hidden:
<style>
@media (prefers-color-scheme: dark) {
.light-img { display: none !important; }
.dark-img { display: block !important; max-height: none !important; overflow: visible !important; }
}
</style>
<img class='light-img' src='https://example.com/logo-dark-text.png' width='160' alt='Acme' style='display:block;border:0;'>
<!--[if !mso]><!-->
<img class='dark-img' src='https://example.com/logo-light-text.png' width='160' alt='Acme' style='display:none;max-height:0;overflow:hidden;border:0;'>
<!--<![endif]-->The <!--[if !mso]><!--> wrapper hides the dark image from desktop Outlook, which does not understand the media query and would otherwise show both. Clients that do not support the query show only the light image, so this is a safe fallback. Give both images the same alt text, since the hidden one is not announced in most clients but might be where display:none is ignored.
Blocked images
Dark mode and image blocking combine badly. If the logo is blocked, the reader sees alt text in whatever color your text rules give it. Set a color on the image or its parent cell so the alt text is readable in both schemes.
Preheaders, borders, and other details
- Hidden preheader text stays hidden in dark mode, and its color does not matter, but keep the color matched to the background so a client that ignores
display:nonedoes not show it. - Thin borders and dividers in light gray vanish on dark gray. Use a border color that you also override in the dark query.
- Background images are not inverted. Provide a
bgcolorfallback that works in both schemes, and make sure text over the image is readable either way. - Gmail's heuristics can invert a color you thought was "dark enough." Test, adjust the value slightly, and test again.
A practical order of work
- Build and finish the light version first, with inline colors and neutral off-white and off-black values.
- Add the two meta tags and
color-schemeproperty. - Add classes to every element with a background, text, border, or link color.
- Write the
prefers-color-schemeblock that overrides each class. - Add the
data-ogscanddata-ogsbrules for Outlook.com. - Fix images: logos, icons, swap pairs, and alt text colors.
- Test in light and dark in each target client, and revise the colors that failed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Dark text on dark background | Overrode text color but not the cell background | Add a class and dark rule for every background |
| Dark styles never apply in Gmail | Gmail ignores `prefers-color-scheme` | Choose colors that survive Gmail's own inversion |
| Both logo versions appear in Outlook | Desktop Outlook ignores the hidden-image styles | Wrap the dark image in `<!--[if !mso]><!-->` |
| Logo vanishes on dark | Dark transparent PNG | Use a light outline, a light variant, or a solid panel |
| Overrides have no effect | Inline style beats the rule | Add `!important` to each override |
Exercise
A template uses a white background and near-black text, both set inline. You add @media (prefers-color-scheme: dark) { .text { color: #f1f1f1 !important; } }. In Apple Mail dark mode the text is nearly invisible. What went wrong, and what is the fix?
Show answer
The text color was overridden but the background was not, so light text now sits on the white background. Add a class such as .card to the cell that sets the white background, and add a dark rule such as .card { background-color: #1e1e1e !important; } inside the same media query. Also add a rule for links and borders if they use light-mode colors.
Exercise
Why do you wrap a dark-mode image swap in <!--[if !mso]><!--> and give the hidden image display:none inline?
Show answer
Clients that do not support the media query would otherwise show both images. Inline display:none hides the dark image by default, and only a client that supports prefers-color-scheme reveals it with the !important override. Desktop Outlook ignores the media query and hidden-image styles, so the conditional comment removes the dark image from its HTML entirely, leaving only the light one.