When to format HTML
HTML from CMSs, template engines, and copy-paste is often minified or inconsistently indented. Beautifying it makes the structure visible — you can see parent-child relationships at a glance, spot unclosed tags, and navigate the document easily.
Minifying goes the other direction: it strips comments and collapses whitespace to produce the smallest possible output, reducing bytes sent to the browser.
How indentation works
Each nested element is indented by 2 spaces relative to its parent. Block elements (div, section, article, ul, li, etc.) each get their own line. Inline elements (a, span, strong, em) stay on the same line as their surrounding text. Void elements (br, hr, img, input) are self-closing and don't increase indent.
Content inside <script>, <style>, and <pre> blocks is preserved exactly — whitespace in these tags is meaningful and must not be changed.
HTML vs CSS vs JS minification
HTML minification typically saves 5–20%. CSS minification saves 20–40%. JavaScript minification saves 40–70% (with variable renaming). For maximum performance, minify all three — most build tools do this automatically in production.
- HTML: remove comments, collapse whitespace between tags
- CSS: also remove redundant semicolons, collapse shorthand values
- JS: also rename local variables, remove dead code (tree-shaking)