Minify HTML online by removing the unnecessary whitespace and line breaks between elements. Text spacing, attributes, comments and embedded code are preserved, so the compact output is ready to copy into your project.
Minified HTML will appear here
Paste HTML in the input panel and it minifies automatically
Load sample data to get started
What this tool can do
Whitespace Removed
Indentation and line breaks between elements are stripped
Meaningful Spaces Kept
Spaces between text and inline tags survive minification
Pre & Textarea Exact
Whitespace-sensitive contents are copied character for character
Attributes Preserved
IDs, classes, URLs and quoting stay as written
Comments & Doctype Kept
HTML comments and the doctype pass through
Script & Style Untouched
Embedded JavaScript and CSS are copied verbatim
Copy & Download
Copy the minified source or save it as .html
Fragments Welcome
Partial HTML minifies without a full document
Methodology and approach
The minifier parses the markup and writes it back out without the indentation and line breaks between elements, while meaningful text spacing, attributes, comments, the doctype, pre and textarea contents, and embedded script or style code are preserved exactly as written.
HTML written by hand or exported from a CMS usually carries indentation, line breaks and blank lines that exist only so the source can be read. An HTML minifier removes that formatting overhead and writes the document back out on far fewer lines, while keeping the elements, the attributes and the text a visitor actually sees.
Minification is not validation. A minifier rewrites the whitespace around the markup; it does not check the document against the HTML rules. This tool parses the markup with an HTML-aware parser rather than deleting spaces with a pattern, so it knows the difference between indentation and a space that separates two words, between a tag and a less-than sign in plain text, and between markup and the JavaScript or CSS inside a script or style element.
Paste the HTML into the input panel on the left. It is parsed and minified automatically as you type, and Ctrl+Enter forces a re-run at any time. The minified markup appears in the output panel on the right, with a status line reporting how many elements were processed and how the UTF-8 size of the output compares with the input.
If the markup cannot be read - a start tag with no closing bracket, an attribute quote that is never closed - the output panel clears and reports the parser error with its line and column instead of showing stale output. Your input is left exactly as it was, so you can fix the problem in place and the minified document updates on its own.
When the result looks right, Copy puts the complete minified source on the clipboard, and Download saves it as a .html file. Everything runs in your browser; nothing is uploaded.
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<main>
<h1>Hello World</h1>
<p>This is a sample paragraph.</p>
</main>
</body>
</html><!DOCTYPE html><html><head><title>Example Page</title></head><body><main><h1>Hello World</h1><p>This is a sample paragraph.</p></main></body></html>The parser reads the document into a tree, and the minifier writes that tree back out without the whitespace between structural nodes: the indentation between a block element and its neighbor, the line breaks around a heading, the blank lines a formatter left behind. Runs of whitespace inside text collapse to the single space a browser renders, so the source shrinks without the words running together.
Not every space is formatting. Text mixed with inline elements keeps the spaces that separate it from those elements - "Hello <strong>world</strong>!" never becomes "Hello<strong>world</strong>!" - and the spaces inside a paragraph are collapsed rather than deleted. The result is the same set of words and the same rendered spacing, with only the source layout removed.
<p>
Hello <strong>world</strong>! Welcome to <a href="/about">our website</a>.
</p><p>Hello <strong>world</strong>! Welcome to <a href="/about">our website</a>.</p>Safe minification aims at the same rendered page: the same elements in the same order, with the same attributes and the same text. The whitespace it removes is the whitespace browsers collapse when they render - indentation beside block-level markup, line breaks between paragraphs of source.
Careless whitespace removal can change what a visitor sees, which is why this tool does not remove it indiscriminately. Spaces between text and inline tags are preserved, the contents of pre and textarea are copied character for character, and embedded JavaScript and CSS are never rewritten. Even so, minification changes the source, so a page whose layout depends on unusual whitespace - a stylesheet using white-space: pre in an unexpected place, for instance - is worth comparing before and after rather than assumed identical.
Yes. Attributes are copied through in source order with the quoting the document used: class="a b" keeps its double quotes, an unquoted value stays unquoted, and a boolean attribute such as disabled keeps its bare form. IDs, classes, URL attributes and data attributes are never rewritten or reordered.
HTML comments, the <!DOCTYPE> declaration and entity references such as are copied out verbatim rather than stripped. Conditional comments and comments that carry build instructions therefore survive minification exactly as they were written.
Removing indentation and line breaks reduces the byte size of the source, and the status line above the output reports the before and after UTF-8 sizes so you can see what this document actually saved. Heavily indented documents shrink noticeably; markup that is already compact has little left to remove, and closing tags that were missing from incomplete input are added back the way a browser adds them, which can occasionally make the output the same size or a little larger than the input.
Minification is one step among several. It reduces the HTML that travels over the wire, but it does not replace HTTP compression, caching, image sizing or the rest of a performance toolkit - it works alongside them.
The classic one is a space that was not merely formatting: text that touches an inline element, or two words that only look separated by a newline. This tool collapses runs of whitespace in text and keeps the spaces between text and inline tags, so words do not run together - but aggressive minifiers elsewhere do not always take that care.
Whitespace-sensitive elements are the second. pre, textarea and listing render every space, so their contents are copied through untouched here; a minifier that rewrites them will visibly change the page.
Embedded JavaScript and CSS are a third: their whitespace is not HTML formatting, and a newline inside a // comment or a string literal has meaning. This tool never rewrites script or style contents - it does not minify JavaScript or CSS here - so embedded code comes through intact.
The last is markup that cannot be read at all. An attribute value with no closing quote or a start tag with no closing bracket is reported with its line and column, your input is left untouched, and the output clears rather than showing a stale document next to text it does not belong to.
Preparing HTML source for production, where every unnecessary byte in the markup is one the visitor downloads. Reducing the whitespace a template engine or a static-site generator wrote into a page. Compressing an HTML fragment exported from a CMS before storing or delivering it. Reviewing the difference between formatted and compact source, to see exactly how much of a document is layout.
It is the wrong tool when the document needs to be checked against the HTML rules - that is validation, and the HTML Validator is the page for it - or when the goal is to make unreadable source readable again, which is minification's opposite and the HTML Formatter's job.
Common questions about this tool