explaingit

uncss/grunt-uncss

3,799HTMLAudience · developerComplexity · 2/5Setup · easy

TLDR

A Grunt plugin that removes unused CSS from your stylesheets, shrinking a full Bootstrap stylesheet from 120KB down to 11KB by scanning which styles your HTML pages actually use.

Mindmap

mindmap
  root((grunt-uncss))
    What it does
      Scans HTML files
      Removes unused CSS
      Outputs slim stylesheet
    Tech Stack
      Grunt build tool
      JavaScript
      UnCSS library
    Use Cases
      Bootstrap sites
      Server-rendered CMS
      Mobile optimization
    Audience
      Web developers
      Frontend builders
    Limitations
      Complex selectors
      Runtime CSS classes
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Strip unused Bootstrap CSS from a multi-page website to cut stylesheet size from 120KB to ~11KB.

USE CASE 2

Automate CSS cleanup as part of a Grunt build pipeline before deploying to production.

USE CASE 3

Scan a WordPress or CMS site by URL to trim its stylesheets without access to static HTML files.

USE CASE 4

Whitelist JavaScript-added CSS classes so they are preserved even though they don't appear in static HTML.

Tech stack

JavaScriptGruntHTMLCSSNode.js

Getting it running

Difficulty · easy Time to first run · 30min

Requires an existing Grunt setup, adding the plugin and a config block is straightforward once Grunt is in place.

License information is not mentioned in the explanation.

In plain English

grunt-uncss is a plugin for Grunt, a JavaScript build tool that automates repetitive tasks in web development projects. This particular plugin solves a specific problem: when you use a large CSS framework like Bootstrap in your website, you typically only use a fraction of the styles it provides, but the full stylesheet still gets loaded by every visitor. grunt-uncss scans your HTML files, figures out which CSS rules are actually used on those pages, and produces a smaller stylesheet containing only those rules. The result can be dramatic. The README demonstrates taking a multi-page Bootstrap site from over 120 kilobytes of CSS down to about 11 kilobytes. Smaller stylesheets mean faster page loads, especially on mobile connections. To use it, you add the plugin to your existing Grunt configuration, then tell it which HTML files to scan and where to write the cleaned CSS output. The configuration is a short block of JavaScript that lists your input HTML files and a destination filename for the trimmed stylesheet. You can also provide a list of selectors to ignore, which is useful for CSS classes that get added to the page by JavaScript at runtime and would not appear in the static HTML scan. The plugin also supports scanning pages by URL rather than local files, which makes it possible to use with server-rendered sites built on platforms like WordPress or other content management systems, though that approach is slower. One known limitation noted in the README is that the CSS parser it relies on does not handle certain complex CSS selectors correctly, which can cause errors at build time. The suggested workaround is to move those problematic styles into a separate stylesheet that the plugin does not process. The project relies on the underlying UnCSS library for the actual CSS analysis, so issues with parsing behavior should be reported there rather than on this plugin's tracker.

Copy-paste prompts

Prompt 1
I'm using Bootstrap on my website and the CSS file is huge. Show me how to set up grunt-uncss in my Gruntfile to scan my HTML pages and output only the CSS I actually use.
Prompt 2
My grunt-uncss task is failing because of complex CSS selectors. How do I move the problematic styles into a separate file that uncss skips?
Prompt 3
I have a WordPress site and I want to use grunt-uncss by scanning URLs instead of local HTML files. Write the Grunt config block to do this.
Prompt 4
After running grunt-uncss, some CSS classes added by JavaScript are missing from the output. How do I add them to the ignore list in the Grunt config?
Open on GitHub → Explain another repo

← uncss on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.