How to Minify HTML: Comments and Whitespace

Last updated: · 3 min read

On production pages, every byte of HTML affects how quickly the page downloads. The HTML Minifier removes comments and unnecessary whitespace, turning the markup into a smaller document. The content of pre, textarea, script and style is kept intact, because whitespace there can carry meaning. A single space between inline elements is preserved so that the visible text still reads correctly. Minification runs in your browser, so your code is never sent to a server.

Formula

1. Split the HTML into tags, text, comments and the doctype.
2. Remove comments; only conditional comments such as <!--[if ...]> are kept.
3. Reduce every run of spaces and line breaks in a text fragment to a single space.
4. Remove the whitespace at the start and end of block elements such as div, p and ul.
5. Copy the contents of pre, textarea, script and style unchanged, and join all parts without adding spaces between them.

This is not a mathematical formula but a parsing and joining algorithm. The whitespace rules follow the whitespace processing model of the WHATWG HTML Living Standard. The aim is to remove only the unnecessary characters while keeping the visible text and the structure exactly as they are.

What the inputs mean

HTML
The HTML code to minify. It can be indented or span several lines. Comments and extra whitespace are removed, while text content is preserved.

Worked example: Minifying an indented paragraph

  1. Input: a <p> tag, a line 'Hello <b>world</b>' indented by two spaces, a closing </p>, and then a <!-- note --> comment.
  2. The indentation and line breaks sit between tags, so they are removed.
  3. The three spaces in 'Hello <b>world</b>' are reduced to one, because the space between Hello and the inline b element is kept as a separator.
  4. The <!-- note --> comment is removed entirely.

Result: The output is a single line: <p>Hello <b>world</b></p>. The visible text is unchanged; only the unnecessary characters are gone.

📦 HTML Minifier

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Minified HTML is hard to read, so always keep the source file. Minification alone does not make a page dramatically faster; server-side compression such as gzip or Brotli usually gives a larger gain, so consider both together. This is general information.

Common mistakes

  • Expecting the single space between inline elements to disappear; the tool keeps it and only reduces the extra spaces.
  • Keeping only the minified HTML as the source; later edits become very difficult.
  • Deploying without noticing that comments were removed; notes written into the code are lost in the process.
  • Assuming minification alone gives enough speed; compression must be configured separately.
  • Minifying HTML with unclosed tags and hiding the problem; the broken structure remains broken after minification.

Frequently asked questions

Are all comments removed?

Ordinary comments are removed. Conditional comments such as <!--[if ...]> are kept, because browsers still read them.

Does the page look different after minifying?

Usually not. Spaces that affect the reading of text are preserved, and extra whitespace between block elements is not displayed anyway.

Are pre and textarea contents damaged?

No. Their contents are left as they are, so code samples and text areas keep their layout.

How much smaller does the file get?

It depends on indentation and the number of comments. The gain becomes more visible when server compression is used as well.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice