Sub topic 3
Inline CSS and what a style block can do
This chapter assumes you know basic HTML and CSS, including the box model and media queries. It also assumes you know that an email client is the program that receives and displays a message. The previous chapter showed how to build a template with tables. Here you will style that template.
Why inline CSS is the default
Email clients treat styles differently from browsers. Some strip <style> blocks entirely. Others remove everything outside a limited set of rules. Gmail, for example, has historically ignored embedded styles in some account types. Inline styles, which sit in a style attribute on each element, survive in nearly every client.
So the working model is:
- Put every style that matters for the basic design inline. This includes colors, fonts, padding, and widths.
- Put only optional enhancements in a
<style>block. These include media queries, dark mode overrides, and hover effects. - Write the template so it still looks correct if the style block is dropped.
This approach is called progressive enhancement. The inline layer is the baseline, and the style block improves it where the client allows.
What goes inline
Inline CSS does not inherit the way you may expect. Some clients reset fonts on table cells, so setting font-family on <body> alone is not enough. Repeat the text properties on the cells that hold text.
<td style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;">
Thanks for your order.
</td>Use properties with wide support: padding, font-family, font-size, line-height, color, background-color, text-align, and border. Prefer padding on table cells over margin, because margin support on table cells and some elements is unreliable. Write colors as six-digit hex values and sizes in px.
What a style block can do
A <style> block in the <head> is the only place media queries can live. It also lets you target things that inline styles cannot, such as @media rules, :hover, and prefers-color-scheme.
<style>
@media screen and (max-width: 600px) {
.stack { display: block !important; width: 100% !important; }
}
</style>Notice the !important. Inline styles have higher priority than rules in a style block. If a cell has width:50% inline, a class rule in the block loses unless it uses !important. This is the most common reason a media query appears to do nothing.
flowchart TDThe diagram below shows how a client decides which styles apply.
Common errors
- Relying on the style block for layout. If the block is stripped, the template collapses. Keep widths and spacing inline.
- Forgetting
!importantin media queries. The inline value wins, and the phone layout never changes. - Using shorthand or modern features without a fallback. Features such as CSS variables,
flex, andgridare not safely supported across clients. - Inlining by hand and letting the two copies drift. The inline value and the class rule can disagree. Many teams write normal CSS and run an inliner tool during the build, such as Juice or a service built into an email framework. The tool copies rules into
styleattributes and leaves media queries in the block.
Check your understanding
Exercise
A cell has style="width:50%" inline. Your media query sets .stack { width:100% } and the cell does not change on a phone. What is the likely cause, and what is the fix?
Show answer
Inline styles outrank rules in a style block. Add !important to the media query declaration, as in .stack { width:100% !important; }.
Exercise
Why should the template look acceptable even when the style block is removed?
Show answer
Some clients strip or ignore style blocks. If the baseline design is inline, those readers still see a readable layout, and the style block only adds enhancements such as stacking columns.