Email CSS Inliner with Gmail Clipping and Support Warnings
Operator: Abdulla Al Fahad. How tools are built and checked
You write an email with a tidy style block: .card gets a background, .card p gets a colour, and one rule at the bottom stacks the columns on small screens. Then a mail client strips or rewrites the head, and the layout falls apart. Inline styles survive because they travel with each element, so the usual last step before sending is to copy every rule onto every element that matches it.
That step is a small piece of cascade logic: which rule wins when two disagree, how an existing style attribute ranks, and what cannot be inlined at all. This page does it with the browser's own selector engine, shows what was left in the head and why, and flags CSS that some clients will ignore.
Key features
- Matching with the browser's querySelectorAll, so descendant, child, sibling, attribute and structural selectors behave as they will in a client
- Cascade ordering by !important, then inline versus stylesheet, then specificity (ids, classes and attributes, elements), then source order
- Existing style attributes merged in and kept ahead of normal stylesheet rules
- Media queries, :hover and other state rules, pseudo-elements, @font-face and keyframes left in the head
- !important preserved on the inlined declarations
- Optional removal of class attributes that no remaining head rule uses, and of rules that match no element
- Specificity that handles :not(), :is(), :has() and :where() the way the CSS specification does
- Output size against Gmail's clipping limit, and a support table for the risky CSS actually present
How the cascade is reproduced and what stays in the head
Each rule's selectors are tested one by one. A selector with a state pseudo-class (:hover, :focus, :active, :visited), a pseudo-element (::before, ::after) or any other part that cannot be a static attribute is left in the style block, because no inline style can express it. A rule written as a, a:hover { color: red } is split: the plain a is inlined and the a:hover selector stays in the head with the original declarations.
Specificity is computed from the selector text: ids count in the first position, classes, attributes and pseudo-classes in the second, element names and pseudo-elements in the third, :not(), :is() and :has() take the specificity of their most specific argument, and :where() adds nothing. For each element and property the declaration with the highest key wins, where the key is important, then inline versus sheet, then specificity, then order of appearance across all style blocks. The surviving declarations are written back in ascending rank order, so a lower-ranked padding shorthand comes before a higher-ranked padding-left and the longhand still overrides it.
Existing inline declarations rank above normal stylesheet rules and below stylesheet rules marked !important, exactly as the browser applies them. The output keeps !important on the value, which is useful because some clients inject their own CSS and an important inline declaration is the one that holds.
How it works
The page parses your HTML with the browser's own parser into an inert document: scripts do not run and nothing loads. It reads every style element, splits each into rules, and asks the document which elements each selector matches. Declarations are collected per element, ranked, merged with the element's existing style attribute and written back as one style attribute.
Rules that cannot be expressed on an element stay in a rewritten style block in the head, together with at-rules such as @media and @font-face, and style elements left with nothing in them are removed. The result is serialised from the document, so the HTML you get is the browser's reading of your markup, with its tags closed and attributes quoted.
Common use cases
- A hand-coded 14 KB invoice template with 30 rules goes to an email service that does not inline CSS itself, and comes back with every td carrying its own padding and colour.
- A button looks right in the browser but loses its hover colour and its mobile padding in a client: both rules are listed as left in the head, which is where those rules have to live.
- A newsletter that measures 71 KB before inlining is checked against the Gmail clip before tracking and personalised blocks are added by the sending platform.
How to use this tool
- Paste the full HTML of the email, or drop the file. Keep the style block in the document.
- Choose whether to drop rules that match nothing and whether to strip class attributes.
- Press Inline CSS and read the counts: rules inlined, elements styled and rules left in the head.
- Check the size bar and the support table, then copy or download the result and send a test to real inboxes.
Example
Before
<style>
.card p { color: #333; margin: 0 0 8px }
p.lead { font-size: 18px }
a:hover { text-decoration: underline }
@media (max-width: 480px) { .card { padding: 8px !important } }
</style>
<td class="card"><p class="lead" style="margin:0 0 12px">Invoice ready</p></td>After (style attribute and head)
<p class="lead" style="color: #333; font-size: 18px; margin: 0 0 12px;">Invoice ready</p>
In the head: only the a:hover rule and the @media block remain.
The two rules tie on specificity (0,1,1), so source order puts font-size beside color, and the element's own margin:0 0 12px beats the sheet's margin.Tips for best results
- Write the mobile overrides in @media with !important. They stay in the head, and the !important is what lets them beat the inline styles this tool writes.
- Give table cells a background-color as well as any background image; classic desktop Outlook will use the colour when it ignores the image.
- Put the unsubscribe link and footer in the first 90 KB of the HTML if the message is near the clip limit; Gmail hides whatever comes after about 102 KB, and a long hidden footer is the part recipients need.
- Prefer a few broad selectors such as td.cta over dozens of one-off classes. Every rule that matches forty cells is written forty times, so a smaller rule set gives a smaller output.
Common pitfalls
Rules left in the head only work in clients that read the head. Gmail does for most accounts, but some apps and platforms strip style blocks, so a layout that depends on a head rule should degrade gracefully without it.
Selectors that the browser cannot parse, and rules written with CSS nesting, are left in the style block and listed as warnings rather than guessed at.
This tool inlines CSS that is already in the document. Linked stylesheets are not downloaded; paste their rules into a style block first.
Common errors and how to fix them
- "The selector ... could not be matched and was left in the style block"
- The browser rejected the selector, often a vendor-prefixed pseudo-class or a stray character. Fix or remove it; until then it stays in the head unchanged.
Best practices
- Send a test to a Gmail address, an Apple Mail inbox and a classic desktop Outlook after every layout change. The support table says where to look, but only a real inbox shows what a client does.
What the support table covers
The support table covers the features found in your CSS that are known problems somewhere: flexbox and grid, CSS variables, background images and gradients, rounded corners, shadows, max-width, positioning, opacity, calc(), animations and transforms, plus @media, @font-face and @import. Classic desktop Outlook, which lays out with Word's engine, accounts for most of the failures (the new Outlook uses a web engine and behaves differently). The table is intentionally short and conservative; check it against a current compatibility reference and real test sends before relying on it.
How the size warning works
Size is measured in UTF-8 bytes of the final HTML. Gmail clips messages at about 102 KB and replaces the rest with a link, which can hide the footer and the unsubscribe link, so the page warns from 85 percent of that limit and treats anything over it as an error. Inlining usually makes the file bigger, because a rule that applied to forty cells is now written forty times.
Frequently asked questions
Why are some rules still in the head after inlining?
They cannot be expressed on an element: media queries, :hover and other state selectors, pseudo-elements, @font-face and keyframes. Email clients need those in the head.
Does it minify the HTML?
No. It rewrites style attributes and the style block and otherwise leaves your markup and whitespace as the browser's HTML parser serialises it, so conditional comments for Outlook are kept.