← All blog posts

CSS Minifier for students — with examples

WhiteUI · A plain-English guide to the CSS Minifier

School and college web work often asks for a page that loads fast and looks right: a class website, a homepage mockup, or a small project site. The CSS Minifier shrinks the stylesheet for those pages in seconds, and the examples below use the same numbers your coursework will, so you can check your own results against them.

At a glance

A button rule: 27% smaller33 bytes kept12 bytes removed45 bytes → 33 bytes, 12 saved
A button rule of 45 bytes minifies to 33 bytes, saving 12 bytes, which is 27 percent smaller.

Example 1: a button on a class website, 45 bytes to 33 bytes

A class website usually has a button, and a button rule is a good first example because it is short enough to check by hand. Written the usual way, over five lines, it is 45 bytes:

.btn {
  color: blue;
  background: white;
}

Minified, it becomes .btn{color:blue;background:white}, which is 33 bytes, saving 12 bytes, or 27% smaller. The saving comes from the line breaks, the indent spaces, the spaces after the colons, and the final semicolon before the closing brace. The two declarations are still there, in the same order, with the same values. If your teacher asks how many bytes were saved, the answer is 12, and you can show the working: 45 minus 33.

Example 2: a homepage mockup, 186 bytes to 129 bytes

A homepage mockup needs a longer stylesheet, so this page block is a better test: four rules, a comment, and 186 bytes. Minified, it becomes .page{font-family:Georgia,serif;line-height:1.6;color:#333}.page h1{font-size:28px;margin-bottom:12px}.page p{margin-bottom:16px}, which is 129 bytes, saving 57 bytes, or 31% smaller. The comment /* header styles */ disappears, the eleven lines collapse into one, and three semicolons go, one before each closing brace. The spaces that stay are the ones that matter: the space between .page and h1, and the space after the comma in Georgia, serif.

Example 3: a rule with a picture and a calculation

Some pages use a picture file with a space in its name, and a width worked out with calc(). This rule has both, and it is 91 bytes:

.hero {
  background: url("my photo.jpg");
  width: calc(100% - 20px);
  margin: 0 auto;
}

Minified, it becomes .hero{background:url("my photo.jpg");width:calc(100% - 20px);margin:0 auto}, which is 75 bytes, saving 16 bytes, or 18% smaller. The space inside "my photo.jpg" survives, because the tool copies quoted strings untouched, and the spaces around the minus sign in calc(100% - 20px) survive too. That is worth knowing for an exam question about what minifying keeps: strings and the spaces inside a calculation are safe.

The four rules, for revision

Write these down: remove /* */ comments, collapse whitespace, remove spaces around punctuation, and drop the semicolon before each closing brace. Run them on the default sample and you get 105 bytes in, 68 bytes out, and 37 saved, which is 35% smaller. Every question about what the tool does can be answered from those four rules, and the worked examples above are the proof that they hold.

The common mistake

Students sometimes hand in the minified CSS as their coursework, or paste it into an assignment that asks for readable code. A marker who reads a single long line cannot see the structure, the comments are gone, and the work looks unfinished. Use the tool to test a layout and to measure the saving, then hand in the readable original, with the minified version alongside it only if the brief asks for it.

Frequently asked questions

How much does a button rule shrink?

From 45 bytes to 33 bytes, saving 12 bytes, which is 27% smaller.

How much does a 186-byte stylesheet shrink?

From 186 bytes to 129 bytes, saving 57 bytes, which is 31% smaller.

What are the four rules of the CSS Minifier?

Remove /* */ comments, collapse whitespace, remove spaces around punctuation, and drop the semicolon before each closing brace.

Does minifying change quoted file names?

No. Strings in quotes are copied untouched, so "my photo.jpg" keeps its space.

More guides on the CSS Minifier

Try it yourself

Try the CSS Minifier — free, in your browser.

Open the CSS Minifier