Developer Tools
Free CSS Minifier & Beautifier Online
Minify or beautify CSS code. Remove comments, whitespace and optimize output.
CSS Minification and Beautification: A Practical Guide
CSS minification removes characters from a stylesheet that the browser does not need to parse and apply the rules correctly — comments, whitespace, and redundant semicolons. A developer-formatted stylesheet with consistent indentation, spacing, and documentation might be 50 KB; the minified equivalent is typically 32–38 KB. When combined with server-side Brotli compression, the same stylesheet might transfer as only 7–9 KB. Minification is a zero-risk, no-code performance improvement that every production website should apply.
What Gets Removed During Minification
A CSS minifier applies the following transformations:
- Comments stripped —
/* ... */blocks are removed entirely. This includes section dividers, documentation comments, and TODO notes. Comment content is never rendered and the browser ignores it completely. - Whitespace collapsed — spaces, tabs, and newlines around structural characters (
{,},:,;,,,>,+,~) are removed. The indentation and line breaks that make your stylesheet readable are unnecessary for the browser's CSS parser. - Trailing semicolons removed — CSS allows the final declaration in a rule block to omit its trailing semicolon. The minifier removes it, saving one byte per rule.
color: red;before}becomescolor:red. - String content preserved — values inside quotes (
content: "→ ",url('image.png'),font-family: "Times New Roman") are never modified. The parser protects all quoted strings.
CSS Beautification
The beautify mode reverses minification — it takes compressed or single-line CSS and reformats it with consistent indentation and line breaks. This is useful when you receive minified third-party CSS (a plugin, a component library, a CDN stylesheet) and need to read or modify it.
The beautifier expands the minified form by:
- Opening each rule block on a new line with a space before
{ - Indenting each declaration two spaces inside its rule block
- Placing each declaration on its own line ending with a semicolon
- Separating each rule block with a blank line for scanability
- Splitting selector lists so each selector in a group appears on its own line
/* Minified */
.btn{display:inline-flex;align-items:center;padding:8px 16px;border-radius:6px;font-weight:600;cursor:pointer}
/* Beautified */
.btn {
display: inline-flex;
align-items: center;
padding: 8px 16px;
border-radius: 6px;
font-weight: 600;
cursor: pointer;
}Use beautify when auditing a third-party stylesheet, writing overrides, debugging specificity conflicts, or adapting a component's styles to your own design system.
What This Tool Does Not Do
Some CSS optimisers go further than whitespace removal. This tool intentionally does not:
- Remove unused rules — detecting which selectors match elements in your HTML requires a full DOM analysis. Tools like PurgeCSS, the Vite plugin
vite-plugin-purgecss, or Tailwind's JIT compiler perform this at build time. - Merge shorthand properties — converting four
margin-*declarations intomargin: 0requires understanding CSS cascade and property interaction rules. This is handled by cssnano's advanced preset. - Normalise colours — reducing
#ffffffto#ffforrgb(0,0,0)to#000is safe but requires a CSS-aware AST parser. cssnano applies these transforms automatically. - Strip vendor prefixes — removing obsolete
-webkit-or-moz-prefixes requires a browser-compatibility database. Autoprefixer manages this based on your Browserslist configuration. - Deduplicate rules — identifying and merging duplicate declarations across a large stylesheet requires full rule analysis.
CSS Minification in a Build Pipeline
For production projects, CSS minification belongs in your automated build, not as a manual step:
- Vite — minifies CSS automatically in production builds using esbuild (default from Vite 3) or Lightning CSS (opt-in from Vite 4.4). Zero configuration required for standard setups.
- Webpack — use
css-minimizer-webpack-pluginwhich wraps cssnano. Add it tooptimization.minimizerin your Webpack configuration. - PostCSS + cssnano — the most configurable CSS minifier. cssnano runs as a PostCSS plugin with two presets:
default(safe, widely used) andadvanced(more aggressive, may occasionally change visual output for edge-case rules). - Next.js — minifies CSS by default in production builds via SWC. No configuration needed.
- Lightning CSS — Rust-based CSS processor that handles minification, autoprefixing, and modern CSS transforms in a single pass. Significantly faster than PostCSS on large stylesheets and produces excellent output.
Adding cssnano to a PostCSS pipeline:
// postcss.config.js
module.exports = {
plugins: [
require('autoprefixer'),
process.env.NODE_ENV === 'production' && require('cssnano')({
preset: ['default', { discardComments: { removeAll: true } }],
}),
].filter(Boolean),
};Critical CSS and Inline Styles
Critical CSS is the subset of your stylesheet required to render above-the-fold content without a render-blocking external stylesheet request. Inlining it in a <style> tag in the HTML <head> lets the browser paint the initial viewport immediately, while the full stylesheet loads asynchronously.
Minifying the critical CSS block is essential — every byte inlined adds to your HTML response size, which affects Time to First Byte (TTFB) and Largest Contentful Paint (LCP). A 4 KB critical CSS block minified to 2.5 KB and Brotli-compressed becomes roughly 600 bytes.
Tools that extract critical CSS automatically:
- Critters — a Webpack plugin (also used by Angular CLI) that inlines critical CSS by rendering pages in a virtual browser and capturing the styles used for visible elements.
- critical (npm package) — a standalone tool that integrates with Gulp, Grunt, or custom build scripts. Configurable viewport size and screen dimensions for multi-breakpoint critical extraction.
- Penthouse — the underlying engine used by most critical CSS tools. Can be run directly from the command line or Node.js API.
CSS Variables and Custom Properties
CSS custom properties (--primary-color: #3b82f6, --spacing-md: 16px) are fully supported by the minifier. Their values can contain arbitrary strings including spaces, so whitespace inside a custom property value is preserved exactly. Only the surrounding whitespace in the declaration itself is collapsed:
/* Input */
:root {
--primary-color: #3b82f6;
--font-stack: "Inter", "Helvetica Neue", sans-serif;
--grid-gap: clamp(16px, 2vw, 32px);
}
/* Minified */
:root{--primary-color:#3b82f6;--font-stack:"Inter","Helvetica Neue",sans-serif;--grid-gap:clamp(16px,2vw,32px)}@media Queries and Nested Rules
The minifier handles all standard CSS at-rules, including @media, @keyframes, @supports, @layer, and @container. Nested braces are tracked so rule bodies are correctly identified and trailing semicolons are only removed where safe (before a closing }, not inside @keyframes percentage blocks where they're required).
/* Input */
@media (max-width: 768px) {
.container {
padding: 0 16px;
flex-direction: column;
}
}
/* Minified */
@media (max-width:768px){.container{padding:0 16px;flex-direction:column}}The beautifier handles nested rules correctly by tracking brace depth, so @media blocks are indented, and the rules inside them are double-indented.
Measuring the Impact
Use the stats panel in this tool to see the immediate size reduction for any file you paste. For ongoing monitoring:
- Browser DevTools → Network — filter by CSS, check the "Size" column for compressed transfer size vs. uncompressed resource size.
- Lighthouse — the "Minify CSS" audit flags stylesheets that contain whitespace and comments. Running Lighthouse in Chrome DevTools or CI gives you a direct measurement of potential savings.
- Coverage panel — DevTools → More tools → Coverage shows what percentage of each CSS file is actually applied on the current page. High unused-CSS percentages are a signal to investigate PurgeCSS or split your stylesheets by route.
- WebPageTest — provides waterfall charts and compression analysis, showing gzip/Brotli savings alongside raw file sizes.
CSS-in-JS and Styled Components
If your project uses CSS-in-JS libraries (styled-components, Emotion, vanilla-extract, Stitches), the CSS is generated at build time or runtime from JavaScript. Minification in this case is handled differently:
- Build-time extraction — tools like vanilla-extract and Linaria extract CSS to static files at build time, which are then minified by your standard CSS pipeline (PostCSS/cssnano).
- Runtime libraries — styled-components and Emotion inject styles as
<style>tags at runtime. Minification of the template literals in your JavaScript components is handled by your JS minifier. styled-components v6 includes a built-in minifier for template literal CSS. - Atomic CSS (Tailwind, UnoCSS) — generated utility classes are already minimal by design; the JIT compiler only emits classes actually used in your HTML/JS. The resulting stylesheet is small to begin with, typically 5–20 KB before compression.
This tool is most useful for projects with traditional external stylesheets, component-scoped CSS files in frameworks like Vue SFCs, or any CSS that falls outside an automated build pipeline.
Combining Minification with Compression
Minification and server-side compression are complementary and both should be applied. Minification removes redundant characters before the file is served; GZIP or Brotli compression then encodes the minified text more efficiently. The reason both matter is that compression works on patterns of repeated byte sequences — minified CSS has fewer unique sequences, which makes compression more effective.
Typical real-world results for a 50 KB stylesheet:
- Original, uncompressed: 50 KB
- Minified only: ~33 KB (34% reduction)
- Original + GZIP: ~12 KB
- Minified + GZIP: ~8.5 KB (83% total reduction from original)
- Minified + Brotli: ~7 KB (86% total reduction)
Most static hosting platforms (Vercel, Netlify, Cloudflare Pages) serve Brotli-compressed assets automatically. Ensure your server is not serving uncompressed CSS — check the Content-Encoding: br or Content-Encoding: gzip header in the Network panel.
FAQ
Common questions
What does the CSS minifier remove?
The minifier removes all CSS comments (/* ... */), collapses consecutive whitespace (spaces, tabs, newlines) to the minimum required, removes spaces around structural characters like { } : ; , > + ~, and eliminates trailing semicolons before closing braces. Property values, selector names, and quoted strings are preserved exactly.
What is the beautify mode?
Beautify mode does the opposite of minification — it takes compressed or single-line CSS and expands it with readable indentation and line breaks. Each rule block is indented two spaces, each property appears on its own line, and selector lists are separated onto individual lines. Useful when you receive minified CSS from a third party and need to read or edit it.
Does the minifier handle @media queries and nested rules?
Yes. The minifier processes all standard CSS including @media, @keyframes, @supports, and @layer blocks. The beautifier tracks brace depth so nested rules are indented correctly. Custom properties (CSS variables) and calc() expressions are passed through unchanged.
How much size reduction can I expect?
CSS minification typically saves 20–35% compared to developer-formatted source. Removing comments alone can save 5–15% in documentation-heavy files. The gains stack well with server compression: a 50 KB stylesheet minified to 33 KB and then Brotli-compressed might transfer as 8 KB.
Does it remove vendor prefixes or unused rules?
No. This is a safe, structure-preserving minifier — it only removes whitespace and comments. It does not analyse which rules are used in your HTML, and it does not strip -webkit-, -moz-, or other vendor prefixes. For unused CSS removal, use tools like PurgeCSS or the browser devtools Coverage panel.
Is my CSS sent to a server?
No. All processing runs entirely in your browser. Your stylesheets are never uploaded, transmitted, or stored anywhere. The tool works offline once the page is loaded.
Should I use this instead of a PostCSS or build tool?
For production projects, CSS minification belongs in your build pipeline: PostCSS with cssnano, Vite (built-in), or Webpack with css-minimizer-webpack-plugin. Use this tool for quick one-off minification, for CSS snippets outside your main project, or to check how much a file could save before setting up build integration.
Can it handle SCSS or Less?
No — this tool only processes standard CSS. SCSS and Less must be compiled to CSS first (using sass, lessc, or your build tool). The compiled .css output can then be minified here.
More in Developer Tools