Remove unused CSS

Cleaning up your HTML email results in smaller file sizes, which translates to faster email sendouts, faster opens (think slow 3G), and snappier paint times.

Also, Gmail will clip your email around 102KB, so anything past that mark won't even be in the DOM (which can lead to unexpected results like tracking pixel not loaded or, worse, hidden unsubscribe links).

This Transformer will remove any unused CSS styles and corresponding classes in your HTML, helping you reduce your file size.

Usage

Enable it in your Environment config:

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

Customization

You may configure this Transformer through the removeUnusedCSS key in your config.js.

whitelist

Array of classes or id's that you don't want removed. You may use any matcher patterns.

config.js
module.exports = {
  removeUnusedCSS: {
    whitelist: ['.External*', '.ReadMsgBody', '.yshortcuts', '.Mso*', '#*']
  }
}

backend

If you use computed class names, like for example class="{{ computedRed }} text-sm", the library will normally treat {{ and }} as class names and will remove them.

To prevent this from happening, use the backend option to define the delimiters:

config.js
module.exports = {
  removeUnusedCSS: {
    backend: [
      { heads: '[[', tails: ']]' },
    ]
  }
}

By default, Maizzle preserves {{ }} and {% %}, so there's no need to add them.

removeHTMLComments

Set to false to prevent email-comb from removing <!-- HTML comments -->.

config.js
module.exports = {
  removeUnusedCSS: {
    removeHTMLComments: false
  }
}

removeCSSComments

Set to false to prevent email-comb from removing /* CSS comments */.

config.js
module.exports = {
  removeUnusedCSS: {
    removeCSSComments: false
  }
}

Preserving CSS comments when inlining

If you have CSS inlining enabled, CSS comments will still be removed, even with removeCSSComments disabled.

You may use the data-embed attribute on a <style> tag to disable inlining for CSS inside it, if you need to preserve CSS comments.

For example, MailChimp uses CSS comments to define styles that are editable in their email editor. Here's how you can preserve them:

  1. Set removeCSSComments: false in your config, as above
  2. Write your CSS with comments in a separate <style> tag:
<style data-embed>
  /*
    @tab Page
    @section Body Background
    @tip Set the background colour for the email body.
  */
  .wrapper {
    /*@editable*/background-color: #EEEEEE !important;
  }
</style>

doNotRemoveHTMLCommentsWhoseOpeningTagContains

HTML email code often includes Outlook or IE conditional comments, which you probably want to preserve. If the opening tag of a conditional includes any of the strings you list here, the Transformer will not remove that comment.

config.js
module.exports = {
  removeUnusedCSS: {
    doNotRemoveHTMLCommentsWhoseOpeningTagContains: ['[if', '[endif']
  }
}

uglify

Enable this to rename all classes and id's in both your <style> tags and your body HTML elements, to be as few characters as possible.

Used in production, it will help trim down your HTML size.

config.js
module.exports = {
  removeUnusedCSS: {
    uglify: true
  }
}

API

The Transformer uses the email-comb library, see all available options here.

app.js
const {removeUnusedCSS} = require('@maizzle/framework')
const config = {/* email-comb options */}

const html = await removeUnusedCSS(`<div class="unused">test</div>`, config)
Copyright © 2023 Maizzle SRL Brand policy
Edit this page on GitHub