Minified HTML is great for production. Minified HTML is terrible for debugging. When you right-click "View Source" on a page and see 500 KB of HTML on a single line, finding the div that is causing your layout issue is effectively impossible without reformatting.
HTML formatting -- consistent indentation, proper nesting, and readable structure -- is not about aesthetics. It is about making the document structure visible so you can reason about it.
What proper HTML formatting looks like
Unformatted:
<div class="container"><div class="row"><div class="col-6"><h2>Title</h2><p>Content goes here with <a href="/link">a link</a> in it.</p></div><div class="col-6"><img src="image.jpg" alt="Description"></div></div></div>
Formatted:
<div class="container">
<div class="row">
<div class="col-6">
<h2>Title</h2>
<p>
Content goes here with <a href="/link">a link</a> in it.
</p>
</div>
<div class="col-6">
<img src="image.jpg" alt="Description">
</div>
</div>
</div>
In the formatted version, the nesting structure is immediately visible. You can see at a glance that there are two columns inside a row inside a container. You can see that the heading and paragraph are in the first column and the image is in the second. This visual structure matches the document structure.
Formatting rules that matter
Indent nested elements. Every child element should be indented one level deeper than its parent. Two spaces or four spaces -- pick one and be consistent. The indentation makes nesting depth visible.
One attribute per line for complex elements. When an element has many attributes, vertical formatting improves readability:
<input
type="email"
id="user-email"
name="email"
class="form-control"
placeholder="Enter your email"
required
aria-describedby="email-help"
>
Inline elements stay inline. Short inline elements like <a>, <strong>, <em>, and <span> should stay on the same line as their surrounding text. Breaking them onto separate lines adds visual noise without improving readability.
Empty lines between logical sections. Separate the header from the main content, the main content from the sidebar, and the content from the footer with blank lines. These visual breaks mirror the semantic structure.
When formatting matters most
Code review. Reviewing a diff of minified HTML is nearly impossible. Formatted HTML produces meaningful diffs where changes are isolated to specific lines, making the actual modifications clear.
Debugging layout issues. When a flex container is not behaving as expected, formatted HTML lets you count the child elements, verify the nesting, and check that every opening tag has a corresponding closing tag -- all at a glance.
Accessibility auditing. Checking ARIA attributes, label associations, and heading hierarchy requires reading the HTML structure. Formatting makes this feasible.
Handoff between team members. When another developer inherits your HTML, formatting is the difference between "I can understand this" and "I need to reformat this before I can understand it."
Automated formatting
Prettier, the most popular code formatter, handles HTML:
npx prettier --write "**/*.html"
VS Code's built-in formatter (Shift+Alt+F) handles HTML formatting with reasonable defaults. ESLint plugins like eslint-plugin-html can enforce formatting rules in CI.
For one-off formatting of HTML snippets -- API responses, email templates, scraped content, or View Source output -- a dedicated formatting tool is faster than configuring Prettier.
I built an HTML formatter at zovo.one/free-tools/html-formatter that takes any HTML -- minified, poorly indented, or inconsistently formatted -- and produces clean, consistently indented output. Paste messy HTML, get readable HTML. Useful for debugging, code review preparation, and making sense of third-party HTML that was not written with readability in mind.
I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.
Top comments (0)