In this article
Where messy HTML comes from
- Minified pages: sites remove whitespace to load faster, so “view source” shows a single line.
- Page builders and CMS editors: drag-and-drop tools nest many wrapper elements.
- Email templates: table-based layouts with inline styles are hard to follow.
- Copy-paste from Word: brings spans, classes and styles you never asked for.
Why indentation helps
HTML is a tree: elements inside elements. Indenting each level by the same amount makes the tree visible, so you can see at a glance which <div> a section belongs to, where a list ends, and whether a closing tag is missing. Many layout bugs, a sidebar falling under the content, a footer inside a column, turn out to be one misplaced closing tag that is obvious once code is indented.
How to format HTML
- Paste your code into the HTML Formatter.
- Choose your indentation.
- Copy the result.
The formatter indents block elements, keeps short inline content such as links and bold text on one line, preserves the exact content of <pre> and <textarea>, re-indents script and style blocks, and handles void elements (like <img> and <br>) and common unclosed tags such as <li> and <p>.
Checklist after formatting
| Look for | Why |
|---|---|
| Indentation that drifts right and never comes back | A closing tag is missing |
Many nested <div>s with one child each | Builder bloat you may be able to simplify |
Inline style="…" repeated everywhere | Move to a stylesheet if you control the site |
Empty <p></p> and spacers | Use CSS margins instead |
Images without alt | Add descriptions for accessibility and SEO |
Cleaning HTML pasted from Word
Pasting from Word into a CMS often adds class="MsoNormal", font tags and inline styles. The best cure is prevention: paste as plain text (Ctrl+Shift+V) and apply formatting in the CMS. If you already have the messy HTML, format it here to see the clutter, then remove it with Find and Replace using patterns such as class="Mso\w+".
Validating after cleaning
Formatting shows structure but does not prove the HTML is correct. For an official check, paste your code or URL into the W3C Markup Validation Service, which lists errors such as duplicate IDs and invalid nesting. Fix the errors that affect layout and accessibility first; minor warnings on third-party code can usually wait.
Do not format everything you deploy
Formatting is for reading and editing. For production, the server or build tool can minify HTML, and CSS and JavaScript benefit from minification too, see the CSS Minifier and JavaScript Minifier.
Writing HTML from scratch?
Write in Markdown and convert it with Markdown to HTML, you get clean, minimal HTML without builder clutter. To show HTML code as text inside a page, encode it with the HTML Entity Encoder.
Frequently asked questions
What does an HTML beautifier do?
It adds consistent indentation and line breaks so nested tags are easy to read.
Will formatting change how the page looks?
Normally no. Extra spaces between block elements do not change the layout, though whitespace inside inline text can matter.
How do I find unclosed tags?
Formatted code makes mismatched nesting easier to see. An HTML validator lists exact errors.
Should I remove inline styles?
Moving styles to a CSS file makes code cleaner and easier to update.
Is my code uploaded?
No. Formatting happens in your browser.
How do I clean HTML copied from Word?
Paste it into a plain-text editor first or remove classes like MsoNormal, inline styles and empty spans, then format the result.
Does formatting HTML change how the page looks?
No. Indentation and line breaks between tags do not change the layout, except inside pre or textarea tags.
What is the difference between formatting and minifying HTML?
Formatting adds indentation so people can read the code; minifying removes it so the file downloads faster.