Minify HTML
Strip comments, collapse whitespace, and remove redundant attributes from HTML output to reduce file size.
Metrics
This technique can move up to 33% of LCP's share of the Lighthouse score.
This technique does not move TBT.
This technique does not move CLS.
This technique can move up to 33% of FCP's share of the Lighthouse score.
This technique can move up to 33% of SI's share of the Lighthouse score.
This technique can move up to 33% of TTFB's share of the Lighthouse score.
Overview
HTML minification removes characters that are meaningful to developers — indentation, comments, blank lines, redundant attribute quotes — but carry no information for the browser. The result is a smaller HTML payload the browser downloads and parses faster, shaving milliseconds from Time to First Byte and First Contentful Paint.
Impact grade, A+ to D: how much of the Lighthouse score this technique can move. Unaffected: TBT, CLS.
The Problem
Server-rendered HTML from WordPress, especially with many active plugins, often contains verbose output: multiple levels of indentation, inline HTML comments from theme components, redundant boolean attributes, and unnecessary whitespace between tags. These characters are invisible in the rendered page but inflate every HTML response the server sends. On high-traffic sites this also increases CDN egress and bandwidth costs.
Performance Impact
LCP
Low
LCP element is discovered sooner in a compact HTML document
FCP
Low
browser parses a smaller document and reaches first paint sooner
SI
smaller HTML document parses faster; browser populates visible content sooner as it progresses through the document
TTFB
Low
slightly smaller payload means faster network transfer from server to browser
Risk: Medium
aggressive minification can break inline scripts, pre blocks, or malformed HTML
How it works
01
Whitespace and blank lines collapsed
Multiple consecutive spaces, tabs, and newlines are collapsed to a single space. Whitespace between closing and opening tags is removed entirely. The rendered output is identical — browsers ignore extra whitespace in HTML.
02
HTML comments stripped
Comments such as
<!-- end nav -->or plugin debug markers are removed. IE conditional comments (<!--[if IE]>) are preserved because browsers still parse them.03
Redundant attributes shortened
Boolean attributes like
disabled="disabled"are simplified todisabled. Quotes around attribute values containing only alphanumeric characters can be dropped (class=myclass). Default attribute values implied by the HTML spec are removed.04
Protected blocks left intact
Well-implemented minifiers preserve content inside
<pre>,<textarea>, and<script>blocks verbatim, since whitespace is significant inside them.
Limitations
- Aggressive minification can mishandle malformed HTML, inline JavaScript that relies on line breaks as statement terminators, or theme output that uses HTML comments for structural markers. Thorough testing is essential before enabling on production.
- Gains are modest — typically 5–25% reduction in HTML size, which translates to very small absolute savings for most pages. More impactful optimisations (images, JavaScript, server response time) should come first.
- On-the-fly minification without caching adds PHP execution overhead on every request. Always combine with page caching so minification runs once and the result is served from cache.
- Minified HTML makes debugging page source harder. Browser dev tools can pretty-print it, but it is an extra step for developers and support teams.
Common Mistakes
Treating HTML minification as a major performance fix
It is a marginal optimisation. An unoptimised hero image or a render-blocking script can cost seconds; HTML minification saves tens of milliseconds at best. Prioritise higher-impact techniques first.
Using aggressive settings without testing
Start with “safe” defaults that only collapse whitespace and strip comments. Enabling attribute-quote removal or optional-tag stripping requires thorough cross-browser testing of the resulting HTML.
Running minification on every request without a cache
Minify once per page and cache the result. Running a regex over every HTML response without caching turns a micro-optimisation into a noticeable CPU overhead.
Need help applying this to your site?
Targeted fixes, not guesswork. We identify which techniques move the needle for your specific WordPress stack.
Start an Engagement