b2KIT

HTML Minifier

Minify HTML by removing comments, whitespace, and optional tags while preserving functionality.

Tested tool guide Tested browser tools Checked August 16, 2026

What HTML Minifier does, with a checked example

HTML Minifier rewrites pasted HTML into a smaller source form by removing comments, tightening removable whitespace, and omitting tags whose absence is permitted by HTML. It targets bytes spent on authoring layout rather than the document's intended browser behavior. The common surprise is that whitespace is sometimes page content: a space between inline elements can separate visible words, while indentation between structural elements may be dispensable. The compact result is intended for delivery or storage, not easier editing, and should be tested as HTML rather than judged only by its shorter appearance.

Worked example

A concrete input and expected output from the current implementation.

Input

<!-- list note -->
<ul>
  <li>One</li>
  <li>Two</li>
</ul>

Expected output

<ul><li>One<li>Two</ul>

The ordinary comment and formatting whitespace disappear. Each li end tag is optional in this list structure: the second <li> starts a new item, and </ul> ends the final item.

How the result is produced

1

Context-sensitive whitespace

Whitespace is not uniformly disposable. Indentation between structural elements may be removed, but spaces that separate text around inline elements can affect the rendered words. Text inside whitespace-sensitive elements such as pre and textarea also needs preservation. The compact output therefore depends on HTML context, not simply on replacing every run of whitespace with nothing.

2

Comments and optional tags

An ordinary HTML comment is removed from the source. Optional tags follow separate grammar rules: only tags that HTML permits to be absent in their particular surroundings may be dropped. In the example, a following li implies that the previous item has ended, while the ul end tag implies the end of the final item. The </ul> tag itself remains required.

Good uses

  • Turning a pretty-printed generated page into compact HTML for a production release after testing the page's rendering and behavior.
  • Shrinking archived HTML snapshots or fixtures when comments and source indentation are not part of what the fixture must verify.
  • Comparing a document before and after structural minification to determine how much of its uncompressed size comes from markup formatting.

Limits and checks

  • Review spaces around inline elements such as a, span, and strong. Removing one separator can join visible words even when the element hierarchy still looks correct.
  • Inspect comments before removal. Template markers, build directives, and scripts that traverse comment nodes can make an otherwise invisible comment operationally significant.
  • Use the result as HTML, not XML or XHTML. HTML's optional-tag rules do not transfer to XML serialization, which requires explicit structure for non-empty elements.

Common questions

Will the minified document always behave exactly like the original?

Preserving browser behavior is the intended result, but equivalence is not guaranteed for every workflow. Test visible spacing, preformatted content, scripts that inspect the DOM, and template processing that occurs after minification. If another system depends on comments, byte offsets, signatures, or exact source text, the answer can be no even when the rendered page appears unchanged.

Why does the result contain elements without closing tags?

HTML permits particular end tags to be omitted only in defined contexts. A minifier can use those rules, so list items, paragraphs, or other eligible elements may look unfinished while an HTML parser still infers their boundaries. This does not mean arbitrary closing tags are removable, and the same shorthand must not be assumed valid for an XHTML or generic XML consumer.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools