Eskillsfy

Home / Blog / Web and CSS

Web and CSS · 23 Nov 2026

CSS Minifier & Beautifier: how it works and how to build one

Minify CSS for production or beautify it to read. This guide explains what happens behind the button and shows the code, so you can build your own.

What it does

Minify removes comments, spaces and the last semicolon in each block, and shows how many bytes you saved. Beautify puts every rule and declaration on its own line so you can read minified CSS from a third party.

When to use it

  • Smaller CSS files for faster pages
  • Reading minified CSS from a site

How to use it

  1. Paste CSS or open a .css file.
  2. Choose Minify or Beautify.
  3. Copy or download the result.

Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/css-minifier

How it works

Minifying CSS removes what the browser doesn’t need: comments, line breaks, spaces around { } : ; and the last semicolon in each block. A careful minifier protects strings first so spaces inside content: “…” stay.

Beautifying reverses it: new line after { and ;, indent inside blocks.

Build it yourself

Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.

cssMinify.ts (ts)

export function minifyCss(css: string) {
  const keep: string[] = [];
  return css
    .replace(/"(?:\\.|[^"])*"|'(?:\\.|[^'])*'/g, (m) => `\u0000${keep.push(m) - 1}\u0000`)
    .replace(/\/\*[\s\S]*?\*\//g, "")
    .replace(/\s+/g, " ")
    .replace(/\s*([{}:;,>+~])\s*/g, "$1")
    .replace(/;}/g, "}")
    .trim()
    .replace(/\u0000(\d+)\u0000/g, (_, i) => keep[+i]);
}

Make it production-ready

  • Keep the space in calc(1px + 2px), where it is required.
  • Shorten #ffffff to #fff and 0px to 0.

Questions

Is it safe for production?

It only removes whitespace and comments. It does not rename or merge rules, so the result behaves exactly like the input.

Try the CSS Minifier & Beautifier

Free, runs in your browser, nothing is uploaded.

Open it
Keep reading

More in Web and CSS

All posts