Inline CSS

Automatically inline CSS from <style> tags in your HTML emails.

CSS inlining is still important in HTML email, mainly because of Outlook on Windows, which doesn't support multiple classes on elements.

It can also help preserve a decent layout in email clients that do not support embedded CSS (in <style> tags), or when an email is forwarded.

Not to mention that the utility-first approach in Tailwind CSS works great with CSS inlining: utility classes are not 'global', so you won't end up with a font-family inlined on every element.

Usage

To enable CSS inlining, simply set inlineCSS to true in your config:

config.js
module.exports = {
  inlineCSS: true,
}

Customization

If you need control over how your CSS is inlined, you may pass a configuration object to inlineCSS. Doing this in your Environment config.js will enable CSS inlining for all Templates when building for that Environment.

Style to attribute

Defines which CSS properties should be duplicated as what HTML attributes.

It runs by default as part of the CSS inlining process and it duplicates vertical-align as valign, but you may customize the property-attribute mapping.

For example, this property-attribute assignment:

config.js
module.exports = {
  inlineCSS: {
    styleToAttribute: {
      'background-color': 'bgcolor',
    }
  }
}

... will transform this:

<table class="bg-slate-300">
  <tr>
    <td>...</td>
  </tr>
</table>

... into this:

<table bgcolor="#cbd5e1" style="background-color: #cbd5e1">
  <tr>
    <td>...</td>
  </tr>
</table>

Attribute to style

Duplicates specified HTML attributes as inline CSS.

See the documentation here.

Width attributes

Array of HTML elements that will receive width attributes based on inline CSS width.

Defaults to an empty array [] so that no width attributes are added.

Works together with styleToAttribute.

config.js
module.exports = {
  inlineCSS: {
    applyWidthAttributes: ['table', 'td', 'th']
  }
}

Height attributes

Array of HTML elements that will receive height attributes based on inline CSS height.

Defaults to an empty array [] so that no height attributes are added.

Works together with styleToAttribute.

config.js
module.exports = {
  inlineCSS: {
    applyHeightAttributes: ['table', 'td', 'th']
  }
}

Keep only attribute sizes

Define for which elements should Maizzle keep only attribute sizes, like width="" and height="". Elements in these arrays will have their inline CSS widths and heights removed.

It's set to empty arrays by default, so that no elements are affected:

config.js
module.exports = {
  inlineCSS: {
    keepOnlyAttributeSizes: {
      width: [],
      height: []
    }
  }
}

You can add HTML elements like this:

config.js
module.exports = {
  inlineCSS: {
    keepOnlyAttributeSizes: {
      width: ['table', 'td', 'th', 'img', 'video'],
      height: ['table', 'td', 'th', 'img', 'video']
    }
  }
}

Prefer bgcolor attribute

Enable this option to remove any inlined background-color CSS properties, but keep any corresponding bgcolor attributes.

config.js
module.exports = {
  inlineCSS: {
    preferBgColorAttribute: true
  }
}

You may pass an array of tag names that it should remove the background-color from:

config.js
module.exports = {
  inlineCSS: {
    preferBgColorAttribute: ['td'] // default: ['body', 'marquee', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr']
  }
}

In the example above, background-color will be removed only from <td> elements.

Excluded properties

Array of CSS property names that should be excluded from the CSS inlining process.

Names are considered unique, so you will need to specify each one you'd like to exclude.

For example:

config.js
module.exports = {
  inlineCSS: {
    excludedProperties: ['padding', 'padding-left']
  }
}

Prevent inlining

Use the data-embed attribute on a <style> tag to prevent Juice from inlining the CSS inside it. Useful for writing email client CSS hacks, or for preserving CSS comments in tandem with the removeCSSComments: false Cleanup option.

API

You may pass your own CSS through the customCSS option.

If you don't specify customCSS, your HTML string will need to have a <style> with CSS tag in the <head>.

Additionally, you may configure the Juice library by passing options in the same object.

app.js
const {inlineCSS} = require('@maizzle/framework')
const config = {
  customCSS: '',
  excludedProperties: ['padding', 'padding-left'] // Juice option
}

const html = await inlineCSS('html string', config)
Copyright © 2023 Maizzle SRL Brand policy
Edit this page on GitHub