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.