CSS Minifier

Shrink a stylesheet by removing comments, indentation and the characters browsers do not need. Expect roughly 20 to 40% off hand-written CSS. Rules and their order stay as written, so the cascade does not change.

CSS Input
Minified CSS

1 CSS Minifier example

Source (283 bytes)

1
2
3
4
5
6
7
8
9
10
11
/* Card component */
.card {
    background : #ffffff;
    border-radius : 0.75rem;
    padding : 1.5rem 2rem;
    box-shadow : 0 1px 2px rgba(0, 0, 0, 0.05);
}

.card:hover {
    box-shadow : 0 4px 12px rgba(0, 0, 0, 0.08);
}

Minified (152 bytes)

1
.card{background:#fff;border-radius:.75rem;padding:1.5rem 2rem;box-shadow:0 1px 2px rgba(0,0,0,.05)}.card:hover{box-shadow:0 4px 12px rgba(0,0,0,.08)}

2 What gets removed

Comments

Every /* ... */ block goes, including the license header at the top. Keep a copy of the source if that header has to ship.

Whitespace

Newlines, indentation and the spaces around braces, colons, semicolons, commas and the > combinator. The single space inside a shorthand value stays, because it separates the values.

Redundant characters

The semicolon before a closing brace, the leading zero in 0.5rem, and #ffffff collapsed to #fff when both halves of each pair match.

Nothing else

Selectors, declarations, at-rules and their order are left as written, with no merging and no reordering. Strings, url() values and calc() expressions are set aside before minifying and put back unchanged.

3 Purge first, then minify

Run PurgeCSS before you minify. The two solve different problems, and purging usually saves far more. Say a framework build is 300 KB: minifying might take it to 220 KB, while removing the 95% of selectors your markup never uses could take it to 12 KB. Minify whatever survives the purge, which also leaves the minifier far less to process.

How to use the CSS Minifier tool

  1. Paste your stylesheet on the left, or use Upload CSS to load a file.
  2. The minified version appears on the right as you type, with the original size, the new size and the percentage saved in the header.
  3. Click Copy Result, or Download to save the output as style.min.css.
  4. Strings, url() values and calc() expressions are set aside before minifying, so anything inside them keeps its spacing.

How to minify CSS in a build, with PostCSS, PHP and Python

Command line

# The usual choice: cssnano through PostCSS
npm i -D postcss postcss-cli cssnano
npx postcss src/style.css --use cssnano -o dist/style.min.css

# esbuild, if it is already in the project
npx esbuild src/style.css --minify --outfile=dist/style.min.css

# Tailwind minifies on its own
npx tailwindcss -i src/input.css -o dist/style.css --minify

# Then check what you actually shipped
gzip -c dist/style.min.css | wc -c

Node.js

const postcss = require("postcss");
const cssnano = require("cssnano");
const fs = require("node:fs");

const css = fs.readFileSync("src/style.css", "utf8");

postcss([cssnano({ preset: "default" })])
    .process(css, { from: "src/style.css", to: "dist/style.min.css" })
    .then((result) => {
        fs.writeFileSync("dist/style.min.css", result.css);
        console.log(css.length, "->", result.css.length);
    });

// The "default" preset is conservative. "advanced" merges rules and
// reorders declarations, which can change the cascade. Read the diff.

Python

# pip install csscompressor
from csscompressor import compress

with open("src/style.css") as f:
    css = f.read()

minified = compress(css)

with open("dist/style.min.css", "w") as f:
    f.write(minified)

print(f"{len(css)} -> {len(minified)} bytes "
      f"({100 - round(len(minified) / len(css) * 100)}% smaller)")

Common errors and how to fix them

calc(100% - 20px) stopped working
The spaces around + and - inside calc() are required by the spec, and a naive minifier that strips them breaks the expression. This tool protects calc() for that reason; if yours does not, keep the expression in a custom property.
A media query no longer matches
@media (min-width:640px) is fine, but in @media screen and (min-width:640px) the space before the parenthesis is required. Removing it breaks the query.
Content in a ::before disappeared
Whitespace inside a quoted content string is meaningful. If a minifier collapsed it, the string was not protected; this tool sets strings aside before minifying.
The stylesheet is still enormous after minifying
Minifying removes characters, not rules. A framework build is large because most of its selectors are unused. Run PurgeCSS or Tailwind's content scanning first; purging can save 90% where minifying saves around 30%.
The license header vanished
Comments are removed, including the one at the top. Many build minifiers keep /*! ... */ comments for this reason, but this tool removes those too. Keep the license in a separate file if it has to ship.

Good to know

Compile, purge, minify, then compress, in that order. Compile Sass, purge unused selectors, minify, and let the server gzip or brotli the result. Each step has less to process because the step before removed the bulk. In the other order, the minifier works through 300 KB to save 80 KB that PurgeCSS was about to delete anyway.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

What exactly is removed?

Comments, newlines, indentation, the spaces around braces, colons, commas and the > combinator, the last semicolon in a block, and the leading zero in values like 0.5em. It also shortens #ffffff to #fff. Everything that affects rendering stays.

Will it break my CSS?

It should not. It leaves selectors and declarations alone and only touches the characters between them. Quoted strings, url() values, data URIs and calc() expressions are set aside first and put back unchanged, whitespace included.

Does it merge or reorder rules?

No. Merging duplicate selectors can change the cascade, so rules stay where you wrote them. Use PurgeCSS if you want to remove rules nothing matches.

How much does it save?

Usually 20 to 40% on hand-written CSS, and less on a framework build that ships partly minified already. The panel header shows the size before and after, and the percentage saved.

Should I still gzip after minifying?

Yes. They work on different things: minifying removes characters, and gzip compresses repetition in what remains. The two together beat either one alone.

Before and after

To read a minified file again, run it through the CSS formatter. If the stylesheet is large because it is a full framework build, PurgeCSS will save you far more than minifying will. If you write Sass, compile it first with the SCSS compiler.

More Code Formatters & Beautifiers

Browse all code formatters & beautifiers

Copied to clipboard!