Sub topic 3
Inline CSS and what a style block can do
This chapter assumes you know basic HTML and CSS, the box model, and how media queries work. It also assumes you know that an email client is the app that receives and displays HTML mail. The previous chapters covered why email HTML differs from web HTML and how to structure a template with tables. Here you will decide where each CSS rule belongs: in a style attribute on the element, or in a <style> block in the head.
Why inline CSS is the default
On the web, you link a stylesheet and every browser applies it. Email clients do not give you that guarantee. Some clients remove <link> tags entirely. Some remove the <style> block, or support only part of it. Others rewrite or prefix your selectors.
The one place where almost every client keeps your CSS is the style attribute on an element. For that reason, the baseline rule for email is simple: put every rule your layout depends on inline.
<td style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;">
Thanks for your order.
</td>Treat the inline styles as the version of the email that every client can render. Anything else you add is an enhancement.
What a style block can do
A <style> block in the <head> is still useful. It is the only place you can write rules that inline styles cannot express:
- Media queries, such as
@media screen and (max-width: 600px). - The
prefers-color-schememedia query for dark mode. - Pseudo-classes such as
:hover. Support is uneven, so use hover only for polish. - Client-specific resets, for example for
a[x-apple-data-detectors].
A minimal block looks like this:
<style>
@media screen and (max-width: 600px) {
.stack { display: block !important; width: 100% !important; }
}
</style>The rule is that the style block is an addition on top of working inline styles. If the client drops the block, the email must still be readable. It may just look like the desktop layout shrunk down.
Client support for style blocks
Support has improved over the years, but you should still plan for gaps. These are the patterns to know:
| Situation | What happens | What to do |
|---|---|---|
| Client strips `<style>` in the head | All media queries and embedded rules are lost | Make the inline version usable on its own |
| Client supports only part of CSS | Unsupported properties or selectors are ignored | Keep embedded rules simple, using class selectors |
| One bad rule in the block | In some clients, a parsing error can discard the entire block | Validate the CSS and avoid unusual syntax |
| Client rewrites class names | Selectors may no longer match | Use plain, short class names and test |
The row about a bad rule matters in practice. Some clients stop parsing the block when they see syntax they do not understand. If you put a rule that only one client understands first, you can lose everything after it. Keep experimental or client-specific rules in separate <style> blocks, or place them after the rules that matter most.
Specificity and the `!important` rule
Inline styles have higher specificity than any selector in a style block. This is why a media query rule such as .stack { display: block; } does nothing if the element already has display:table-cell inline. The inline value wins.
To override inline CSS from a style block, you must add !important to the embedded declaration:
<style>
@media screen and (max-width: 600px) {
.stack { display: block !important; width: 100% !important; }
.pad { padding: 16px !important; }
}
</style>This is one of the few places where !important is the correct tool and not a code smell. Limit it to declarations that exist to override an inline value inside a media query. Do not add it to ordinary rules.
The consequence for your design is that inline values describe the desktop, or default, state, and the media query rules describe the changes for small screens.
A worked example
Here is a two-column row that works with only inline styles, and improves when the style block survives.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Order update</title>
<style>
@media screen and (max-width: 600px) {
.stack { display: block !important; width: 100% !important; }
}
</style>
</head>
<body style="margin:0;padding:0;background-color:#f4f4f4;">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">
<table role="presentation" width="600" cellpadding="0" cellspacing="0" border="0" style="width:100%;max-width:600px;background-color:#ffffff;">
<tr>
<td class="stack" width="50%" valign="top" style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;">Items</td>
<td class="stack" width="50%" valign="top" style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;">Delivery</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>In a client that keeps the style block and supports media queries, the two cells stack on a phone. In a client that removes the block, the two cells stay side by side inside a table that still shrinks to the screen width, because the width and max-width are inline. Both results are readable.
Which properties to inline
Not everything needs to be inline. Use this as a guide.
- Always inline:
font-family,font-size,line-height,color,padding,background-color,border, andtext-align. These define the base appearance. - Also use attributes:
width,height,cellpadding,cellspacing,border,align,valignandbgcolor. Older clients, notably desktop Outlook, respect these attributes more reliably than CSS. - Block only: media queries, dark mode overrides, and hover effects.
Repeating the font settings on every <td> feels wasteful, but table cells in some clients do not inherit text styles from a parent as you expect. Setting them on each cell that holds text avoids surprises.
Automating inlining
Writing every style by hand is slow and error-prone. Most teams write a normal stylesheet during development and run an inliner as a build step. The tool reads the CSS, finds the matching elements, and copies the declarations into style attributes. Well-known options include Juice, Premailer, and the inliner built into frameworks such as MJML.
A few points apply to any inliner:
- It should leave media queries and other rules it cannot inline in the
<style>block. Check the output to confirm this. - Inlined output is long and hard to read. Keep your source file and treat the inlined file as a build artifact.
- Inlining raises the specificity of every rule to the inline level, so your
!importantoverrides in the block are still required. - Check that shorthand properties are not expanded in ways that conflict with your overrides. If your media query sets
paddingand the inliner wrotepadding-left,padding-right, and so on, the override may only partly apply.
Here is a small Node.js script using Juice:
const juice = require("juice");
const fs = require("fs");
const html = fs.readFileSync("src/email.html", "utf8");
const output = juice(html, {
preserveMediaQueries: true,
applyWidthAttributes: true,
applyHeightAttributes: true,
removeStyleTags: false
});
fs.writeFileSync("dist/email.html", output);Run npm install juice first. The options keep media queries in the block and also copy width and height onto elements as attributes, which helps older clients.
Common errors
- Media query has no effect. The inline value is winning. Add
!importantto the embedded declaration. - The whole style block disappears in one client. Look for a rule that client cannot parse. Move client-specific rules to their own block.
- The layout is fine in your browser and broken in the inbox. You probably relied on a
<link>stylesheet or on a selector the client dropped. Inline the essentials. - Text styles missing in some cells. Set font properties on the cell that holds the text.
- Hover colors do nothing. Many clients ignore
:hover. Never put required information behind it.
Review
The inline style attribute is the layer every client reads, so it holds the base design. The <style> block holds what inline CSS cannot say: media queries, dark mode, and hover. Embedded rules must carry !important to beat inline values. Use an inliner in your build, but inspect its output.
Exercise
A template has <td class="col" style="display:table-cell;width:50%;"> and a media query .col { display:block; width:100%; } inside @media (max-width:600px). On a phone in a client that supports media queries, the columns do not stack. Why, and what is the fix?
Show answer
Inline styles have higher specificity than the class selector in the style block, so the inline display:table-cell and width:50% win. Add !important to the embedded declarations: .col { display:block !important; width:100% !important; }.
Exercise
A client strips your <style> block. Name two things you should have done so the email is still usable, and say what you lose.
Show answer
Put the base typography, colors, padding, and widths inline, and use a fluid table with width:100%;max-width:600px so it still scales down. You lose the media-query behavior, such as stacking columns, and any dark mode overrides, but the content stays readable.