HTML Formatter & Beautifier

Beautify messy or minified HTML with consistent indentation, or minify it for production, while keeping pre, textarea, script and style contents untouched.

Last updatedHow we build & check our tools
Results will appear here once you enter your values.

How the formatter decides where lines break

The formatter reads your HTML into a tree of elements, then prints it back with one level of indentation per nesting depth. Three rules keep the result readable without changing how the page renders:

  • Block containers such as
    ,
      ,
    and put each child on its own line.
    • Inline content stays together. When an element holds only text and inline elements (a, strong, em, code, span, img and similar), it is printed on a single line, for example

      Total: $42

      . Splitting those onto separate lines would insert visible spaces between words and links.
    • Void elements defined by the HTML standard (br, img, input, meta, link, hr and others) never get a closing tag or an extra indent level.

    Runs of spaces and newlines in normal text collapse to one space, which is exactly how browsers render them. Attributes are kept as you wrote them, and quoted values that contain > are handled correctly. Formatting is idempotent: running the output through again produces the same text.

    What is never changed

    Whitespace is significant inside

     and , so their contents are copied byte for byte, including leading spaces and blank lines. The bodies of  and  are also left exactly as written; re-indenting JavaScript can break template literals and regular expressions, and a < inside a script is not treated as a tag. Use a dedicated JavaScript or CSS formatter for those.

    The formatter is forgiving with broken markup: an element left open is closed at the end of its parent, and a stray closing tag with no matching opening tag is dropped. That makes the output valid but can hide the original mistake, so if the result nests differently than you expected, look for a missing in the source. Optional closing tags that HTML allows you to omit (such as and

    ) are added explicitly.

    When to minify instead

    Minify mode removes comments and the whitespace between block elements, and collapses repeated spaces in text, while preserving conditional comments (WHATWG

    HTML Standard: Raw text and escapable raw text elements

    Why script, style and textarea contents are not parsed as markup

    How whitespace is handled by HTML, CSS, and in the DOM

    Whitespace collapsing and why inline content is kept on one line

    Nu HTML Checker

    Recommended validator for markup errors