Removing Unused CSS

A complete guide to eliminating CSS bloat and improving page performance through tree shaking and automated tools.

Every web project accumulates dead code over time. CSS is particularly prone to bloat because developers add styles for features that get deprecated, copy-paste components without cleaning up styles, or include entire framework CSS files when they only need a fraction. This guide covers how to identify and remove unused CSS using modern tools and build processes.

Our web development team regularly implements CSS optimization as part of performance-focused projects, ensuring lean stylesheets that contribute to excellent Core Web Vitals scores.

What Is Unused CSS

Unused CSS refers to selectors that don't match any elements in the rendered page. This code bloat accumulates over a project's lifecycle through various sources:

  • Orphaned stylesheets: CSS files from deprecated features
  • Legacy component styles: Styles for components that were replaced
  • Framework overhead: Entire CSS libraries when only portions are needed
  • Copy-paste accumulation: Styles added without cleanup

Unlike JavaScript, where unused code is easier to detect through static analysis, unused CSS detection requires understanding dynamic DOM manipulation since class names can be added or removed at runtime.

Why It Matters

Large CSS files directly impact performance. The browser must download, parse, and apply all CSS before showing the page. Even unused styles block rendering:

  • Increased payload: Larger download times affect First Contentful Paint
  • Parse overhead: CSSOM construction takes longer with more rules
  • Memory impact: Larger CSS Object Model in browser memory
  • Mobile impact: Especially pronounced on constrained devices and slow connections

Modern web development demands lean CSS. Every kilobyte counts toward Core Web Vitals scores and user experience. When working with our frontend development services, we prioritize CSS efficiency from project start.

CSS Tree Shaking Fundamentals

Tree shaking is a form of dead code elimination popularized by JavaScript bundlers like Rollup and webpack. The concept applies to CSS by identifying selectors that don't appear in any content files.

How Tree Shaking Works

Unlike JavaScript's ES2015 module system, CSS lacks a native tree-shaking mechanism. Tools approach this by:

  1. Content scanning: Analyzing HTML, templates, and source files
  2. Selector extraction: Finding all CSS classes, IDs, and element selectors
  3. Matching: Comparing extracted selectors against the stylesheet
  4. Removal: Eliminating rules that have no match
// Build tool configuration for CSS optimization
module.exports = {
 module: {
 rules: [
 {
 test: /\.css$/,
 use: [
 MiniCssExtractPlugin.loader,
 {
 loader: 'css-loader',
 options: { importLoaders: 1 }
 },
 {
 loader: 'postcss-loader',
 options: {
 postcssOptions: {
 plugins: ['purgecss-plugin']
 }
 }
 }
 ]
 }
 ]
 }
}

PurgeCSS provides comprehensive plugins for all major build systems, making integration straightforward regardless of your tooling choice.

PurgeCSS: The Leading Solution

PurgeCSS has become the standard tool for removing unused CSS. It scans your content files to find used selectors, then removes everything else from your stylesheets.

How PurgeCSS Works

  1. Content Analysis: Scans HTML, JavaScript, JSX, Vue templates, and other file types
  2. Selector Extraction: Uses specialized extractors for each file format
  3. CSS Processing: Walks through CSS and identifies used vs unused rules
  4. Output Generation: Produces clean CSS containing only necessary styles

Key Features

  • Multiple extractors: Support for various template languages
  • Safelisting: Keep dynamic or runtime-generated classes
  • Framework plugins: Official integrations for webpack, PostCSS, Gulp, and more
  • Flexible configuration: Control what gets scanned and what gets removed
# CLI usage example
purgecss \
 --content src/**/*.html src/**/*.js src/**/*.jsx \
 --css src/**/*.css \
 --out dist/

Frameworks like Tailwind CSS rely on content scanning for tree shaking to work effectively, demonstrating how modern tooling approaches the problem.

Framework-Specific Integration

Next.js

Next.js includes built-in CSS optimization. For the App Router and Pages Router, configure content paths for comprehensive scanning:

// next.config.js
module.exports = {
 compiler: {
 // Enable remove unused exports for CSS
 removeUnusedCss: true
 },
 experimental: {
 optimizeCss: true
 }
}

Tailwind CSS

Tailwind's utility-first approach requires tree shaking to keep file sizes manageable. The JIT compiler automatically removes unused styles:

// tailwind.config.js
module.exports = {
 content: [
 './pages/**/*.{js,ts,jsx,tsx}',
 './components/**/*.{js,ts,jsx,tsx}',
 './app/**/*.{js,ts,jsx,tsx}',
 './src/**/*.{js,ts,jsx,tsx}'
 ],
 theme: {
 extend: {}
 },
 plugins: []
}

Vue, React, and Angular

Each framework has specific PostCSS plugins and webpack configurations for PurgeCSS integration. Our React development services and Vue development services include optimized CSS workflows as standard practice.

For teams using modern frameworks, combining performance optimization services with CSS tree shaking delivers significant improvements in page load times and user experience.

Best Practices for Unused CSS Removal

Safelisting Selectors

Some classes can't be detected through static analysis. Use safelisting for:

  • Dynamic class names generated at runtime
  • Classes used in JavaScript without template references
  • Third-party library dependencies
purgecss({
 content: ['./src/**/*.{html,js,jsx}'],
 safelist: {
 standard: ['hidden', 'active', 'dropdown-*'],
 deep: [/^card-/],
 variants: ['sm', 'md', 'lg']
 }
})

Configuration Guidelines

  • Comprehensive content globs: Include all template and source files
  • Keep globs updated: Add new directories as the project evolves
  • Test after changes: Verify styles render correctly after enabling purge
  • Start conservative: Begin with full purging, then safelist as needed

Common Pitfalls to Avoid

  • Over-safelisting "just in case" defeats the purpose
  • Forgetting to update content configuration when adding new directories
  • Skipping visual regression testing after CSS changes
  • Not testing across different browsers and devices

Following these best practices ensures your CSS remains lean throughout the project lifecycle, supporting our commitment to quality assurance in all web development projects.

Measuring the Performance Impact

Quantify the benefits of unused CSS removal:

MetricBeforeAfterImprovement
CSS File Size250 KB9 KB96% reduction
First Contentful Paint1.8s0.9s50% faster
Lighthouse Performance7294+22 points

Real-world implementations demonstrate that careful CSS optimization can reduce file sizes dramatically, with some projects dropping from hundreds of kilobytes to single-digit kilobytes.

Core Web Vitals Impact

Removing unused CSS directly improves:

  • Largest Contentful Paint (LCP): Smaller CSS = faster rendering
  • First Input Delay (FID): Less main thread work from CSS parsing
  • Cumulative Layout Shift (CLS): Faster style application reduces layout shifts

Our technical SEO services incorporate CSS optimization as part of comprehensive performance strategies that improve search rankings and user experience.

Ready to Optimize Your CSS?

We help teams implement modern CSS workflows that keep stylesheets lean and performant. Contact us to discuss how we can improve your website's performance.

Sources

  1. CSS-Tricks - Unused - Comprehensive overview of unused CSS detection challenges and PurgeCSS success stories
  2. PurgeCSS Documentation - Complete technical reference for CSS tree shaking
  3. Tailwind CSS - Controlling File Size - Official guidance on tree shaking with utility frameworks