HTML Formatter & Beautifier

Paste any HTML and instantly format it with 2-space or 4-space indentation, or minify it to remove whitespace and comments. Handles nested elements, inline tags, pre/script/style blocks, and more. Everything runs in your browser.

How It Works

  1. 1

    Paste your HTML into the left panel — minified, unindented, or any mix

  2. 2

    Choose Format or Minify mode, then pick your indent size (2 or 4 spaces) and type (spaces or tabs)

  3. 3

    The formatted output appears instantly on the right — copy it or download as an .html file

When To Use This Tool

  • When you've copied HTML from a browser DevTools inspector and need to read it

  • When a legacy template or CMS output is all on one line and impossible to debug

  • When you need to minify HTML before deploying to reduce page size

  • When reviewing a diff between two HTML files — formatted HTML makes diffs readable

  • When converting tabs to spaces (or vice versa) in an HTML file to match your project's style guide

Frequently Asked Questions

Complete Guide to HTML Formatting and Minification

Why HTML formatting matters

HTML structure maps directly to the DOM tree, and unformatted HTML hides that structure. When all elements are on one line or indentation is inconsistent, it is hard to see parent-child relationships, spot unclosed tags, or understand nesting depth. Consistent indentation is especially important in template languages (Jinja2, Handlebars, Blade) where the indentation of template tags must match the indentation of the HTML they emit. Formatted HTML is also far easier to diff when reviewing changes in version control — a one-line minified file shows the entire file as changed when any part changes.

Block vs inline elements: the formatting rule

The central decision in HTML formatting is whether an element gets its own line or stays inline with surrounding content. Block elements (div, p, section, article, h1-h6, ul, ol, li, table, tr, td, form, fieldset, blockquote, pre) get their own lines with the opening tag, content, and closing tag each on a separate line if the content is itself a block element. Inline elements (span, a, strong, em, code, b, i, u, label, abbr, cite) stay on the same line as their surrounding text or other inline elements. Mixing them incorrectly can introduce visual whitespace — a newline between two inline elements in HTML creates a space character in the browser output.

Void elements and self-closing tags

Void elements in HTML5 cannot have children and must not have closing tags: area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr. Writing <br></br> is invalid HTML. Writing <br /> (with XHTML-style self-closing slash) is valid in HTML5 but the slash is optional and ignored. In SVG and MathML embedded in HTML, the self-closing syntax is significant. When formatting, void elements are placed on their own line at the current indent level without increasing the indent depth.

Pre, script, and style: whitespace-sensitive blocks

Three HTML elements require their content to be preserved verbatim: pre (preformatted text, whitespace is rendered literally), script (JavaScript code with its own indentation conventions), and style (CSS code). A formatter must detect these blocks and pass their content through unchanged. Reformatting a pre block would change what is displayed in the browser. Reformatting script content with HTML indentation would corrupt the JavaScript (e.g., template literals that include newlines). These blocks should be formatted with language-specific tools: prettier --parser babel for JavaScript, prettier --parser css for CSS.

HTML minification for production

Production HTML delivery benefits from minification primarily through reduced file size and consequently faster TTFB and FCP. The gains vary: a document with many comments and generous indentation may shrink 20-30%; a tightly written template may only shrink 5-10%. Minification is most impactful for server-rendered pages where the full HTML is sent on every request. Single-page apps (React, Vue, Angular) typically have a small index.html and do most of their rendering client-side, so HTML minification is less critical. Build tools like webpack, Vite, and Next.js apply HTML minification automatically in production builds.

EditorConfig and consistent formatting across teams

To maintain consistent HTML indentation across a team without manual formatting, use .editorconfig files. A typical web project configuration: [*.html] indent_style = space, indent_size = 2, end_of_line = lf, charset = utf-8, trim_trailing_whitespace = true, insert_final_newline = true. EditorConfig is supported natively by many editors (VS Code, JetBrains IDEs) and via plugins in others. Combine with a formatter like Prettier (which supports HTML via --parser html) in a pre-commit hook to enforce formatting automatically.

Debugging HTML structure issues

Common HTML structure bugs that formatting helps diagnose: unclosed tags (a missing </div> causes everything after to nest inside that div), incorrect nesting (a block element inside an inline element is invalid, e.g. a <div> inside a <span>), duplicate IDs (multiple elements with the same id attribute), and table structure errors (td without tr without table). The browser's DevTools Elements panel shows the corrected DOM — browsers aggressively repair malformed HTML. If the DevTools panel and your source look different, the browser corrected an error. Formatting your source and comparing it to the DevTools representation is a practical debugging technique.

Was this tool helpful?

Related Tools

This free HTML formatter and beautifier formats HTML with 2 or 4 space indentation (or tabs), handles inline elements, preserves pre/script/style content, and includes an HTML minifier that removes whitespace and comments. All processing happens in your browser.

This tool runs entirely in your browser. Your code never leaves your device.