Xerobit

HTML Formatter: Beautify and Indent HTML Online

This HTML formatter (also called an html prettifier or html beautifier) takes messy, minified or poorly indented HTML and rewrites it with consistent indentation and one tag or text run per line, so you can read and edit it. Paste your code, choose an indent size from 1 to 8 spaces, and the result appears instantly. Everything runs in your browser, so nothing is uploaded.

Formatted HTML

How to use the html prettifier

  1. Paste your raw or minified HTML into the HTML input text area.
  2. Set the number of Indent spaces you prefer (the default is 2).
  3. The formatted output updates automatically as you type or paste.
  4. Copy the result from the Formatted HTML area for use in your editor.

How to format HTML

To format HTML, paste it into the input box, pick an indent size (2 spaces is the default) and copy the result. Each opening tag raises the indent by one level, each closing tag lowers it, and every tag and text run goes on its own line. Here is a real input and the exact output of this tool at 2 spaces:

Before

<div class="card"><h2>Title</h2><p>Hello <strong>world</strong>, and <em>more</em>.</p><ul><li><button type="button">One</button></li><li>Two</li></ul><img src="a.png" alt="A"><script>if(a<b){go()}</script></div>

After

<div class="card">
  <h2>
    Title
  </h2>
  <p>
    Hello
    <strong>
      world
    </strong>
    , and
    <em>
      more
    </em>
    .
  </p>
  <ul>
    <li>
      <button type="button">
        One
      </button>
    </li>
    <li>
      Two
    </li>
  </ul>
  <img src="a.png" alt="A">
  <script>if(a<b){go()}</script>
</div>

Parsing the input and the output with the parse5 HTML parser gives the same elements, the same attributes and the same text once whitespace is ignored. Formatting the output a second time returns it unchanged. Note the line , and: the original had no space between </strong> and the comma, the formatted version has a line break there (see the rendering question below).

What the formatter changes and what it leaves alone

This online html formatter works on the text of your markup. It does not build a DOM, validate, repair or reorder anything.

Part of the HTMLWhat happens
Indentation and line breaksRebuilt from the tag nesting; existing indentation and blank lines are discarded
Text between tagsRuns of whitespace become one space, the text is trimmed and put on its own line
Tag names, attributes and valuesCopied as written, including upper case names such as <DIV> and unquoted values
Void elements such as img and brOne line, no extra indent level
Comments and doctypeKept on their own line
pre, textarea, script, styleContent between the tags is kept exactly, only the opening tag is indented
Unclosed or mismatched tagsNot fixed; the indent level simply follows the tags you wrote

Inline elements such as strong, em and a are treated like block elements, so each goes on its own line. That keeps the indentation rule simple but makes the output longer than hand-formatted markup. The HTML Standard defines the void elements and the raw text elements (script and style) that the formatter special-cases.

How the formatting works

An html prettifier parses the source string into a sequence of tokens: opening tags, closing tags, self-closing tags, comments, doctypes, and text nodes. It then walks through those tokens while tracking a depth counter. Each opening tag increases the depth, each closing tag decreases it, and self-closing tags leave it unchanged.

The indent string is simply the space character repeated depth * indentSize times. For example, with an indent size of 2, a token at depth 3 receives 6 leading spaces. Text nodes are trimmed of surrounding whitespace; if they contain non-whitespace characters they are printed on their own line at the current depth plus one.

Content inside <pre>, <textarea>, <script>, and <style> elements is treated as raw text. The formatter captures everything between the opening and closing tags verbatim, preserving all original whitespace and line breaks, because altering that content would change how the page renders or executes.

Void elements and self-closing tags

HTML defines a set of void elements that never have a closing tag and cannot contain child content. When the formatter encounters one, it prints the tag on its own line without increasing the depth counter. The standard void elements are:

ElementDescription
<area>Image map area
<base>Base URL for relative links
<br>Line break
<col>Table column properties
<embed>External content plugin
<hr>Thematic break
<img>Image
<input>Form control
<link>External resource link
<meta>Metadata
<source>Media source
<track>Text track for media
<wbr>Word break opportunity

If you write a void element with a trailing slash like <br />, the formatter recognizes it as void and handles it identically to <br>.

When to use an html prettifier

Developers reach for an html prettifier in several common situations. Minified production HTML strips all unnecessary whitespace to reduce file size, making it nearly impossible to read. Pasting that single-line string through a formatter restores the visual hierarchy immediately.

Server-side templating engines often generate markup with inconsistent nesting. Running the output through a formatter reveals structural mistakes such as unclosed tags or misplaced elements. Code reviews also benefit: rather than parsing dense diffs by eye, reviewers can format both versions and compare them cleanly.

For learning purposes, beginners studying generated DOM structures can paste view-source output into the tool to see exactly how elements nest inside one another.

Prettifying versus minifying

Prettifying and minifying are inverse operations. An html prettifier adds whitespace, line breaks, and indentation to maximize human readability. A minifier does the opposite: it collapses multiple spaces into one, removes line breaks, and strips optional quotes to produce the smallest possible byte count for network transfer.

In a typical workflow, developers write readable HTML during development, optionally run it through a formatter to enforce team style rules, and then apply a minifier as a build step before deployment. You can use the HTML minifier on this site to reverse the process when you need to shrink your markup for production.

Common edge cases

Not all HTML is perfectly well-formed. A robust formatter must handle several edge cases gracefully. Unclosed tags cause the depth counter to drift upward, resulting in excessive indentation for subsequent elements. The formatter used here processes tokens sequentially without attempting to auto-close tags, so malformed input produces output that reflects the actual structure provided.

Attributes containing angle brackets, such as <div data-expr="a < b">, can confuse naive parsers that split on every < character. This tool uses a state machine that tracks whether it is currently inside a quoted attribute value, preventing false token boundaries.

Inline text mixed with inline elements, like <p>Hello <strong>world</strong></p>, is formatted with each node on its own line. While this differs from how some editors keep inline content together, it guarantees consistent indentation regardless of the nesting depth.

Worked examples

ExampleInputResult
Minified divHTML input: <div><p>Hello</p></div>; Indent spaces: 2<div> / <p> / Hello / </p> / </div>
Nested listHTML input: <ul><li>One</li><li>Two</li></ul>; Indent spaces: 4<ul> / <li> / One / </li> / <li> / Two / </li> / </ul>
Self-closing tagsHTML input: <div><img src="a.png"><br><input type="text"></div>; Indent spaces: 2<div> / <img src="a.png"> / <br> / <input type="text"> / </div>

FAQ

How do I format HTML online?

Paste your HTML into the input box above, choose the indent size and copy the formatted result. It updates as you type and nothing leaves your browser. For a code editor, the same result comes from running a formatter on save, but this page needs no setup.

What is an HTML beautifier?

An HTML beautifier, also called an html prettifier or html code formatter, rewrites the whitespace of your markup so that nesting is visible: each tag on its own line, indented by depth. It changes layout of the source, not the elements or attributes. To beautify HTML or to pretty print HTML means exactly this, and a formatter html tool is the same thing under another name.

Does formatting change how the page renders?

Sometimes. MDN documents that whitespace between adjacent inline elements is rendered as a space, and that runs of spaces collapse to one. Because this tool puts inline elements on separate lines, text such as <strong>world</strong>, and becomes world , and on screen. Whitespace between block elements is ignored by browsers. Content inside pre, textarea, script and style is kept exactly.

Can it minify HTML too?

No, this page only formats. To go the other way and shrink markup, use the HTML minifier.

Does this html format online tool validate or fix my HTML?

No. It does not check that tags are closed, nested correctly or allowed where they appear. Broken input is indented according to the tags as written, so a missing closing tag shows up as extra indentation.

Does this html prettifier upload my code to a server?

No. All formatting happens locally in your browser using JavaScript. Your HTML is never sent over the network.

Will formatting change how my page renders?

In most cases, no. HTML ignores extra whitespace between block elements. However, adding line breaks between inline elements can introduce small visual gaps. The formatter preserves content inside pre, textarea, script, and style tags exactly as written.

Can I format broken or invalid HTML?

Yes. The formatter processes whatever tags and text you provide without validating the document against the HTML specification. Severely malformed input may produce unexpected indentation, but it will not crash.

What indent size should I use?

Two spaces is the most common convention in modern web development. Four spaces is also widely used. Choose whichever matches your project style guide.

Does it handle XML or SVG?

It is designed for HTML. It handles self-closing tags and basic XML-like syntax, but it applies HTML-specific rules such as treating known void elements as self-closing even without a trailing slash.

How do I reverse the formatting?

Use the companion HTML minifier to strip whitespace and compress the formatted HTML back into a compact form.

Sources and references