CSS dead code elimination
Last updated 6 years ago by parshap .
Public Domain · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install css-eliminator 
SYNC missed versions from official npm registry.


Walk rules of a style sheet and remove unused code.


Given an HTML document hello.html:

		<p>Hello World</p>

And some styles styles.css:

p {
	color: red;

a {
	color: blue;

We can eliminate parts of the styles that will not affect the DOM (example.js):

var eliminator = require("css-eliminator");
var parse = require("css-parse");
var stringify = require("css-stringify");
var fs = require("fs");

var css = fs.readFileSync("styles.css");
var html = fs.readFileSync("hello.html");
var eliminate = eliminator(html);

var ast = eliminate(parse(css))
$ node example.js
p {
  color: red;

Alternatively, you can use css-eliminator as a Rework plugin.

How It Works

This module determines if each rule in a style sheet is dead or not. A rule is considered dead if no elements exist in the given HTML document that match the rule's selector.

Pseudo Elements and Pseudo Classes

Any pseudo parts of a selector (e.g., ::after in div::after) are stripped from the selector before determining if any elements match. This is so that selectors like a.btn:hover will remain as long as an element matching a.btn exists.

Selector Filtering

To white-list, black-list, or otherwise filter which rule selectors are dropped, options.filter can be a function that will be called for each selector. If the function returns true, the selector will not be dropped and the rule will be left intact.

For example, if your appliation dynamically adds .user-profile divs to the page, you can keep all selectors containing .user-profile:

var eliminate = eliminator(html, {
	filter: function(selector) {
		return /\.user-profile/.test(selector);

You can use a selector parser like slick for powerful arbitrary selector filtering.


The API operates on HTML as strings, and CSS as ASTs produced by css-parse.

eliminator(html, options)

Create an eliminate(ast) function that will operate on CSS ASTs with the given HTML document as context.


  • options.filter: A function to control which selectors are removed.


Remove any dead code in the given CSS AST and return the AST.


  • Some pseudo classes should be considered (e.g., :nth-child())
  • Allow multiple DOM inputs
  • Remove unused keyframe definitions
  • Remove empty @media blocks and other at-rules
  • Remove unused animation keyframe declarations
  • Remove duplicate property declarations across multiple rules
  • Bug with ".wrapper ::selection {}" if .wrapper has only text nodes

Alternate Approach

Another approach would be to walk the DOM and use something like getComputedStyle() to determine which styles actually affect the DOM.


npm install css-eliminator

Current Tags

  • 1.0.1                                ...           latest (6 years ago)

12 Versions

  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           7 years ago
  • 0.5.0                                ...           7 years ago
  • 0.4.1                                ...           7 years ago
  • 0.4.0                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.3                                ...           7 years ago
  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Maintainers (1)
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 2
Dependencies (4)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |