ToolsHub4u

Remove comments and extra whitespace from HTML

Remove comments and repeated whitespace from HTML. Attribute values, pre blocks and scripts are left exactly as written, and conditional comments survive.

  • Preserves raw blocks
  • Exact size saving
  • Runs locally
Ready Developer & Utility
1Enter
2Adjust
3Copy result

HTML Minifier

1

HTML input

Options

Conditional comments used by old versions of Internet Explorer are always kept, even when comments are removed.

No data yet Live
2

Minified HTML

Reduced 258 to 180 characters (30.2% smaller).

Characters saved

78

Minified HTML

  • Raw blocks were protected0 pre, textarea, script or style blocks kept exactly as written.
  • Whitespace between tags removedRemove it only when the markup does not rely on inline-block gaps. A space between two inline elements renders as a gap.
  • Text whitespace collapsed to one spaceCollapsing matches normal HTML rendering. It does not remove the space between words.
Before
258 chars
After
180 chars
Saved
30.2%
Comments removed
1

This is a conservative minifier. It does not rename classes, remove optional tags, inline resources or rewrite attributes. Check the rendered page before replacing a live template.

This page removes optional characters from HTML: comments, whitespace between tags and repeated whitespace inside text. Three checkboxes control which of those changes are allowed, and the result area reports the before and after size so you can see whether the option set actually saved anything.

It scans the document into four kinds of part — tags, text, comments and raw blocks — and edits each kind with its own rule. That is not a full HTML parse, but it is enough to keep an attribute value such as title="two spaces" from being mistaken for text. It does not build a DOM tree, rename classes, or touch the JavaScript and CSS inside script and style.

What the three options do

Remove comments deletes ordinary HTML comments, including licence notices and analytics placeholders. Conditional comments are kept, because they are instructions to old versions of Internet Explorer rather than notes to a reader. If you still ship a conditional comment, this is the behaviour you want, and the stats count only the ordinary comments that went.

Remove whitespace between tags deletes whitespace that sits between two markup parts and contains nothing else. That space is usually insignificant, but not always: two inline-block elements separated by a newline render with a visible gap, and removing it closes that gap. The checklist warns about this, which is the honest position; the tool cannot know whether your layout depends on it.

Collapse text whitespace turns a run of spaces, tabs and newlines inside text into one space. This matches how HTML renders text by default, so a paragraph with three spaces between words looks the same before and after. It does not remove the single space between words, so the output stays readable.

A worked example

The page's 258-character sample has one comment, a newline and two-space indent between every tag, and a paragraph with single spaces. Minified with all three options on, it came back as 180 characters, 30.2% smaller, with one comment removed.

Minification results for the same 258-character document
OptionsAfterSaving
Comments, between tags, text18030.2%
Between tags only21018.6%
Text only22214.0%
Comments only22811.6%
None2580%

The all-options run removed the placeholder comment, the newlines and indentation between tags, and the repeated whitespace in text. The text inside strong kept its single spaces, and the paragraph stayed on one line, which is what the browser would render anyway.

The single-option rows are worth reading together. Between-tags alone saved the most, because the sample is indented on every line. Text alone collapsed each newline-and-indent run to one space rather than deleting it, so it kept 222 characters: the spaces still separate words and tags. Comments alone saved 30 characters, exactly the placeholder plus its delimiters.

Text collapsing is easier to see on messy input. A 56-character block with blank lines and runs of spaces inside a paragraph came back as 41 characters, 26.8% smaller, with the sentence intact. Turning on between-tags as well brought it to 39 characters by dropping the spaces left around the block elements.

Check inline-block layoutsIf two buttons or cards rely on the whitespace between their tags, removing that whitespace closes the gap. Keep the between-tags option off for those pages.

Attributes, comments and raw blocks

Attribute values are never rewritten. A test element written as <p title="two spaces" data-json='{"a": "b"}'> came back byte for byte, still 52 characters. The same applies to a style attribute such as style="margin: 0 auto" and to an attribute containing >: the scanner follows quotes, so a greater-than sign inside a value does not end the tag.

An ordinary comment is removed entirely, including the delimiters. A conditional comment such as <!--[if IE]>.<![endif]--> is preserved as one block, and a test with one conditional and one normal comment removed only the normal one, reporting 44 characters from 59. That is a deliberate exception, not an oversight.

pre, textarea, script and style are protected. Their contents are copied exactly, because collapsing whitespace inside a pre would change the displayed code and collapsing it inside a textarea would change its default value. Someone else's JavaScript is also not this tool's business to reformat. A test with an indented script kept its comments, braces and spacing unchanged and only shortened the paragraph after it.

Because a raw block is one part, the between-tags option now works across its boundary. In the test, <pre> keep\n this </pre>\n<p> collapse this </p> lost the whitespace after the closing pre and kept both the preformatted text and the collapsed paragraph. An earlier version left a stray space there.

Protection also means those blocks are not minified. A large inline script or a style block keeps every space and comment, so the overall saving on a page with a lot of inline code will be smaller than the markup alone suggests. Minify the CSS and JavaScript separately if that matters.

What it will not do

It does not remove optional quotes around attribute values, does not shorten boolean attributes, and does not drop the optional closing tags that HTML allows. Those transformations save bytes but require a parser and a clear understanding of the document, and they can break naive string processing.

It does not touch inline event handlers, inline styles or the contents of script and style. It does not remove empty elements, merge adjacent text nodes or reorder attributes. And it does not validate: a malformed document goes in and a malformed document comes out, only shorter.

Text is edited only where it is genuinely text. A stray < in a sentence that never forms a tag is left as written rather than guessed at, and a comment inside a script block is left inside the script because the block is copied whole. Neither case is a loss: the output stays valid and nothing is silently moved.

The practical result is a modest reduction, typically 10-30% on hand-written markup with comments and indentation, and close to zero on markup that is already compact. That is not a failure. The page removes only characters that are safe to remove without understanding the document, and it reports zero when there is nothing to remove.

Checking before you deploy

Compare the rendered page, not only the character count. The two risks are the inline-block gap and a comment that a script reads from the DOM. A comment used as a template or a marker will be gone, and the script that looks for it will fail silently.

Keep the original file. Minification is one-way in practice, and comments that documented a decision are not recoverable from the output. The HTML beautifier can make the result readable again, but it cannot bring the comments back. For a structural comparison, paste both versions into the code diff checker.

If you need a real build-step minifier, use one that parses HTML and can also process the linked CSS and JavaScript. This page is for a quick reduction on a snippet, an email template or a one-off page where adding a build step is not worth it.

Questions

Common questions

Short answers about this tool and its limits.

Does it change the spaces inside attribute values?

No. Attribute values are copied exactly, so title="two spaces", a JSON data- attribute and style="margin: 0 auto" all keep their spacing. Only text between tags is collapsed.

Are conditional comments removed?

No. Comments wrapped in [if IE] are preserved even when the remove-comments option is on. Ordinary comments are deleted. A test with one of each removed only the ordinary one.

Does it minify JavaScript or CSS?

No. script and style contents are protected and copied exactly, and the checklist reports how many raw blocks were kept. Use the JavaScript minifier and CSS minifier for those, then paste the results back.

Can removing whitespace break my layout?

Yes, in one common case: inline-block elements separated by a newline render with a gap, and removing that whitespace closes it. Keep the between-tags option off if your layout depends on the gap.

Does it validate the HTML?

No. It treats the input as text and removes characters. A malformed document stays malformed. Run a validator if correctness matters.

Why is the saving smaller than other minifiers?

Because it only removes comments and optional whitespace. It does not rewrite attributes, drop optional closing tags or process inline code. A parser-based build tool will usually save more.

Does the between-tags option work next to a pre or script block?

Yes. The whitespace between a raw block and the next tag is removed while the block itself is copied whole. A test with <pre> followed by <p> lost the gap and kept the preformatted text unchanged.

Is my HTML uploaded?

No. Minification runs in the browser with no account and no server copy. The result disappears when you close the tab unless you copy or download it.