HTML Minifier

Minify HTML online: remove comments and collapse extra spaces and line breaks, keeping <pre>, <textarea> and <script> content intact. In your browser.

Runs in your browser. Files never leave your device.
6 words · 70 chars

About the HTML Minifier

This HTML minifier deletes <!-- comments --> and squeezes every run of spaces, tabs and line breaks down to a single space. What you get back is compact markup, usually on one line: a lighter page, a smaller embed code, or a snippet short enough for a character-limited field.

Paste your markup and the minified HTML is ready in the result box immediately. Tags, attributes and visible text are left as they are, and the parts where whitespace matters, <pre>, <textarea> and <script>, are copied untouched, so the page looks and works the same in a browser while carrying less dead weight.

How the HTML is minified

The minifier reads the markup from start to end, telling text, tags and comments apart. Every HTML comment, from <!-- to the next -->, is removed, even when it runs over several lines. In the text between tags, each stretch of spaces, tabs and new lines becomes one ordinary space. Inside a tag, the spaces between attributes are reduced the same way, but anything inside quotes, such as alt="two spaces", is kept exactly.

A single space is kept wherever whitespace used to be, including between tags. That is deliberate: in HTML, a space between two inline elements such as <a> or <span> can show up on screen, and removing it outright could glue words together. Collapsing it to one space keeps the layout while still removing indentation and blank lines, which is where most of the saving in hand-written HTML comes from.

Some elements are handled separately. The contents of <pre> and <textarea> are copied as they are, because their spaces and line breaks are visible. Inline <script> code is copied as it is too, so // comments and statements without semicolons keep working. The CSS inside <style> is minified with the same rules as the CSS Minifier. A non-breaking space character is never collapsed.

Assumptions and limits

  • Nothing beyond whitespace and comments is rewritten: optional closing tags, attribute quotes and boolean attributes are untouched, and no tag is removed.
  • Because inline scripts and <pre> blocks keep their line breaks, a page that contains them won't come out on a single line. Move scripts into separate .js files if you need one line.
  • All comments are removed, including old Internet Explorer conditional comments and comments used as markers by some CMS plugins or templates.
  • If the input contains no HTML tags at all, a warning is shown in place of the result.

Worked examples

Minify HTML for a navigation menu

A six-line list with indentation and a closing comment is 83 characters. Minified, it is one line of 56 characters, about a third smaller. The comment is gone and each <li> is separated by a single space.

Input:
<ul>
  <li>Home</li>
  <li>About</li>
  <li>Contact</li>
</ul>
<!-- end of menu -->

Output:
<ul> <li>Home</li> <li>About</li> <li>Contact</li> </ul>

Extra spaces inside text

Spaces typed for alignment collapse to one, which matches what the browser was already displaying, because HTML shows several spaces in a row as one.

Input:  <p>Price:   <strong>$20</strong></p>
Output: <p>Price: <strong>$20</strong></p>

Compress HTML for an embed code or widget

A pricing card of 106 characters, written over five lines with a header comment and four-space indentation, becomes a single 76-character line (about 28% smaller) that can be pasted into a CMS custom-HTML block, an ad slot or a JSON field without stray line breaks.

Input:
<!-- pricing card -->
<div class="card">
    <h3>Pro plan</h3>
    <p class="price">$12 / month</p>
</div>

Output:
<div class="card"> <h3>Pro plan</h3> <p class="price">$12 / month</p> </div>

A code sample and a style block

The <pre> block keeps its indentation and line break, while the <style> block is minified as CSS.

Input:
<style>
  .code { color : #333; }
</style>
<pre>if (x)
    run();</pre>

Output:
<style>.code{color:#333;}</style> <pre>if (x)
    run();</pre>

What is removed and what stays

In your HTMLAfter minifying
<!-- comments -->, single or multi-lineRemoved
Indentation and blank linesRemoved
Line break or several spaces between tagsOne space
Spaces inside visible textCollapsed to one
Tags, attributes, quoted attribute valuesUnchanged
Content of <pre>, <textarea> and <script>Copied as it is
CSS inside <style>Minified as CSS

How to use the HTML Minifier

  1. Paste your HTML. Put the markup in the Input Text box; the counter below it shows how many characters the original has.
  2. Check the compact result. The Result box shows the minified code, with comments gone and whitespace reduced to single spaces.
  3. Copy and use it. Press Copy, then paste the compact HTML into your template, email platform or embed field.

Frequently asked questions

Does minifying HTML make a website faster?

A little. Each HTML file gets smaller, so there is less to download. Pages with deep indentation and many comments gain the most. Images, scripts and fonts are usually far larger than the HTML itself, so minifying is a small, cheap win rather than a fix for a slow site.

Why are there still spaces between my tags?

Because each block of whitespace is reduced to one space rather than removed. A space between inline elements such as links, buttons or bold text can be visible, and keeping one avoids words running together while still cutting out indentation and new lines.

Will minified HTML break my inline JavaScript?

No. The code inside <script> tags is copied exactly as you wrote it, line breaks and // comments included, so it runs the same. The flip side is that inline scripts are not made any smaller; use a JavaScript minifier on them separately if size matters.

Can I minify a full HTML page with the head and body?

Yes. Paste the whole document, including <!DOCTYPE html>, <head> and <body>. Everything is treated the same way, and <pre>, <textarea> and <script> blocks keep their layout wherever they appear.

Can I turn minified HTML back into readable code?

Comments that were removed can't be recovered, but the layout can. Paste the minified code into the HTML Beautifier on VelixaKit to indent the tags by nesting again, or keep your original file as the version you edit.

Is the HTML I paste stored?

No. Minification happens in your browser while you type; the code isn't uploaded, logged or kept after you close the page, so unreleased templates and client markup stay on your computer.

This tool runs entirely in your browser. What you enter or upload stays on your device and is never sent to our servers.