Skip to main content

    HTML Minifier

    Minify HTML online. Collapse whitespace and strip comments without touching your tags, attributes, scripts, styles or preformatted text.

    Free to use. Runs in your browser.

    Paste HTML and click Minify. Safe mode collapses each run of ordinary whitespace to a single space and removes ordinary comments, while leaving your tags, attributes and the contents of script, style, pre and textarea byte for byte. If the result does not match your input it is discarded and your original is returned.

    Use it to create a smaller deploy copy while keeping your readable source HTML unchanged.

    Input HTML
    Mode

    Safe keeps every tag, attribute, script, style, pre and textarea exactly as they are, and never joins two words together.

    Minified Output

    Why Minify HTML?

    HTML minification removes whitespace and comments that browsers don't need. How much that saves depends almost entirely on how the file was written. Measured across 245 real framework-rendered pages, Safe mode saved a median of 1.0%, because on those pages the tags themselves are 57% of the bytes and script and style content is another 30%. Hand-written HTML is the opposite: a two-space-indented page saved 25%, and a four-space page carrying comments saved 53%.

    Is it the biggest performance win? No — that is images, JavaScript and CSS. And if your HTML comes out of a framework it may already be about as small as it is going to get. Minify by all means, but check the before and after figures rather than assuming a percentage.

    Paste your HTML and get a minified version. Safe mode removes ordinary comments and collapses each run of ordinary whitespace in text to a single space — never to nothing, so two words can never be joined. Start tags and attributes are returned byte for byte, and the contents of script, style, pre, textarea, xmp and listing are untouched, as is any element that declares preformatting through a style attribute or a whitespace-pre utility class. Before anything is shown, the result is compared against your input; if a tag, an attribute, a protected region or a visible word has changed, the result is discarded and your original comes back with no saving reported.

    What Minification Removes

    ElementBeforeAfterSavings
    Indentation<div> <p>Hello</p> </div><div> <p>Hello</p> </div>Most of the saving on hand-written HTML
    Ordinary comments<!-- Navigation -->(removed)Varies with the source
    Conditional comments<!--[if IE]>...<![endif]-->(kept)None, by design
    Tags and attributes<div class = "a" >(unchanged)None, by design
    script, style, pre, textarea(any content)(unchanged)None, by design
    style="white-space:pre" or class="whitespace-pre-line"(any content)(unchanged)None, by design

    What this means for you: Safe mode is built so the page still renders the same, and it hands your original back rather than show you a result that does not match. Aggressive mode additionally removes the whitespace between tags, which is meaningful between inline elements such as spans and links, so it can change how a page looks — use it only where you know the markup is not whitespace-sensitive. If you need to read the output later, the HTML Beautifier will re-format it.

    Performance Optimisation Beyond Minification

    Enable Gzip/Brotli compression

    Server-side compression reduces HTML by 60-80%. Minification + Brotli together can reduce a 100KB page to under 10KB over the wire. Most CDNs and web servers support this with one config line.

    Inline critical CSS

    Put above-the-fold CSS directly in <style> tags to avoid render-blocking requests. The page renders instantly while the full stylesheet loads asynchronously. This is the #1 LCP optimisation.

    Defer non-critical scripts

    Add defer or async to <script> tags that aren't needed for initial render. Analytics, chat widgets, and social media embeds can all load after the page is interactive.

    Remove unused code

    Unused CSS and JavaScript are a bigger problem than unminified HTML. A single unused 50KB JS bundle dwarfs any HTML whitespace savings. Audit with Chrome DevTools Coverage tab.

    Typical HTML Page Sizes

    Page TypeRaw HTMLMinifiedMinified + Gzip
    Simple landing page15 KB11 KB3 KB
    Blog article30 KB22 KB7 KB
    E-commerce product page80 KB55 KB15 KB
    Dashboard (heavy tables)200 KB140 KB30 KB
    Average web page50 KB35 KB (30%)10 KB (80%)

    HTML is rarely the performance bottleneck — images and JavaScript are far larger. Minification is worth doing, but "zero risk" is the wrong way to think about it: most minifiers parse your HTML into a document and write it back out, which quietly discards anything that does not fit the model, such as the unmatched closing tags at the top of a footer partial. This one uses a specification-conformant tokenizer to find where things start and end, then copies every byte it emits out of your original source rather than rebuilding it, which is why fragments come back intact.

    Worked Example: Shipping a Static HTML Page

    Priya has a static product page with hand-written HTML. The readable source is 42 KB because it has comments, indentation, and clear sections for header, features, FAQ, and footer.

    1. Keep source readable

    She keeps index.html readable in version control so future edits are easy to review.

    2. Create a deploy copy

    She pastes the HTML into the minifier and saves the output as the published file. Comments and indentation are removed from that copy only.

    3. Test whitespace-sensitive blocks

    The page has a small code example inside pre. She checks that it still displays correctly after minification.

    4. Check the network panel

    In DevTools, the minified HTML is smaller before compression and smaller again after brotli or gzip is applied by the host.

    HTML Minification Edge Cases

    pre and code blocks

    Whitespace can be meaningful inside code examples, poetry, command output, and ASCII diagrams. Test those areas after minifying a content-heavy page.

    textarea defaults

    Textarea content is visible to the user and may preserve spacing. Avoid collapsing intentional spaces inside form examples or templates.

    Email conditional comments

    Old Outlook email templates can rely on conditional comments. If you are minifying HTML email, test the output in the clients you support.

    Inline scripts and styles

    This page focuses on HTML whitespace. Minify complex inline CSS and JavaScript with tools made for those languages when size matters.

    What to Test After Minifying

    AreaCheckWhy
    LayoutHeader, nav, main content, and footer still renderA pasted partial document can lose context when reused
    LinksInternal anchors and external links still workHTML minification should not change href values
    FormsLabels, required fields, and submit buttons still behaveForms often expose subtle markup errors quickly
    Structured dataJSON-LD script blocks remain valid JSONWhitespace is safe, but broken script tags are not
    Analytics and embedsThird-party snippets still load in the browser consoleSome snippets are sensitive to copied or trimmed code

    Safe HTML Minification Workflow

    • Keep a readable source file. Comments and indentation help future edits, so treat minified HTML as output.
    • Minify after template rendering. If a CMS or build step injects content, minify the final output rather than a half-rendered template.
    • Check generated source in a browser. Use View Source or DevTools to confirm the minified file is the one being served.
    • Pair it with compression. Minification reduces source bytes. Gzip or brotli reduces transfer bytes.
    • Avoid editing the minified copy. If you need a change, edit the readable source and create a fresh minified output.

    Related Tools

    How to use this tool

    1

    Paste your HTML into the input area

    2

    Click Minify HTML to compress

    3

    Copy the result or download as a .min.html file

    Common uses

    • Reducing HTML file size for production deployment
    • Stripping comments from HTML before going live
    • Preparing HTML email templates for sending
    • Optimising static HTML pages for faster load times

    Share this tool

    Frequently Asked Questions

    What does HTML minification do?
    In Safe mode it removes ordinary HTML comments and collapses each run of ordinary whitespace in text down to a single space. It never removes that whitespace entirely, so words cannot be joined together. Your start tags and attributes come back byte for byte, and the contents of script, style, pre and textarea are left exactly as they were. How much you save depends almost entirely on how the source was written: see 'How much space will I save?' below.
    Will minification break my page?
    Safe mode is built not to. A browser already renders any run of whitespace as a single space, so collapsing a run to one space changes nothing on screen, and pre, textarea, script and style are protected by name. Before showing you anything, the tool compares the result against your input: if a tag, an attribute, a protected region or a single visible word has changed, it discards the result, gives you your original back and reports no saving. It also protects any element that declares preformatting on the tag itself, whether through a style attribute or a utility class such as whitespace-pre-line. What it cannot see is an external stylesheet that sets white-space on a selector, because that is not in the HTML you pasted — if your layout depends on that, check the result before deploying. Aggressive mode is a different matter and is described below.
    Is my HTML sent to a server?
    No. The whole thing runs in your browser. Nothing is uploaded, stored or transmitted, so it is safe for production HTML containing sensitive data.
    Should I minify HTML for production?
    Yes, it's free performance. Minified HTML loads faster, parses quicker, and uses less bandwidth. Combined with Gzip/Brotli compression, a 100KB page can shrink to under 10KB over the wire.
    What's the difference between minification and compression?
    Minification removes unnecessary characters from the source code itself. Compression (Gzip, Brotli) encodes the file more efficiently for transfer. They're complementary, minify first, then compress. Together they reduce size by 70-90%.
    Does minification remove my CSS and JavaScript?
    No, and it does not reformat them either. The contents of every script and style element are copied across untouched, character for character, so a line comment cannot swallow the code beneath it and a nested CSS rule cannot be dropped. For minifying the CSS or JavaScript itself, use dedicated tools.
    Can I undo minification?
    Not exactly, comments are permanently removed. But you can re-format the structure using an HTML beautifier to restore readable indentation. That's why you should always keep your source files unminified and only minify for production.
    How much space will I save?
    It depends far more on how the HTML was written than on the tool. Measured across 245 real framework-rendered pages, Safe mode saved between 0.2% and 6.1%, with a median of 1.0% — on those pages 57% of the bytes are tags, which are preserved exactly, and another 30% is script and style content, which is protected. Hand-written HTML with indentation and comments is a different story: the same mode saved 25% on a two-space-indented page and 53% on a four-space page with comments. The tool shows you the exact before and after sizes, which is the only number that matters for your file.
    Should I minify HTML or just rely on Gzip?
    Both. Gzip compresses what's there, but it still has to process every byte. Fewer bytes in means fewer bytes to compress, so minification before compression usually gives a smaller output.
    Does Google care about minified HTML?
    Google doesn't directly rank minified vs unminified HTML. But minification improves page load speed, which is a ranking factor via Core Web Vitals. Faster pages also get better user engagement, which indirectly helps rankings.
    What about whitespace in pre tags?
    The contents of pre and textarea are preserved exactly, including indentation, line breaks and tabs, and so are script and style. This holds even for the odd cases: a self-closed <pre/>, an uppercase <PRE>, and a near-miss closing tag such as </press> inside the block. Elements that declare preformatting on the tag itself are protected too, through either a style attribute or a utility class such as whitespace-pre-wrap. The gap that remains is an external stylesheet setting white-space on a selector, which is not information the pasted HTML carries.
    Can I minify HTML automatically in my build?
    Yes. Vite uses html-minifier-terser by default in production builds. Webpack has HtmlWebpackPlugin with minify options. Most current build tools handle this automatically, this online tool is for quick one-off jobs.
    Should I minify HTML email?
    You can, but test the email after minifying. Some email clients are sensitive to table layout and conditional comments, so keep a readable source template and only send the tested minified copy.
    Does minification remove analytics scripts?
    No. Script tags and inline JavaScript stay in the document. This tool removes comments and whitespace around markup, not the functional code inside a script tag.
    Can minified HTML still be crawled?
    Yes. Search engines and browsers parse minified HTML normally. Minification changes the text layout in the source file, not the elements, links, headings, or structured data the crawler reads.
    What should I preserve before minifying?
    Keep the original source file, especially if it contains comments, template hints, or readable spacing around email tables. Minified output is a deploy artefact, not the file you should edit later.
    Can I minify a single HTML component?
    Yes. You can paste a full document, a component fragment, or a single repeated block. Just make sure the result is used in the same context as the original snippet.
    What happens to conditional comments?
    This tool removes HTML comments. If you are working with old Outlook email conditional comments, keep the readable source and test before removing them.

    Results are for general informational purposes only and should be checked before use. They are not professional advice. See our Disclaimer and Terms of Service.