Skip to content

How to Format HTML Code So It's Readable

By Durim Hoti · Last updated

HTML often arrives as one long line: minified by a build tool, exported from a page builder or copied from the browser. It works, but you can't see which element sits inside which. Formatting puts every block element on its own line and indents children under their parent.

Format HTML step by step

  1. Open the HTML formatter.
  2. Paste the code, or click Open file. The formatted version appears on the right shortly after you stop typing.
  3. Choose Indent (2 spaces, 4 spaces or Tab), Attributes and Wrap at (no wrap, 80 or 120 characters).
  4. For full documents, tick Indent <head> and <body> if you want their contents indented one more level.
  5. Click Copy or Download.

Example: a navigation and a pricing block on one line

One line of HTML
<nav class="menu"><ul><li><a href="/" class="active">Home</a></li><li><a href="/pricing">Pricing</a></li></ul></nav><main><h1>Plans</h1><p>Pick the plan that <em>fits</em> you.</p><button type="button" class="btn btn-primary" data-plan="pro" aria-label="Choose Pro">Choose</button></main>

With the default settings (2 spaces, attributes kept on one line):

Formatted
<nav class="menu">
  <ul>
    <li><a href="/" class="active">Home</a></li>
    <li><a href="/pricing">Pricing</a></li>
  </ul>
</nav>
<main>
  <h1>Plans</h1>
  <p>Pick the plan that <em>fits</em> you.</p><button type="button" class="btn btn-primary" data-plan="pro" aria-label="Choose Pro">Choose</button>
</main>

The list items keep their links on the same line, and the button stays next to the paragraph. Links, buttons and <em> are inline elements, and breaking the line inside running text could add a visible space, so the formatter leaves them where they are.

The button has four attributes. With Attributes: One per line each one gets its own line, aligned under the first:

One per line
<nav class="menu">
  <ul>
    <li><a href="/"
         class="active">Home</a></li>
    <li><a href="/pricing">Pricing</a></li>
  </ul>
</nav>
<main>
  <h1>Plans</h1>
  <p>Pick the plan that <em>fits</em> you.</p><button type="button"
          class="btn btn-primary"
          data-plan="pro"
          aria-label="Choose Pro">Choose</button>
</main>
The HTML formatter with Indent set to 2 spaces and Attributes set to One per line. The input is a single line; the formatted HTML on the right has 15 lines, with the nav, ul and li elements indented and the button's class, data-plan and aria-label attributes each on their own line.
One line in, 15 lines out, with each button attribute on its own line.

Attributes on separate lines make long class lists readable and give clean diffs in Git: a changed class shows up as one changed line.

What a formatter does with broken HTML

This snippet has an unclosed <p>, an unclosed outer <div> and a stray </span>:

Broken HTML
<div><p>Open paragraph<div>inner</div></span>
Formatted
<div>
  <p>Open paragraph
  <div>inner</div></span>

Nothing was added or removed. The output simply ends without stepping back to the left margin, and the stray </span> is still there. That is your cue: paste the code into the HTML validator, which lists each error with its line.

Common problems

Inline tags stay on one line

That is on purpose, as in the example above. If you want a tag on its own line anyway, add a line break before it in the input; existing line breaks are kept.

A line is still very long

Long text or many attributes stay on one line with No wrap. Choose Wrap at 80 or 120 chars, or put attributes one per line.

The indentation looks wrong after a certain line

A closing tag is missing or misspelled before that line. Look for the element whose children never step back out.

Template tags such as {{ }} or <?php ?> look odd

The formatter knows HTML, not template languages. Code between template tags is kept, but its indentation may not follow your template's logic.

What to do next

When the code is ready for production, make it small again; see how to minify HTML, CSS and JavaScript. For separate files, use the CSS formatter and the JavaScript formatter. Need only the words? See how to extract text from HTML.

FAQ

Does formatting change how the page looks?
Normally not: the formatter only adds line breaks and indentation between tags, and text inside <pre> keeps its spaces. Spaces between inline elements can matter in rare layouts, so check the page after formatting if spacing is critical.
Does the formatter fix errors in my HTML?
No. It indents what is there. A missing closing tag shows up as indentation that never steps back, which helps you find it. To list the errors, use the HTML validator.
Can I format CSS and JavaScript too?
CSS and scripts inside the HTML are indented along with it. For separate .css and .js files, use the CSS formatter and the JavaScript formatter.
Is my code uploaded?
No. Formatting runs in your browser; files up to 10 MB can be opened.
Can I turn formatted HTML back into one line?
Yes, that is minifying. The HTML minifier removes the indentation and line breaks again, plus comments and other bytes the browser doesn't need.

Try it and keep reading