Skip to main content
Beta: Front-End Checklist is currently in beta. Some issues are still being fixed. Thanks for your patience.

Optimize CSS file size

Keep individual CSS files small and remove unused styles to accelerate the critical rendering path.

Utilities
Quick take
Typical fix time 10 min
  • Keep individual CSS files small to speed up parsing and rendering
  • Use minification and remove unused CSS (PurgeCSS)
  • Leverage modular CSS or Tailwind to reduce duplication
Why it matters: Large CSS files block rendering and increase the time it takes for the browser to construct the CSSOM, leading to a slower First Contentful Paint.

Rule Details

CSS is a render-blocking resource. The browser must download and parse all CSS files before it can start painting pixels to the screen. Keeping these files lean is critical for fast page loads.

Code Examples

1. Removing Unused CSS with PurgeCSS

If you use a framework like Tailwind or Bootstrap, you likely have thousands of unused classes.

// postcss.config.js
const purgecss = require('@fullhuman/postcss-purgecss')
 
module.exports = {
  plugins: [
    purgecss({
      content: ['./src/**/*.html', './src/**/*.js', './src/**/*.vue']
    })
  ]
}

2. Minifying CSS

Always minify CSS for production to remove whitespace and comments.

/* Development */
.header {
  background-color: #ffffff;
  margin: 10px;
}
 
/* Production (Minified) */
.header{background-color:#fff;margin:10px}

3. Splitting CSS by Route

Instead of one giant app.css, load only the CSS needed for the current page.

<!-- Only on the contact page -->
<link rel="stylesheet" href="/css/contact-page.css">

Why It Matters

  • Rendering Performance: Smaller CSS files mean faster CSSOM construction and quicker First Contentful Paint (FCP).
  • Network Efficiency: Reducing file size lowers the time spent in the "download" phase of the network request.
  • Parsing Overhead: Browsers must parse the entire CSS file even if only a fraction of the styles are used on the current page.
  • Mobile Experience: Large files are particularly detrimental on mobile devices with limited CPU power and slower network connections.

Best Practices

Use PageSpeed Insights (opens in new tab) or the browser coverage view after trimming CSS so you can confirm the route is actually shipping less render-blocking code instead of just reorganizing styles.

Use Minification: Ensure your build process includes a CSS minifier like CSSNano. ✅ Purge Unused Styles: Use tools to remove CSS rules that aren't used in your HTML/templates. ✅ Atomic CSS: Consider methodologies like Tailwind CSS which encourage highly reusable, small utility classes. ✅ Inline Critical CSS: Inline the styles for the above-the-fold content to eliminate a network request.

Don't Include Large Frameworks Whole: Avoid linking to the full Bootstrap or Foundation CDN files if you only use a few components. ❌ Avoid Deep Nesting in Preprocessors: Deeply nested Sass/Less can generate unnecessarily long and complex CSS selectors, increasing file size.

Tools & Validation

Verification

Automated Checks

  • Measure the affected page or flow in Lighthouse, PageSpeed Insights, or DevTools and confirm the targeted metric improves.
  • Inspect the network waterfall or performance timeline to confirm the intended resource or execution change actually took effect.

Manual Checks

  • Verify the change on a throttled mobile profile, not just local desktop.
  • If this rule maps to a budget or Web Vital, confirm the page now stays within that threshold.

Use with AI

Copy these prompts to use with your AI assistant, or install the MCP server to use directly from Claude, Cursor, or Windsurf.

Check

Verify implementation

Audit the size of CSS files and identify those exceeding recommended limits (e.g., > 50KB gzipped).

Fix

Auto-fix issues

Minify CSS, remove unused styles, and consider splitting large stylesheets into smaller, page-specific files.

Explain

Learn more

Explain how CSS file size affects the critical rendering path and browser performance.

Review

Code review

Review the routes, assets, and loading behavior that affect Optimize CSS file size. Flag exact files, requests, or rendering steps that add unnecessary network, CPU, or layout cost, and describe the measurement method used to confirm the issue.

Sources

References used to support the guidance in this rule.

Further Reading

Tools and supplementary material for exploring the topic in more depth.

PageSpeed Insights
pagespeed.web.devTool

Rules that often go hand-in-hand with this one.

Optimize JavaScript bundle size

Checks for JavaScript files that exceed recommended size limits to ensure fast interaction

Performance
Keep page weight under 1500KB

Total page weight including all resources is under 1500KB (ideally under 500KB).

Performance
Enable text-based compression

Compress text resources (HTML, CSS, JS) using Gzip or Brotli to reduce data transfer size.

Performance

Was this rule helpful?

Your feedback helps improve rule quality. This stays internal for now.

Loading feedback...
0 / 385