💻 Coding

HTML Minifier

Paste HTML and get a compressed version instantly, comments and extra whitespace stripped, pre/textarea/script/style content left untouched.



    
0 B
Original Size
0 B
Minified Size
0%
Reduction

About the HTML Minifier

Shrinking your markup means stripping HTML comments and collapsing redundant whitespace, but the contents of <pre>, <textarea>, <script>, and <style> tags are left completely alone so meaningful spacing and code inside them survive intact. Conditional comments like <!--[if IE]> stick around on purpose, since some browsers still depend on them.

The whole process happens right in your browser, so nothing you paste in ever leaves your machine.

A simpler regex approach, deliberately, unlike the site's HTML Formatter

This tool's sibling, the HTML Formatter, runs a full tag-aware tokenizer that tracks nesting depth and nested elements to reprint markup with correct indentation. Minification is a narrower problem, all it needs to do is protect a handful of special blocks and generically collapse whitespace elsewhere, so this tool uses a more targeted set of regular expressions instead of building a complete parse tree. That's a deliberate scope match: parsing arbitrary nested HTML in general genuinely does need a real tokenizer (regex alone famously can't reliably handle arbitrarily nested structure), but finding "everything between this specific opening tag and its matching closing tag" and then collapsing whitespace around what's left is squarely within what a well-constructed regex can do reliably.

How the backreference keeps mismatched tags from corrupting the output

The regex that protects pre, textarea, script, and style content, <(pre|textarea|script|style)(\s[^>]*)?>[\s\S]*?<\/\1>, captures the tag name in a group and then reuses that exact captured name via \1 when matching the closing tag. That backreference guarantees a <script> block only gets closed by an actual matching </script>, never accidentally by an unrelated </style> that happens to appear nearby, which would otherwise scramble which content gets protected and which gets minified.

Why whitespace between tags collapses to one space, not zero

The tool replaces runs of whitespace between tags with a single space (> <) rather than removing that whitespace entirely (><). This distinction matters for how a page actually renders: HTML treats whitespace between inline elements as visually significant, <span>A</span> <span>B</span> renders with a visible gap between A and B, but stripping that space entirely would visually merge them together with no gap at all. Keeping exactly one space is the safe middle ground, it still removes the bulk of excess formatting whitespace while never accidentally changing how inline content is displayed.

Why conditional comments get rescued before the general comment-stripper runs

Conditional comments (<!--[if IE]> ... <![endif]-->) were a legacy mechanism that let developers target markup specifically at old versions of Internet Explorer, since removed from modern browsers but still present in some codebases maintaining compatibility with older systems. This tool's minifier stashes any conditional comment block safely aside using a dedicated pattern before its general "strip every comment" regex runs, so a genuinely functional conditional comment survives the minification pass intact instead of getting deleted along with ordinary, purely explanatory <!-- notes --> comments.

Two separate cleanup passes for two different kinds of extra whitespace

Horizontal whitespace between tags and vertical whitespace from blank lines are handled as two distinct steps. Inter-tag spacing gets collapsed to a single space as described above, while a separate pass splits the text into lines, trims each one, and discards any line that's now empty, removing the extra vertical blank lines that indentation-heavy source HTML often accumulates. Treating these as separate problems keeps each regex focused and easier to reason about, rather than trying to handle both spacing directions with one overly clever pattern, and it's why the output stays multi-line and readable rather than collapsing into one dense unbroken string.

Frequently Asked Questions

Why does this minifier use regex instead of a full HTML parser?

Minification only needs to protect a few special blocks and generically collapse whitespace elsewhere, a narrower task than full parsing. A targeted set of regular expressions handles this reliably, while the site's HTML Formatter uses a complete tag-aware tokenizer since it needs to track nesting depth for indentation.

How does the tool make sure a script block doesn't get closed by an unrelated style tag?

The preservation regex captures the tag name and reuses that exact captured value as a backreference when matching the closing tag, guaranteeing a script block is only closed by an actual matching /script tag, never accidentally by a nearby, unrelated closing tag.

Why is whitespace between tags collapsed to one space instead of removed entirely?

HTML treats whitespace between inline elements as visually significant, removing it entirely could merge adjacent inline content together with no visible gap. Collapsing to exactly one space removes excess formatting whitespace while never altering how inline content actually renders.

Will minifying delete conditional comments like <!--[if IE]>?

No, conditional comments are specifically detected and protected before the general comment-stripping step runs, since some legacy codebases still rely on them for targeting older browsers. Only ordinary, purely explanatory comments get removed.

Does this minifier merge blank lines and inter-tag spacing with the same logic?

No, they're handled as two separate passes. Inter-tag whitespace collapses to a single space, while a separate step trims each line and discards ones that are now empty, removing accumulated vertical blank lines from indentation-heavy source HTML.

Does any of my pasted HTML get sent to a server?

No, the entire minification process runs locally in your browser using JavaScript regular expressions. Nothing you paste is uploaded anywhere.