Developer Tools guide
6 Practical Ways to Use an HTML Formatter in Daily Work
ยท 4 min read
An HTML formatter re-indents markup so the nesting is visible: one block element per line, with children indented under their parents. Embedded <style> and <script> blocks get formatted as CSS and JavaScript too. It's useful well beyond tidying your own files. Any time you have to read HTML someone else generated, formatting it first makes the job faster.
Here are six situations where that pays off, and what to watch for in each.
1. Reading a minified production page
Production HTML is often minified into one line. When you need to check what the server actually sent (meta tags, a script order, a missing attribute), view the source, copy it and format it:
<!doctype html>
<html>
<head>
<title>Demo</title>
<style>
.a {
color: red;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
<p>Formatted <b>locally</b>.</p>
<script>
const x = { a: 1 };
console.log(x);
</script>
</body>
</html>
After formatting with 2-space indentation:
<!doctype html>
<html>
<head>
<title>Demo</title>
<style>
.a {
color: red;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
<p>Formatted <b>locally</b>.</p>
<script>
const x = { a: 1 };
console.log(x);
</script>
</body>
</html>
The CSS and JavaScript inside got formatted too, and short inline content like <li><a โฆ>Home</a></li> stays on one line.
2. Finding the tag that breaks your layout
A stray closing tag can push half a page out of its container. Beautifiers that don't parse the HTML just re-indent blindly. This one uses Prettier, which parses the document first and refuses to print broken markup:
<div><p>Unclosed<span>x</div>
The code could not be parsed. Unexpected closing tag "div". It may happen when the tag has already been closed by another tag. โฆ (1:24)
The (1:24) is line 1, column 24, exactly where the parser lost track. That beats scrolling through DevTools looking for the gap.
3. Cleaning up CMS and WYSIWYG output
Rich-text editors and page builders produce correct but unreadable HTML: long single lines, nested <div>s, inline styles everywhere. Format it before you move it into a template so you can see which wrappers to delete. If the destination is a docs site or README rather than a template, convert it with HTML to Markdown instead.
4. Editing HTML email templates
Email HTML is table-heavy and often exported as one block. Formatting makes the <table>, <tr> and <td> nesting readable, so you can find the cell you need to change. Format, edit, and paste back. If your email platform is picky about whitespace, compare a test send before and after.
5. Reviewing generated markup in pull requests
Framework output, static-site generator pages and snapshot tests are easier to review once formatted. Since Prettier always prints the same input the same way, two people formatting the same markup get identical results, and diffs show real changes, not indentation noise. Pick 2 spaces, 4 spaces or Tab under Indent to match your codebase.
6. Learning from other people's markup
Want to see how a site structures its navigation, or how a component library nests its accessible menus? Formatted HTML is the fastest way to read the actual structure: landmarks, ARIA attributes, heading levels. Nothing you paste is uploaded. The formatter runs in your browser, with Prettier loaded on demand.
How to format HTML step by step
- Open the HTML Formatter and paste your markup into HTML input. Load sample fills in a small example.
- Choose the Indent.
- Click Format HTML (or press Ctrl/โ + Enter).
- Copy the Formatted HTML, or download it as
output.html.
What to watch for
Inline whitespace is respected. In HTML, the space between <b>locally</b> and the following . can affect what's rendered. Prettier won't add or remove whitespace around inline elements like <a>, <span> and <b>. To wrap long lines without changing the rendering, it sometimes produces odd-looking breaks inside tags, such as a > starting the next line. That looks strange but it's deliberate. Don't "fix" it by hand unless you know the whitespace doesn't matter.
Line width is 80 characters. Elements that fit within 80 characters stay on one line, and longer ones are broken up, with attributes moved onto separate lines when needed.
Formatting isn't validation. Prettier can parse HTML that a validator would warn about, such as a missing alt or duplicate IDs. Use a dedicated validator or accessibility checker for those.
Templating syntax may fail. Plain HTML is supported. Files mixed with server-side templating (PHP tags, Jinja blocks or Handlebars inside attributes) may not parse, or may come out mangled. Format the rendered output instead.
For standalone stylesheets and scripts, the CSS Formatter and the JavaScript Formatter use the same Prettier engine.
FAQ
Will formatting change my page's appearance?
For block-level structure, no. Prettier is designed to leave inline whitespace alone, so text rendering shouldn't change either. The exception is CSS that changes whitespace handling, like white-space: pre on non-<pre> elements. Check those areas after formatting.
Does it fix invalid HTML?
No. If the markup can't be parsed, you get an error with a position instead of output. Fix the reported tag and run it again.
Can it minify HTML?
No, this tool only beautifies. Minify HTML as part of your build, where the minifier can be configured to keep the whitespace that matters.
Make the next wall of markup readable with the HTML Formatter.
