← All blog posts

HTML Minifier formula — how it actually works

WhiteUI · A plain-English guide to the HTML Minifier

The HTML Minifier is not a black box. It follows five short rules, in a fixed order, and every byte it saves can be traced back to one of them. There is no guessing and no hidden step: the same input always gives the same output. The rules below are the whole method, followed by a worked check you can do by hand.

1. Remove comments: <!-- ... --> → nothing
2. Collapse each run of whitespace to one space
3. Remove the space between > and <
4. Remove the space after > and before <
5. Trim the ends

Saved % = (original − minified) ÷ original × 100

At a glance

Inline spaces: 29% saved14 bytes removed35 bytes kept49 bytes in, 35 bytes out
A paragraph with doubled spaces goes from 49 bytes to 35 bytes. The 14 bytes removed are the extra spaces between words.

Rule 1: comments are removed

Anything between <!-- and --> is deleted, including the markers themselves. The sample comment <!-- a note --> is 15 characters, and all 15 disappear. A comment in the head of a page, such as <!-- page title -->, is 19 characters, and all 19 disappear. Comments never reach the reader, so removing them changes nothing on screen. One warning: a comment sitting between two words with no space on either side joins those words, so <p>Hello<!-- note -->world</p> becomes <p>Helloworld</p>, and the two words run together.

Rule 2: whitespace runs collapse to one space

Any run of spaces, tabs and line breaks becomes a single space. A paragraph written as <p>Hello world, this is a test.</p> is 49 bytes, and after this rule it reads <p>Hello world, this is a test.</p>, which is 35 bytes, so 14 bytes are saved, or 29% smaller. The words still have one space between them, so the sentence reads exactly as before. This is the rule that does most of the work on ordinary pages, because indented HTML is full of line breaks and tabs.

Rules 3 and 4: spaces around tags are cut

After the runs are collapsed, the space between a closing angle bracket and the next opening bracket is removed, so > < becomes ><. Then the space after a > and the space before a < are removed too, so > Hello < becomes >Hello<. That is how the sample heading <h2> Hello </h2> becomes <h2>Hello</h2>: the two spaces around Hello are cut because they sit next to tags, not between words. Inside a sentence the single space stays, because it sits between two letters, not next to a bracket.

Rule 5: the ends are trimmed

Any space left at the very start or the very end of the whole input is removed. A file that is only whitespace, seven characters of spaces and line breaks, minifies to nothing at all: 0 bytes. An empty input stays empty, at 0 bytes, and the tool reports 0% smaller rather than an error.

Worked example: check the maths yourself

Take the sample and work it through by hand. The original is 91 bytes and the minified version is 64 bytes, so the saving is 91 − 64 = 27 bytes. The percentage is 27 ÷ 91 × 100, which is 29.7%, shown rounded as 30%. You can also account for the 27 bytes rule by rule: the comment is 15 characters, the four line breaks are 4, the indentation is 6 spaces, and the two spaces around Hello are 2, and 15 + 4 + 6 + 2 = 27. If your hand count lands on 27, you have the method right.

The common mistake

People read the rules and expect the tool to shorten attribute values or rename classes, because that is what some heavier minifiers do. This one does not: attribute names, values and quotes are copied exactly as written, so class="card" stays class="card" and href="/about/" stays href="/about/". If your page is already short on whitespace, the saving will be small, and that is the tool working correctly, not a fault.

Frequently asked questions

What are the five rules of the HTML Minifier?

Remove comments, collapse each whitespace run to one space, remove the space between tags, remove the space after and before tags, and trim the ends.

How is the percentage saved worked out?

As (original − minified) ÷ original × 100. For the sample that is 27 ÷ 91 × 100, which is 29.7%, shown rounded as 30%.

Does the minifier change attribute values?

No. Attribute names, values and quotes are copied exactly as written; only comments and whitespace are touched.

What happens if I minify an empty input?

Nothing. An empty input stays at 0 bytes and the tool reports 0% smaller, and a file of only whitespace minifies to 0 bytes too.

More guides on the HTML Minifier

Try it yourself

Try the HTML Minifier — free, in your browser.

Open the HTML Minifier