A code minifier makes source files smaller by removing everything a browser or parser does not need: comments, indentation, line breaks and extra spaces. This free online code minifier handles JavaScript, HTML, CSS and XML in one place. Paste your code, pick the language, and you get a compact version plus the exact number of bytes saved, with nothing to install and nothing to upload.
Smaller files download faster, and faster downloads help pages load sooner on slow mobile connections. The tool is also handy when you want a quick check before a deploy and have no build pipeline at hand.
How to use the Code Minifier
- Choose the language: JavaScript, HTML, CSS or XML.
- Paste your code into the source box, or press Load sample to try an example.
- Press Minify, or use Ctrl + Enter (Cmd + Enter on a Mac).
- Check the percentage saved and the before and after sizes in the result card.
- Press Copy result and paste the minified code where you need it.
How a code minifier works
The tool applies careful text transforms that depend on the language you select. It reads your code, skips over quoted strings so their content is never touched, removes comments, and collapses whitespace wherever that is safe. It is not a full parser, so it does not rename variables or rewrite your logic.
What gets removed in each language
| Language | Removed | Left untouched |
|---|---|---|
| JavaScript | Line and block comments, indentation, extra spaces, most line breaks | Strings, template literals, regular expressions, line breaks that protect code without semicolons |
| CSS | Comments, spaces around braces, colons, commas and semicolons, the last semicolon in a block | Quoted strings, spaces inside values such as calc() |
| HTML | Comments, repeated whitespace, spaces between block-level tags, extra spaces inside tags | Conditional comments, pre and textarea content, spacing between inline elements |
| XML | Comments, whitespace between elements, extra spaces inside tags | CDATA sections and text content |
Why the tool plays it safe
JavaScript lets you leave out semicolons, and the engine adds them at some line breaks. If every line break were deleted, code written that way could stop working. The minifier therefore keeps a line break wherever removing it might change the meaning. In CSS, the spaces inside calc(1px + 2px) are required, so they are never stripped.
Worked examples
The examples below show what to expect. The symbol ↵ marks a line break, and there is no line break after the last line.
JavaScript
Input: function add(a, b) {↵ return a + b; // sum↵}. The minifier drops the comment, the indentation, the spaces after the comma and around the plus sign, and the line breaks. The output is function add(a,b){return a+b;}.
| Original | Minified | Saved |
|---|---|---|
| 45 bytes | 30 bytes | 15 bytes (33.3%) |
CSS
Input: .btn {↵ color: #fff;↵ margin: 0 auto;↵}. The output is .btn{color:#fff;margin:0 auto}. The space inside the value 0 auto stays, because the browser needs it to read two values.
| Original | Minified | Saved |
|---|---|---|
| 41 bytes | 30 bytes | 11 bytes (26.8%) |
HTML
Input: <div class="box">↵ <!-- note -->↵ <p>Hello, world</p>↵</div>. The comment disappears, the spaces around the block tags are removed, and the three spaces in the text become one. The output is <div class="box"><p>Hello, world</p></div>, which is 42 bytes instead of 64, a saving of 34.4%.
Benefits and common uses
- Faster pages: fewer bytes to download means less waiting, especially on mobile networks.
- Quick pre-deploy checks: compress a script or stylesheet without setting up a bundler.
- Email templates: trim inline CSS and HTML so templates stay under size limits.
- Size comparisons: see which files gain the most from minification before you decide where to spend time.
- Learning: compare input and output to see which characters are safe to remove in each language.
Tips for better results
- Select the correct language first, because each mode follows different rules.
- End your JavaScript statements with semicolons. The output will contain fewer line breaks.
- Keep the original source file. Minified code is difficult to read and edit.
- Test the minified result in your page or app before you publish it.
- Serve your files with gzip or Brotli as well. Minifying and server compression stack well.
When a build tool is the better choice
This code minifier removes comments and whitespace only. Production tools such as Terser, esbuild or cssnano also shorten variable names, fold constants and delete unused code, so they usually produce smaller files. Use this page for snippets, quick checks and files that live outside a build pipeline, and let your build tool handle full bundles.
Paste your code, press Minify and see how much you can save.