Toolvito

Works on this device

Email CSS Inliner

Many mail programs drop or limit the style blocks of an email, so the styles must stand in each tag. Paste the HTML of your email, and this page writes the rules of its <style> blocks into style attributes. Rules that cannot be inlined, such as media queries, stay in the style block. The HTML is read as text in your browser and is never run.

Your email

Writes the rules of the <style> blocks into style attributes, because many mail programs drop or limit style blocks.

Paste the HTML source of the message, with its <style> blocks. It is read as text in this browser. Nothing in it is run, loaded or sent.

More options

Some older mail programs follow these attributes better than CSS. The rules for them are on this page.

Without the tick, !important is used to decide which declaration wins and is then left out, so that a kept @media rule can still win on a small screen.

Your values are worked out in this browser. They are not sent to a server.

Your HTML with inline CSS will show here

Paste the HTML, or choose Try an example.

How to use the inliner

  1. Paste the HTML source of your email, with its <style> blocks, or open the file.
  2. Choose what stays in the style block. The first choice keeps only what cannot be inlined, such as @media rules and :hover.
  3. Choose Inline the CSS. The result is the new HTML source. The table lists every rule with its specificity, the number of elements it matched and what happened to it.
  4. Copy the HTML or download it, and send a test message to the mail programs that matter to you.

Which declaration wins

When two rules set the same property of one element, the page follows the cascade of CSS. A rule with a higher specificity wins: an id counts more than a class, and a class counts more than a tag name. When the specificity is the same, the rule that comes later in the source wins.

A style attribute that the element already has wins over the style blocks. A declaration with !important in a style block wins over that attribute. A declaration with !important in the attribute itself wins over everything.

The declarations are written in that order, the weakest first. So a short form such as margin and a long form such as margin-top end up in the order in which a browser would apply them. When one property is set by several rules, only the one that wins is written.

What can be inlined

  • Selectors that are matched: tag names, *, classes, ids and attribute selectors ([href], [href^="https"] and the other forms), joined by a space, >, + or ~, and lists with commas.
  • Pseudo-classes that are matched: :first-child, :last-child, :only-child, :first-of-type, :last-of-type, :only-of-type, :nth-child(), :nth-last-child(), :nth-of-type(), :nth-last-of-type() with odd, even, a number or a form such as 2n+1, and also :root, :empty and :not() around simple selectors.
  • Kept in the style block: selectors that depend on a state or on a part of an element, such as :hover, :focus, :visited, :link, ::before and ::first-line, and the forms :is(), :where(), :has() and :lang(). A style attribute cannot say these. To give every link a style, write the rule for a and not for a:link.
  • Kept in the style block: @media, @font-face, @import and every other @ rule. The rules inside @media are not looked at.
  • Left alone: a rule with a selector that cannot be read, with a pseudo-class that the page does not know, or with the new flag s in an attribute test. A browser that cannot read a selector drops the whole rule, so the page does not inline any part of it.
  • Not read: a <style> block with the attribute data-embed or data-premailer="ignore", with a media attribute other than all or screen, or with a type that is not text/css. Linked style sheets are not read, because the page loads nothing.

Worked example

The example on this page has one style block with 8 style rules and one @media rule. The rule p { margin: 0 0 16px 0; } matches both paragraphs. The rule p.note { color: #666666; font-size: 13px; } matches the second one, which already has style="margin-bottom: 0".

The second paragraph gets style="margin: 0 0 16px 0; color: #666666; font-size: 13px; margin-bottom: 0;". The rule p comes first because its specificity 0,0,1 is the lowest, then p.note with 0,1,1, and the paragraph's own margin-bottom comes last, so it still wins over the margin of the rule.

In all, 7 selectors are inlined, 7 elements get a style attribute, and 16 declarations are written. The rule a:hover and the @media rule stay in the style block.

Old attributes for tables and images

Under More options the page can also write three old HTML attributes, which some older mail programs follow better than CSS. Each one is written by one rule, and a tag that already has the attribute keeps it.

  • width on table, td, th and img, from a width in whole pixels (600px gives width="600"). On table, td and th also from a percentage (100% gives width="100%").
  • bgcolor on body, table, tr, td and th, from a background-color written as # and 6 digits, or # and 3 digits, which is written out as 6. Colour names and rgb() give no attribute.
  • align on td and th, from a text-align of left, center, right or justify.

Limits you should know

  • Test the result in the mail programs that matter to you. Mail programs do not all follow the same CSS, and this page does not know which properties each program takes.
  • The HTML is read as text and is never drawn. Tags that a browser adds by itself (html, head, body, tbody) are not added. So table > tr matches here when the source has no tbody tag, and tbody > tr does not.
  • A kept rule such as a:hover can only win over an inlined property when it has !important, because a style attribute is stronger than a style block.
  • Inherited properties, such as the colour of text, are not copied to child elements. Only the elements that a selector matches get the declarations.
  • Properties and values are not checked. A property that no program knows is written as it is, and short forms such as margin are not taken apart. When several rules set one property, only the one that wins is written, also if a mail program does not know its value. A browser would then fall back to the weaker rule.
  • Broken HTML is read with the usual repair rules. Where a browser repairs in a way of its own, for example a link inside a link, the elements can be counted differently.
  • Class names, ids and attribute values are compared exactly as written, with their capital letters. Tag and attribute names are compared without them.
  • A selector that matches no element is taken out of the style block all the same. If your email tool adds content with those classes later, choose to keep the whole style block.
  • Rules nested inside a rule are not read. An address written without quotes that holds /* is read as the start of a comment.
  • Only the changed start tags and the style blocks are rewritten. Line ends are written as LF. The largest input is 500,000 characters.
  • For the plain text part of the message, use the Email Plain Text Converter.

Related tools