# Code Minifier

> The Code Minifier removes comments, whitespace and other unneeded characters from CSS, JavaScript (using Terser) and HTML, then shows the original size, minified size and the percentage saved.

URL: https://atechcraft.com/code-minifier/
Category: Developer Tools
Last updated: 2026-12-01
Author: Abdul Moid Khan, ATechCraft

Runs in your browser. Your data never leaves your device.

## How to use the Code Minifier

1. Choose CSS, JavaScript or HTML.
2. Paste your code and click Minify.
3. Compare the sizes and savings percentage.
4. Copy or download the minified code.

## What is a code minifier?

A code minifier makes CSS, JavaScript and HTML files smaller by removing everything the browser doesn't need: comments, extra spaces, line breaks and, for JavaScript, long variable names. The code works the same but downloads faster. This tool minifies all three languages in your browser and shows the size before and after, plus the percentage saved.

Smaller files mean faster pages, especially on mobile data. Google's guidance on [page experience and Core Web Vitals](https://web.dev/articles/vitals) rewards pages that load quickly.

## How does minification work for each language?

| Language | Engine | What it removes or changes |
| --- | --- | --- |
| JavaScript | [Terser](https://terser.org/) (BSD licence) | Comments, whitespace, dead code; shortens local variable names; simplifies expressions |
| CSS | ATechCraft's own minifier | Comments, whitespace around `{ } : ; , > + ~`, the last `;` in each block, `0px` → `0`, `0.5` → `.5` |
| HTML | ATechCraft's own minifier | Comments, whitespace between tags, runs of spaces |

**JavaScript** is the most complex. Terser parses your code into a syntax tree, so it understands scope. It can safely rename a local variable `totalPrice` to `t`, because it knows nothing outside the function uses that name. It keeps comments that start with `/*!`, which are usually licence notices. Terser is loaded only when you click Minify in JavaScript mode, so it doesn't slow down the page.

**CSS** strings like `content: "  x  "` are protected before spaces are removed, so they keep their exact spacing. Comments starting with `/*!` are kept too.

**HTML** keeps everything inside `<pre>`, `<textarea>`, `<script>` and `<style>` exactly as it is, because whitespace matters there. Conditional comments for old Internet Explorer are also kept.

## Worked example (CSS)

Input (106 bytes):

```css
/* Card styles */
.card {
  margin: 0px;
  padding: 16px ;
  opacity: 0.5;
}
.card > h2 { color : #333; }
```

Output (59 bytes):

```css
.card{margin:0;padding:16px;opacity:.5}.card>h2{color:#333}
```

The tool reports **106 B → 59 B, 44.3% saved**. On a real stylesheet with long comments the saving is often 20–40%. Exact numbers depend on how the original is written.

## Should I minify by hand?

For a live website, it's better to minify automatically in your build process. Frameworks such as Next.js, Vite and WordPress optimisation plugins already minify CSS and JavaScript. This tool is handy for:

- a quick snippet you're pasting into a CMS or a third-party widget,
- checking how much a file would shrink,
- small static sites without a build step.

Always keep the original, readable source. Minified code is hard to edit.

## Tips

1. **Compress on the server too.** Minification and gzip or Brotli compression work together; you get the best result with both.
2. **Test after minifying.** Load the page and check the browser console for errors.
3. **Format JSON separately.** For JSON, use the [JSON Formatter](https://atechcraft.com/json-formatter/)'s Minify button instead.
4. **Compare versions** of your source with the [Text Diff Checker](https://atechcraft.com/text-diff/), not the minified files.
5. **Build CSS visually** with the [CSS Gradient Generator](https://atechcraft.com/css-gradient-generator/), then minify the result here.
6. **Embedding a small image in CSS?** Convert it with the [Base64 tool](https://atechcraft.com/base64-encoder-decoder/).

## Is minified code harder to debug?

Yes, which is why build tools create source maps: files that map each part of the minified code back to the original lines, so browser developer tools can show readable code while the site serves the small version. This tool doesn't create source maps, so keep your original file and fix bugs there, then minify again.

## Limitations

- The CSS and HTML minifiers are deliberately conservative: they don't merge duplicate rules, shorten colours (`#ffffff` → `#fff`) or remove optional HTML tags. Dedicated build tools such as cssnano and html-minifier-terser go further.
- HTML minification removes whitespace between tags, which can occasionally remove a visible space between inline elements like two links. Check the result.
- Inline `<script>` and `<style>` blocks inside HTML are not minified.
- If your JavaScript has a syntax error, Terser stops and shows the line and column.

## Frequently asked questions

### Will minifying break my code?

Terser is a widely used, safe JavaScript minifier. The CSS and HTML minifiers here are conservative: they keep content inside pre, textarea, script and style blocks. Always test minified code.

### How much smaller will my files be?

It depends on comments and formatting. Gzip or Brotli compression on your server also reduces size, so minification and compression work together.

### Does it rename variables?

Yes for JavaScript, Terser mangles local variable names. Global names and object properties are kept.

### Should I minify by hand?

For production sites, use a build tool that minifies automatically. This tool is handy for quick snippets and checks.
