How to format HTML
Formatting HTML means laying markup out so a human can read it: one element per line, nested tags indented to match their depth. Here is what formatting does, why it makes debugging easier, and the two honest limits to know.
What formatting does
One element per line, nested deeper
A formatter takes markup that a machine produced — one long line, or indentation that drifted over years of edits — and lays it out so a human can read it. Each element goes on its own line, nested elements are indented one step deeper than their parent, and the structure of the page becomes visible at a glance. Void elements like img, br and input, which never have a closing tag, are recognised and do not increase the depth.
Comments and the doctype stay where they are
Comments and the doctype are kept at the depth where they appear, and text between tags is trimmed and placed on its own line. The result is not a prettier page for visitors — browsers do not care about indentation — it is a prettier page for the next person who has to read the code, which is usually you, six months later.
Why formatted HTML is easier to work with
The most common reason to format is inherited code: a page built by a visual editor, exported from a CMS, or minified by a build step arrives as a wall of text, and something in it needs fixing. Formatting turns a thousand-character line into a structure you can scan, so the unclosed div or the duplicated id stops hiding.
Code review is the same story. A formatted diff shows what changed, while a minified diff shows everything changed. And when two people edit the same file, a consistent format keeps the version-control history readable instead of full of whitespace noise. Formatting is a small habit that pays for itself every time someone opens the file.
Formatting versus minifying
Formatting and minifying are opposite operations on the same file. Formatting adds whitespace to make code readable; minifying removes it to make the file smaller. Neither changes what the page means — a browser treats them identically — so the choice is about who reads the file next: a person, or a network.
The normal workflow uses both. You edit the formatted file, and the build step minifies a copy for production. When something breaks in production, you debug the formatted original, not the minified copy. Tools like this one sit in the middle: paste minified markup, get readable markup back, and keep both versions.
Format HTML in the browser
- Open the HTML Formatter and paste your markup into the Input box.
- Choose 2 or 4 spaces for indentation, then click Format HTML.
- Read through the output: nested tags are indented to match their depth, and script, style, pre and textarea blocks are copied through unchanged.
- If a section is indented one level too deep, a tag above it was probably never closed — fix it in the original and format again.
- Click Copy output or Download to save the formatted markup.
Finding unclosed tags with indentation
A missing closing tag shows as too-deep indent
A formatter does not fix broken HTML, but it makes the break visible. A missing closing tag shows up as a section indented one level too deep, because the formatter believes the unclosed element is still open. Scan for the point where the indentation stops making sense, and the missing tag is usually just above it.
Wrong-order closing tags jump out too
The same trick works for a tag closed in the wrong order. When the indentation jumps unexpectedly, read the tags around the jump — one of them is the culprit. It is faster than counting tags by hand, and it works on files of any size.
Picking an indent style and sticking to it
Two spaces is the common default for HTML, and four spaces suits deeply nested documents. Tabs are a third option, used by teams that already use them everywhere. The choice itself matters far less than consistency: a file that mixes two-space and four-space indentation is harder to read than either style on its own.
If you work in a team, agree on one style and let the editor enforce it — most editors can format on save, which ends the debate permanently. For one-off files, pick the style the file already mostly uses, and keep it.
Related guides
- How to Minify HTML — shrink HTML by removing comments and whitespace.
- How to Format XML — indent XML and check it is actually valid.
- How to Escape HTML (Free HTML Entity Encoder) — turn code characters into entities a browser displays as text.
Questions people ask
Does formatting change how the page looks?
For block layout, no. But a line break between two inline elements renders as a space, so formatting can add a small gap inside tight inline content such as icon and label pairs. Check the result in a browser before publishing.
What is the best indent size?
Two spaces is the common default for HTML, and four spaces suits deeply nested documents. What matters is consistency: one style throughout the file beats a mix of styles.
Can formatting fix broken HTML?
No. It indents the tags as written, so a missing closing tag shows up as a section indented one level too deep. That makes the problem easier to spot, but you still have to fix the markup yourself.