Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

HTML beautifier

Paste HTML or open a file and get it back neatly indented, one block element per line, with the CSS and JavaScript inside formatted too. Pick spaces or tabs, copy the result or download it; the code is processed on your device only.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

Attributes
Options
Beautified HTML

35 linesFormatted, nothing removed

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Plain Words</title> <style> body { margin: 0; font: 16px/1.5 serif } h1 { color: #123 } </style> </head> <body> <nav><a href="/">Home</a> <a href="/about/">About</a></nav> <main> <h1>Plain <em>words</em></h1> <p>A short line.<br>Next line.</p> <p><img src="cover.png" alt="Cover"></p> <ul> <li>One</li> <li>Two</li> </ul> <pre> keep this</pre> </main> <script> const year = new Date().getFullYear(); document.title += " " + year; </script> </body> </html>
  • 2-space indent
  • No line wrap
  • 1 style block and 1 script formatted
Size
487 B → 627 B
Lines
2 → 35
Start tags
21 (3 void)
Longest line
61 characters

How to use the HTML Beautifier

  1. Paste the code into Your HTML, or use Or choose an HTML file. The file is read on your device.
  2. Choose the Indent (2 spaces, 4 spaces or a tab) and whether to Wrap long lines at 80 or 120 characters.
  3. Under Attributes, keep them on the tag line or put each one on its own line. Untick Format CSS and JavaScript inside to leave those blocks alone.
  4. Press Copy or Download. Any start and end tags that do not pair up are listed under the chips.

How do I format minified HTML?

Paste it into the box: the beautified version appears straight away, with each block element such as a heading, list or section on its own line and indented one step deeper than its parent. Copy it back into your editor or download it as an .html file. Tags, attributes and text all stay; only the spacing between them changes.

The page in the box is squeezed onto 2 lines and weighs 487 bytes. Beautified with 2 spaces it opens up to 35 lines and 627 bytes, because indentation is made of real characters. The tool counts 21 start tags in it, 3 of them void elements such as <meta>, <br> and <img> that never take an end tag.

Use 2 spaces when you mostly read code in a browser or a narrow window, 4 spaces when your project style asks for it, and a tab when each person should choose the width in their own editor. Wrapping at 80 or 120 characters helps when long paragraphs or tags with many attributes run off the screen. The table shows what each choice does to the example page.

The example page under each setting, measured by this tool
SettingLinesSizeLongest line
2 spaces (default)35627 bytes61 characters
4 spaces35723 bytes63 characters
Tab35579 bytes60 characters
2 spaces, head and body indented35687 bytes63 characters
2 spaces, CSS and JavaScript left as written23558 bytes80 characters
Wrap at 80, one attribute per line38650 bytes61 characters

By default <head> and <body> start at the same level as <html>, a common habit because every page has them and an extra level would push all the real content one step right. Tick Indent head and body if you prefer strict nesting; on the example that adds 60 bytes.

Blank lines you left between sections on purpose are kept, but never more than one in a row, so <p>Hi</p> <p>There</p> with three empty lines comes back with just one. Untick Keep single blank lines to remove them all and get 2 lines instead of 3.

Does beautifying change how the page renders?

Not for normal content. Browsers collapse runs of spaces, tabs and line breaks in ordinary text into a single space, because the CSS white-space property starts at normal. Extra line breaks and indentation between block elements are therefore invisible on the page.

Three places need care, and the beautifier treats each one differently. Text inside <pre> and <textarea> is shown exactly as typed, so it is copied over untouched; in the example, the two leading spaces before "keep" survive. Inline elements such as links, <em> and <span> stay on one line with the text around them, since a line break between two links would show up as a space between them. And whitespace inside a tag's text is left in place, though a run of several spaces may become one, which the browser does anyway.

If your CSS sets white-space: pre or pre-wrap on some other element, its spacing does matter. Check those parts after formatting, or keep them out of the pasted code.

Can it format inline CSS and JavaScript?

Yes. The HTML standard calls <script> and <style> raw text elements: their contents are code, not markup. With Format CSS and JavaScript inside ticked, each rule in a style block gets its own line and each statement in a script is spaced and indented, one level inside the tag. That is how the example's one-line stylesheet turns into two readable rules.

Untick the option to keep those blocks exactly as written, for example when a script was minified on purpose or a style block is generated by a build step. On the example page this leaves 23 lines instead of 35. The formatting is done by js-beautify, an open-source library under the MIT licence, using its own HTML, CSS and JavaScript formatters with the options on this page.

If you need only a stylesheet or only a script tidied, paste it into a style or script tag here, or use the CSS minifier for the opposite job of shrinking a stylesheet.

Why does the beautifier not fix broken HTML?

A beautifier changes layout, not meaning. It indents whatever you paste, so a tag that was never closed is still never closed afterwards, just easier to spot. To help, the tool counts start and end tags for elements whose end tag is required, such as <div>, <span>, <a> and headings, and lists any that do not pair up.

Paste <div><div><p>Sale</div> and the result reads "<div>: 2 start tags, 1 end tag". The formatted output, <div> <div> <p>Sale </div>, makes the problem visible: the outer div has no end tag, so it swallows everything after it.

Some missing end tags are not mistakes at all. The HTML standard lets an <li> end tag be left out when another list item follows, and a <p> end tag when certain block elements follow, so those are never counted as problems. The same standard lists the void elements, including <br>, <img>, <input> and <meta>, and says a trailing slash on them, as in <br />, has no effect. The formatter keeps the slash if you wrote one.

For a full check of attributes, nesting rules and accessibility, run a markup validator as well. The XML formatter is the stricter tool for XHTML, SVG and feeds, where every tag must close and one stray ampersand is an error.

Use it with

Questions

How do I beautify HTML code?

Paste it into the box above. It is re-indented at once, one block element per line, and the result can be copied or downloaded. The example page goes from 2 lines to 35.

How do I use tabs instead of spaces?

Set Indent to Tab. Each nesting level then starts with one tab character, and every editor shows it at the width its user prefers. Tabs also make the file smaller than 2 or 4 spaces.

Will beautifying break my page?

Normally not: browsers collapse spaces and line breaks in ordinary text, and the tool leaves pre and textarea content and inline elements as they are. Elements styled with white-space: pre are the exception to check by eye.

Can it format the CSS and JavaScript inside my HTML?

Yes, style and script blocks are formatted along with the markup. Untick Format CSS and JavaScript inside to leave them exactly as written.

Why are my closing li and p tags missing?

They may never have been there. HTML allows the end tag of a list item or paragraph to be left out in many cases, and the beautifier does not add tags. The output keeps exactly the tags you pasted.

Is my HTML uploaded?

No. The formatting runs in this page with code it has already loaded, so the HTML you paste or open stays on your device. It keeps working if you go offline after the page has loaded.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.