๐Ÿ‹
Menu
How-To Beginner 1 min read 168 words

How to Minify CSS for Production

CSS minification removes whitespace and comments to reduce file size. Learn safe minification techniques that don't break your styles.

Key Takeaways

  • CSS minification strips comments, whitespace, semicolons after the last property, and unnecessary zeros (0.5 โ†’ .5, 0px โ†’ 0).
  • Minification typically reduces CSS file size by 15-25%.
  • Never remove CSS that looks unused without testing โ€” it may be used dynamically.
  • Generate source maps alongside minified CSS so browser DevTools can map back to original line numbers.
  • Client-side CSS minification processes files entirely in the browser.

What Minification Removes

CSS minification strips comments, whitespace, semicolons after the last property, and unnecessary zeros (0.5 โ†’ .5, 0px โ†’ 0). Advanced minifiers also merge duplicate rules and shorten color values (#ffffff โ†’ #fff).

Size Savings

Minification typically reduces CSS file size by 15-25%. Combined with gzip/brotli compression, the total reduction reaches 80-90%. For a 100KB stylesheet, this means serving just 10-20KB over the network.

Safe Minification Rules

  • Never remove CSS that looks unused without testing โ€” it may be used dynamically.
  • Preserve !important declarations exactly as written.
  • Don't merge media queries that appear in different order โ€” cascade matters.
  • Keep @charset declarations at the top of the file.

Source Maps

Generate source maps alongside minified CSS so browser DevTools can map back to original line numbers. This makes debugging minified CSS in production possible.

Browser-Based Minification

Client-side CSS minification processes files entirely in the browser. This is useful for quick one-off minification without setting up build tools.

Related Tools

Related Formats

Related Guides