HandyBench

HTML Minifier

Strip whitespace and comments from HTML.

How to use the HTML Minifier

  1. 1 Paste your HTML.
  2. 2 Choose whether to strip comments.
  3. 3 Copy the minified markup.

What the minifier removes

The routine is deliberately small. It runs five steps in order, and nothing else is rewritten:

  1. Every <pre>, <textarea>, <script> and <style> block is set aside untouched.
  2. If "Strip comments" is ticked, all <!-- ... --> comments are deleted.
  3. Every run of whitespace, including line breaks and tabs, becomes one space.
  4. A whitespace gap between two tags is removed only when at least one of them is block-level (html, head, body, div, p, pre, lists including dl, dt and dd, tables with their rows, cells, captions and column groups, section, header, footer, nav, main, article, aside, address, headings, figure and figcaption, fieldset and legend, details and summary, dialog, menu, search, form, blockquote, option and optgroup, template, meta, link, script, style and title). Between inline tags the gap stays as a single space, and so does the gap beside inline or replaced elements such as a, img, input, button, label, select, textarea, video, audio, iframe and canvas. Tags are read quote-aware, so a > inside an attribute value does not end the tag.
  5. The protected blocks are put back, and the ends are trimmed.

Worked example

A 187-character page with indentation, an HTML comment and a <pre> block, run with comments stripped, comes back as 136 characters, a saving of 27% as the Stat boxes report it:

<!DOCTYPE html><html><head><title>Demo</title></head><body><p>Hello <b>bold</b> <i>italic</i></p><pre>  keep
   this</pre></body></html>

The comment and all indentation are gone, the space between bold and italic is still there, and the two-space indent inside the preformatted block survived. Pages with deep indentation and many comments shrink the most, and already-compact markup barely changes.

Where it can change how a page looks

  • Runs of spaces collapse to one. Browsers already display several spaces in normal text as one, so nothing visible changes. If you need a wider gap, use &nbsp; or CSS, which are not touched. Only the <pre> and <textarea> blocks keep their exact spacing.
  • Conditional comments such as <!--[if IE]> are deleted as ordinary comments when stripping is on.
  • Inline CSS and JavaScript are kept as written. To compress them, run them through the CSS minifier and JS minifier first.
  • No attribute rewriting. Quotes, optional closing tags and boolean attributes are left alone, which is why the saving is smaller than with a build-time tool. The one exception is whitespace: a run of spaces or line breaks inside an attribute value, such as title="a b", is collapsed to a single space like everywhere else outside the protected blocks.

Reading the numbers

Original and Minified are character counts of the text, not bytes on the wire. Servers normally apply gzip or Brotli on top, and those algorithms already squeeze repeated whitespace well, so the real-world gain from minifying is smaller than the percentage shown. Always test the output in a browser before replacing your live file.

When this tool fits

Sites built with a framework normally minify as part of the build, which can also rewrite attributes and drop optional tags. This page suits one-off jobs where no build exists: a hand-written landing page, an HTML email template, an embed snippet or a block of markup for a CMS field with a length limit. Keep your readable source file, since minified markup is awkward to edit later.

Examples

  • ▸ Strip comments and indentation from a hand-written landing page before pasting it into a CMS.
  • ▸ Shrink an HTML email template so it stays under a client's size limit.

Frequently asked questions

Will minifying break my HTML?

It only removes comments and whitespace, and it leaves <pre>, <textarea>, <script> and <style> blocks untouched. Whitespace between two inline tags such as <b> and <i> is kept as a single space, and gaps next to block-level tags such as <div>, <p> and <li> are removed. Still, check the result in a browser before deploying.

How much smaller will my HTML get?

It depends on how much indentation and commenting your markup has. Heavily indented pages can lose a quarter or more of their characters, while already-compact HTML barely changes. Gzip or Brotli on the server already compresses whitespace, so the net gain on the wire is smaller than the percentage shown.

Does it minify inline CSS and JavaScript?

No. Style and script blocks are kept exactly as written. Run them through the CSS minifier and JS minifier separately if you want those shrunk too.

Should I keep an unminified copy?

Yes. Minified HTML is hard to edit, so keep your readable source and minify as a build step or before deploying.