HTML

HTML Beautifier

Details

How to use HTML Beautifier

What the tool does, how to run it, and what to expect from the result.

How to beautify and reindent HTML

Beautifying HTML puts each tag on its own line and indents it by nesting depth, which turns a minified or machine-generated page into something you can read and navigate. It is what you want when you have pulled markup out of a view-source or an API response.

The formatter runs in your browser. It tracks nesting depth and correctly refuses to indent after a void element such as an image or a line break, which is the detail most simple reindenters get wrong.

  • Paste the HTML into the input box.
  • Read the indented output, where nesting depth is shown by two spaces per level.
  • Check any pre or textarea element, because whitespace inside them is rendered literally and the formatter will have changed it.
  • Extract any script or style block and format it with a JavaScript or CSS formatter instead, since its contents are treated as markup here.
  • If you are reformatting a file in version control, commit the reformat on its own so the diff does not hide a real change.
Tips

Getting a better result out of HTML Beautifier

Specific settings and thresholds, not general advice.

  • Do not run this on HTML containing a pre or textarea element. Whitespace inside those two tags is rendered exactly as written, so reindenting them changes what the user sees: a code block gains leading spaces on every line and a textarea gains its indentation as literal content.
  • Script and style contents are reindented as if they were markup, which is not what you want. A script containing a less-than sign in a comparison, or a style block with braces, is being treated as tags rather than as code. Format those with a JavaScript or CSS formatter instead.
  • The formatter knows the fourteen void elements (img, br, input, meta, link, hr and the rest) and correctly does not indent after them. That is the detail most naive reindenters get wrong, producing markup that drifts one level deeper with every image.
  • Whitespace in HTML is not always meaningless. Inline elements collapse runs of whitespace to a single space but do not remove it, so putting a span on its own line can add a space between two words that were previously joined, which shows up in a live page.
  • Reformatting an entire file produces a diff touching every line. Commit the reformat separately from any real change, or your reviewers cannot see what you actually did.
Limits

What HTML Beautifier does not do

The honest boundary, so you do not lose time finding it yourself.

  • It does not protect pre and textarea content, so whitespace inside them is altered and their rendered output changes.
  • It treats script and style contents as markup rather than as code, so embedded JavaScript and CSS are reindented incorrectly.
  • It does not validate the HTML, close unclosed tags, or repair mis-nesting, so broken markup comes out broken.
  • It does not wrap long lines or format attributes, so an element with twenty attributes still occupies one very long line.
Reference

Terms used on this page

Short, plain-language definitions for the formats and settings above.

At a glance

Who HTML Beautifier is for

A quick way to understand who this helps, what it solves, and where it connects next.

Best fit

Developers and anyone working with code who needs quick formatting.

Ideal for

Quickly beautify HTML without a build step or install.

FAQ

Common questions

Short answers for the questions people usually have before trying a utility like this.

Why did my code block change after formatting?

Because the pre element preserves whitespace, and the formatter indented its contents. Everything inside pre and textarea is rendered exactly as it appears in the source, including leading spaces and newlines, so adding indentation adds visible spaces to every line of your code sample and to the initial value of your textarea. Those two elements have to be left untouched, and this formatter does not exempt them, so avoid it on pages that use them.

Can reindenting HTML change how the page looks?

Yes, in two situations. Inside pre and textarea, where whitespace is preserved verbatim. And around inline elements, where whitespace collapses to a single space but is not removed, so moving a span or an anchor onto its own line can introduce a space between two words that were touching. Both effects are subtle and both are real, which is why a formatter that understands HTML semantics is preferable to a reindenter for production markup.

Why does my JavaScript look wrong afterwards?

Because the contents of a script tag are being processed as markup. The formatter splits on angle brackets, so a less-than sign in a comparison or an arrow function looks like the start of a tag, and the code is reindented on that basis. Extract the script, format it with a JavaScript formatter, and put it back, or better, move it to an external file.

Will it fix my broken HTML?

No. It reindents based on the tags it sees, so an unclosed div causes everything after it to be indented one level too deep, and a mis-nested pair stays mis-nested. It is a formatter, not a validator or a repairer. If you suspect the markup is broken, run it through an HTML validator, which will tell you exactly which element is unbalanced.

Is my markup uploaded?

No. The reindenting runs in your browser as a local string transformation. Nothing is sent anywhere, which matters if the page you are formatting is from an unreleased project.

Recommendations

You Might Also Like

Nearby tools from the catalog that fit the same job or workflow.

Cleanor app

Do it all on your device

Cleanor puts these tools in one app: compress and convert images, video, and audio, work with PDFs, and scan text right on your device. Plus free up storage and clear inbox clutter with Email Cleaner. Start with a free trial.

  • iPhone
  • Android
  • Macsoon
  • Windowssoon