OnlineTextLab
Esc

HTML Beautifier

Make minified or tangled HTML readable, with consistent indentation that does not change what the page displays.

Indent with
Formatted HTML

The formatted HTML appears here as you type.

Runs on your device. Your files and text are not uploaded.

About HTML Beautifier

View Source on most modern sites shows one enormous line. Email templates and CMS exports are often not much better. This tool lays the document out with one block element per line, indented by depth, so you can see the structure and find the part you came for.

It takes care not to change the page in the process. A naive formatter puts every tag on its own line, which adds visible spaces between inline elements and can shift a layout. Here, a paragraph with bold words in it stays on one line, as you would write it by hand.

It is also a useful way to debug a broken layout. Bad nesting tends to show up as indentation that does not end where you expected, and a stray closing tag gets a line to itself.

How it works

The HTML is parsed into a tree that keeps the original text of every tag, so attribute order, quoting and spacing inside a tag come through unchanged. Elements are sorted into block and inline. Block elements such as <div>, <p> and <li> start a new line, and an element holding only inline content is written on one line along with it.

Inside that inline content, whitespace is collapsed to single spaces, and no space is ever added where the source had none. <b>a</b><i>b</i> stays joined, since a browser would show a gap if it were split. An element that contains a block child is itself treated as a block, so a link wrapping a whole card is laid out across several lines.

<pre>, <script>, <style> and inline white-space: pre elements are written back exactly as they arrived, and so is <textarea>, whose value would change otherwise. End tags you left out are not invented, so <li>a<li>b stays that way. Running the output through again gives the same output.

How to use HTML Beautifier

  1. Choose 2 spaces, 4 spaces or Tabs under Indent with.
  2. Paste the minified or messy source on the left, or load an .htm or .html file with Open file.
  3. Read the formatted source on the right, with its line count below.
  4. Press Copy result to take it to your editor, or Download .html to save a file.

Omitted end tags and a pre block

Input
<div><h1>Menu</h1><p>Soup <b>4.00</b></p><ul><li>Bread<li>Salad</ul><pre> a
  b</pre></div>
Output
<div>
  <h1>Menu</h1>
  <p>Soup <b>4.00</b></p>
  <ul>
    <li>Bread
    <li>Salad
  </ul>
  <pre> a
  b</pre>
</div>

The <b> stays inside its line, the two <li> items are recognized as siblings even with no end tags, and the second line of the <pre> keeps its original two spaces rather than being indented to match its surroundings.

When it helps

  • Reading the minified source of a page to find how a section was built.
  • Making a CMS export or email template editable, before you start changing it by hand.
  • Reviewing generated HTML in a diff, where one long line would hide every change.
  • Spotting a nesting error that makes a layout collapse, by following the indentation.

Tips for a better result

  • Use the same indent setting as your project, so the formatted code will not cause a large diff later. Most HTML style guides use two spaces.
  • A script or stylesheet that was on one line stays on one line. For a readable version, copy its contents to a formatter for that language.
  • If you only need the words from a page, HTML to Text gets you there faster than reading through tags.

Good to know

  • It is a formatter and not a validator. Unclosed elements, wrong nesting and unknown tags are laid out as they are, with no warning or repair.
  • Long lines are not wrapped, and attributes are not split across lines, so a paragraph or a tag with many attributes stays on one long line.
  • JavaScript and CSS blocks are not reformatted, because changing them risks changing what they do.
  • Blocks and inline elements are decided by tag name. Elements restyled in CSS, such as a <span> set to display: block, are formatted according to their name.
  • Adding line breaks between block elements is harmless in normal layouts. It can create small gaps if a stylesheet makes them inline-block, which is the usual cause if a row of items shifts after formatting.
  • Very deep nesting is flattened past 400 levels so the tool keeps working. Real pages rarely come close.

Questions people ask

Why are some tags not on their own line?

Inline elements like <a>, <b> and <span> stay within the text they belong to. Putting them on separate lines would add whitespace, which a browser displays as a space between words. Keeping them on one line is the only way to guarantee the text does not change.

Why is the code inside my pre block not indented?

Every character in a <pre> is displayed, including leading spaces. Indenting its lines to match the code around it would visibly shift the block on the page, so its contents are left exactly as they came, even where that looks out of step.

Will it add the closing tags I left out?

No. HTML allows some end tags to be omitted, such as those for <li> and <td>, and adding them is a change this tool does not make to your document. It does understand them for working out indentation, so the items line up correctly.

What happens to malformed HTML?

It still formats. An end tag with no matching start tag gets its own line, which makes it easy to spot, and elements left open simply run to the end of their parent. A stray "<" in text is kept as text, and no error stops you.

What to use next

  • HTML Minifier: to compress the file again when you have finished editing
  • HTML to Markdown: if you would rather maintain the content as Markdown
  • HTML to PDF: to turn the finished page into a shareable document

Last reviewed . Read our editorial policy.