Code Formatter guide
What XGM Code Formatter removes and preserves when it minifies HTML, CSS and JavaScript or formats XML, and the whitespace cases that can change a layout.
What HTML minification does
HTML written by people and templates contains indentation, line breaks and comments that browsers do not need. Minification removes them to reduce the number of bytes sent over the network. The page's structure and content stay the same.
<!-- product card -->
<div class="card">
<h2> Shoes </h2>
<p>
Size 42
</p>
</div>
<div class="card"><h2> Shoes </h2><p> Size 42 </p></div>How to minify HTML with Code Formatter
- Open the Code Formatter and paste your HTML.
- Read the minified output and the size saving.
- Copy the result, or download it for your build or template.
- Open the page with the minified HTML and check that the layout looks the same.
Press Load example to see a small page with a comment, indentation and a pre block, which shows what is removed and what is preserved.
Minification runs in your browser. For production sites, minify as part of the build process so every deployment is minified automatically, and use this tool for one-off snippets, email templates or checking what a minifier would change.
Formatting and minifying XML
The XML mode of Code Formatter parses the input with the browser's XML parser, so malformed documents are reported with the parser's error message instead of being reformatted silently. Format indents elements two or four spaces per level; Minify removes the whitespace between elements and drops comments. CDATA sections and processing instructions such as the XML declaration are kept.
Text inside elements is trimmed when formatting. That is safe for configuration files and feeds like DMARC aggregate reports, but not for XML formats where leading spaces in text carry meaning; keep the original for those. Like the minifiers, the XML mode runs entirely in your browser.
Minification versus compression
Web servers and CDNs compress text responses with gzip or Brotli, which removes repeated patterns very efficiently, including most of the whitespace that minification would remove. The additional saving from minifying already-compressed HTML is usually modest. Minification still helps a little, and it helps more for pages that are not compressed, such as some email HTML.
| Version | Relative size |
|---|---|
| Original HTML | 100% |
| Minified | Around 80–90% |
| Original, compressed with gzip | Around 20–25% |
| Minified and compressed | Slightly less than compressed alone |
Check whether compression is active on your site: the response should include Content-Encoding: gzip or br. The HTTP Headers Checker lists all response headers, including that one.
gzip on;
gzip_types text/html text/css application/javascript application/json image/svg+xml;
gzip_min_length 1024;When whitespace matters
HTML collapses runs of whitespace into a single space when rendering, but it does not always remove it. A space between two inline elements, such as two links or a label and an input, is visible. Removing whitespace between tags can join words or remove small gaps between buttons.
| Markup | Risk |
|---|---|
<a>Home</a> <a>About</a> | Links can run together when the space between tags is removed |
| Inline-block buttons separated by line breaks | The small gap between them disappears |
<pre> and <textarea> | Preserved by the minifier; hand-edited minifiers often break them |
Inline <script> with template literals | Preserved by the minifier; whitespace inside strings is meaningful |
| Text nodes with deliberate double spaces | Collapsed to one space (browsers show one anyway, except in pre) |
The minifier collapses whitespace between tags aggressively, which is why a visual check matters. If a gap disappears, add the spacing in CSS (for example with gap or margins) instead of relying on whitespace in the markup, which also makes the layout more predictable.
Minification in a build workflow
Keep readable HTML in your source files and templates, and minify only the output that is deployed. Editing minified HTML by hand is error-prone, and diffs of minified files are unreadable in code review. Most static site generators and frameworks have a minification step or plugin that runs during the production build.
- Write and review templates in readable form.
- Build the site; the minifier runs on generated HTML files.
- Run visual or end-to-end tests against the minified output, not only against development builds.
- Deploy, and confirm compression headers on the live site.
- Keep source maps or the unminified build artefact for debugging when needed.
Server-rendered applications can minify responses in middleware, but that costs CPU on every request. Caching the rendered, minified output, or minifying templates at build time, avoids repeating the work.
Email HTML is a special case. Many email clients have their own rendering quirks, and some clip messages above a certain size, so minification can prevent a long newsletter from being cut off. Test the minified version in the clients your recipients use before sending.
What the minifier does not change
The XGM minifier is deliberately conservative. It does not remove optional closing tags, attribute quotes or default attribute values, and it does not rewrite inline CSS or JavaScript. Those more aggressive optimisations save a few more bytes but are also the ones most likely to break unusual markup or tools that parse the HTML later.
If you need more aggressive minification, use a dedicated build tool with a test suite for your pages. For quick checks, snippets and templates, conservative minification keeps the risk low while still removing most of the waste.
CSS, JavaScript and build tools
The CSS mode and JavaScript mode handle stylesheets and scripts the same way: they remove comments and whitespace but do not rename variables or restructure code. Build tools used by frameworks usually go further, with dead-code removal and name mangling for JavaScript.
For a site such as https://www.example.com, the order of impact is usually: compress responses, cache static assets with long lifetimes, optimise images, reduce JavaScript, and only then minify HTML. Minification is easy, but it is rarely the biggest performance win.
FAQ
Does minified HTML render differently?
It should not, but removing whitespace between inline elements can change small gaps or join words. Check the result visually.
Are conditional comments removed?
No. Comments starting with <!--[if are kept, because they carry meaning for some email clients and old browsers.
Is script and style content changed?
No. The contents of script and style blocks, as well as pre and textarea, are preserved exactly.
How much does minification save?
Often 10 to 20 percent of uncompressed HTML. With gzip or Brotli enabled, the extra saving is smaller.
Should I minify HTML emails?
Carefully. Some email clients have size limits and minifying helps, but test in the clients you care about, because email rendering is sensitive to markup changes.
Is my HTML uploaded?
No. Minification runs in your browser.
Does minification help SEO?
Indirectly at most, through slightly faster pages. Content, structure and overall performance matter far more.
Can I reverse minification?
Formatting tools can re-indent HTML, but removed comments are gone. Keep the original source and minify during builds.
Should I minify HTML on a site that uses a CDN?
Yes, if it is part of your build. CDNs compress responses, but minified input still produces slightly smaller compressed output and slightly faster parsing.
Does minification remove the doctype or html tags?
No. Only comments and whitespace are removed. Document structure, attributes and text content stay as they are.
Why is my inline SVG changed?
Whitespace between SVG tags is collapsed like other markup. Text inside SVG <text> elements may lose deliberate spacing; check such graphics after minifying.