DevTools Logo

HTML Minifier

HTML Minifier

Strip comments and collapse whitespace while preserving pre/code/script

Input

About

This tool minifies HTML documents by removing comments and collapsing whitespace between tags, while preserving the content of <pre>, <textarea>, <script> and <style> elements. Use it to shrink static HTML before deploy or to trim pasted markup. Minification runs entirely in your browser; nothing is uploaded.

    Examples

    Minify a page with a code block

    Input
    <div>
      <!-- a comment -->
      <h1>Hello</h1>
      <pre>
        code
      </pre>
    </div>
    Output
    <div><h1>Hello</h1><pre>
        code
      </pre></div>

    Comments are stripped, inter-tag whitespace is collapsed, and the indentation inside <pre> is preserved exactly.

    Strip redundant attribute quoting

    Input
    <a href="/x" target=_blank rel="noopener noreferrer">Link</a>
    Output
    <a href=/x target=_blank rel="noopener noreferrer">Link</a>

    Quotation marks are removed from attribute values that don't contain whitespace or special characters, shaving bytes without changing browser behavior.

    About this tool

    Paste markup and the minifier removes what browsers ignore anyway: HTML comments (IE conditional comments survive) and runs of whitespace between tags, collapsed to single spaces. Content that must not change — <pre>, <textarea>, <script> and <style> blocks — is set aside first and restored after, so code samples, inline scripts and stylesheets come through byte for byte. A sample: an indented div holding a paragraph and one comment returns as a single-line <div><p>Hi</p></div>.

    Each run reports input size, output size, bytes saved and a savings percentage, so the gain is measured, not guessed. Minification also pairs well with gzip: compression squeezes repetition it can model, while this pass deletes characters outright, so a minified page still ends up smaller after compression — the bytes removed up front are ones no encoder can recover.

    Scope is markup only: code inside script and style blocks is never rewritten, by design. Run stylesheets and scripts through the dedicated CSS/JS minifier and paste the optimized code back into the page.

    How to use this tool

    1. Paste your markup

      Drop HTML into the input area, upload a file, or load a built-in sample.

    2. Pick the options

      Keep comment removal and whitespace collapse on for the tightest output, or toggle either off.

    3. Minify and review

      Run the minifier and compare input and output using the bytes-saved readout.

    4. Copy or download

      Copy the result to the clipboard or download it as minified.html for deployment.

    Use cases

    Shrinking production HTML

    Cut kilobytes from generated HTML before serving it, especially on content-heavy pages where the savings compound across impressions.

    Sanitizing pastes before commit

    Strip developer comments and excess whitespace from hand-edited templates so committed markup stays clean in version control.

    Preparing email HTML

    Reduce file size of HTML email bodies to avoid clipping by client-side size limits and improve mobile rendering speed.

    Common mistakes

    Mistake:Trusting minified output to be byte-identical except for whitespace.

    Fix:Safe minifiers are conservative — they may collapse quotes, drop optional slashes, or remove boolean attributes. Always sanity-check the result before deploying.

    Mistake:Applying minification to a debug build.

    Fix:Minify only the production build. Source maps and readable markup matter when investigating real bugs, so keep the unminified version for development.

    Mistake:Assuming whitespace inside arbitrary tags is preserved.

    Fix:Only <pre>, <code>, <textarea>, <script>, <style> and CDATA are protected. Whitespace inside <p> or <div> is collapsed by design — that's the point.

    Frequently asked questions

    Will minifying HTML break my layout?

    Rarely. Collapsing inter-tag whitespace leaves a single space, which is how browsers read the source anyway, and <pre> and <textarea> contents — where spacing is significant — are preserved untouched. Pages with unusual inline spacing deserve a quick visual check before deploy.

    Does it minify inline CSS and JavaScript?

    No. Script and style contents are protected and restored exactly as written, so inline code keeps working. Minify stylesheets and scripts with the dedicated CSS/JS minifier, then paste the compacted code back.

    How much smaller will my HTML get?

    It depends on the source: comment-heavy, indented markup with blank lines shrinks the most, already-compact pages only a little. Input bytes, output bytes and the savings percentage are shown after every run, giving an exact figure for your document.

    Is minifying still useful with gzip?

    Yes — the steps compound. Compression squeezes repetition it can model, while minification deletes characters outright; the combination ends up smaller than gzip alone. The relative gain shrinks, but bytes removed before encoding stay removed.

    References & standards