Saved

Press the star next to a tool's name to save it.

All my tools

HTML Formatter and Beautifier

Paste messy or minified markup, and this HTML formatter indents it neatly, keeps script and pre blocks safe, and flags tags that don't seem to be closed.

Text and inline tags stay on one line. Content inside pre and textarea is left exactly as it is.

Your code stays in your browser. Nothing is uploaded.

Formatted HTML

Paste your HTML and press Format HTML.

An HTML formatter takes code that is cramped, minified or badly indented and lays it out so that people can read it. Paste your markup, choose your indent, and this HTML formatter puts each block element on its own line with the right amount of indentation. Short elements stay on one line, text and inline tags are kept together, and the contents of pre and textarea are left untouched. It also tells you if some tags don't seem to be matched, which is often the reason a page looks broken. The work happens in your browser, so your code stays private.

How to use the HTML formatter

  1. Paste your HTML into the box.
  2. Choose 2 spaces, 4 spaces or tabs.
  3. Press Format HTML.
  4. Check any warning about unmatched tags.
  5. Copy the clean code back into your project.

An HTML beautifier example

Suppose that you have this on one line: <div><p>Hello <b>world</b></p><ul><li>One</li></ul></div>. After formatting with 2 spaces, the div sits on the first line, followed by its children, each indented by two spaces. The paragraph stays on one line because it is short and has no block elements inside it, and the bold tag remains within the sentence. The list is opened on its own line, and its list item is indented one level further. The structure of the page is now obvious at a glance.

What the formatter changes

  • Line breaks and indentation around block elements such as div, section, ul and table.
  • Runs of spaces and line breaks in text, which are collapsed into a single space.

What it leaves alone

  • Attributes stay exactly as you wrote them.
  • Inline tags such as a, b, em and span stay in the flow of the text.
  • Pre and textarea keep every space and line break, because there the whitespace is part of the content.
  • Script and style blocks are moved as a unit, keeping the relative indentation of their lines.
  • Comments and the doctype are kept in place.

Void elements and optional end tags

Not every element has a closing tag. A void element is an HTML element that can't have any child nodes, and it has only a start tag. MDN lists these as area, base, br, col, embed, hr, img, input, link, meta, param, source, track and wbr. The formatter knows about them, so they don't push the following code further to the right. A slash at the end of a start tag isn't meaningful in HTML, and HTML parsers ignore it on such elements. HTML also allows the end tags of some elements to be left out, such as li and p. The formatter accepts this too. It treats a new list item as the end of the previous one, and it doesn't report those as errors.

Finding unmatched tags

Indentation is a surprisingly good way to find mistakes. If a page's layout is broken, format the code and look for the place where the indentation stops making sense. The warning above the output counts closing tags that have no matching opening tag and elements that were never closed. It doesn't validate your page against the full HTML standard, so treat it as a quick pointer to a problem.

Why format your HTML?

  • Reading other people's code. Minified code from a live page becomes understandable.
  • Finding bugs. Nesting problems are much easier to see.
  • Code reviews. Consistent formatting keeps the changes between versions small and clear.
  • Email templates and CMS exports. These are often generated as one long line.
  • Learning. Clean code shows how a page is put together.

Spaces or tabs?

The right answer is whatever your project already uses. Two spaces are common in web projects, as deeply nested HTML stays closer to the left edge. Four spaces make each level stand out more. Tabs let every developer choose how wide an indent appears in their own editor. Changing the setting after formatting updates the output immediately, so it's easy to compare the options.

Whitespace and the browser

Browsers treat runs of spaces and line breaks in normal text as a single space, which is why this kind of formatting doesn't normally change how a page looks. There is one exception worth knowing about. A gap between two inline-level elements, such as buttons or images placed side by side, can show up as a small visible space if a line break is added between them. If a layout shifts slightly after formatting, check those places first.

Is my code private?

Yes. The formatter runs on your own device, and your HTML isn't uploaded. Nothing is stored when you leave the page, so copy your code before closing the tab.

Common mistakes

  • Pasting a fragment with unbalanced tags. The warning will count them, even if the full page is fine.
  • Formatting template code carelessly. Check any template syntax after formatting.
  • Ignoring the warning. A count above zero is worth a quick look.

Clean up data with the JSON formatter, compare two versions with the diff checker, and make bulk edits with find and replace text.

What next

Use this often? Add HTML Formatt to your Home screen.

Frequently asked questions

How does the HTML formatter work?

It reads your tags and text, then puts block elements on separate lines with consistent indentation.

Will it change how my page looks?

Normally no. It only changes whitespace, but gaps between inline elements can occasionally shift.

Does it change pre or textarea content?

No. That content is kept exactly as you wrote it.

Is an HTML beautifier the same as a formatter?

Yes. Both names describe tools that lay out code neatly.

Is my code uploaded?

No. It's formatted in your browser.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools