← Imperial Products / Tools / Code Minifier

CSS, JavaScript & HTML Minifier

Paste your code to strip comments, collapse whitespace, and reduce file size. Runs entirely in your browser — nothing is sent to any server.

Input
Output
Original: — bytes
Minified: — bytes
Savings: —

What is Code Minification?

Minification is the process of removing all unnecessary characters from source code — including whitespace, comments, and redundant syntax — without changing its functionality. The result is a smaller file that loads faster in the browser and reduces bandwidth usage for both servers and visitors.

Modern web performance best practices require minified assets. Tools like Webpack, Rollup, and Vite do this automatically during a build step, but this tool lets you minify individual files instantly without any build setup.

CSS Minification

A CSS minifier removes block comments (/* ... */), strips unnecessary whitespace around selectors, properties, and values, eliminates redundant semicolons before closing braces, and collapses all multi-line rules onto a single line. The resulting file is functionally identical to the original but can be 30–80% smaller depending on how much whitespace and commentary was in the source.

JavaScript Minification

JavaScript minification is more complex than CSS because whitespace inside string literals must be preserved. This tool removes single-line (//) and multi-line (/* */) comments, collapses unnecessary whitespace between tokens, and strips redundant newlines — all while correctly handling single-quoted, double-quoted, and template literal strings so their contents are never altered.

HTML Minification

HTML minification removes HTML comments, collapses sequences of whitespace between tags to a single space, and removes optional closing tags for common void and structural elements. The page renders identically in all modern browsers.

Why Minify Your Code?

CSS vs JavaScript Minification

CSS minification is straightforward: the language has no string literals that could be confused with whitespace, so aggressive collapsing is safe. JavaScript is trickier because whitespace inside strings, regular expressions, and template literals must be left exactly as written. This tool uses a character-by-character state machine that tracks whether the parser is inside a string or comment before making any whitespace decisions, making it safe for real-world JS code.

For production deployments, consider also enabling Gzip or Brotli compression on your server. Minification and compression are complementary: minification reduces the number of unique characters, which makes compression more effective, typically achieving 60–90% total size reduction compared to the original source.

More Free Online Tools

\n' }; // ─── Mode switching ─────────────────────────────────────────────────────── function setMode(mode) { currentMode = mode; ['css', 'js', 'html'].forEach(m => { document.getElementById('tab-' + m).classList.toggle('active', m === mode); }); document.getElementById('inputArea').placeholder = placeholders[mode]; document.getElementById('outputArea').value = ''; resetStats(); } // ─── CSS minifier ───────────────────────────────────────────────────────── function minifyCSS(css) { // Remove block comments let result = css.replace(/\/\*[\s\S]*?\*\//g, ''); // Collapse whitespace (spaces, tabs, newlines) to a single space result = result.replace(/\s+/g, ' '); // Remove spaces around structural characters result = result.replace(/\s*([{};:,>~+])\s*/g, '$1'); // Remove trailing semicolons before closing braces result = result.replace(/;}/g, '}'); // Remove leading/trailing whitespace result = result.trim(); return result; } // ─── JavaScript minifier ───────────────────────────────────────────────── // State-machine approach: walk character by character, track string/comment state. function minifyJS(js) { const src = js; const len = src.length; let out = ''; let i = 0; while (i < len) { const ch = src[i]; // String: single quote if (ch === "'") { let s = ch; i++; while (i < len) { const c = src[i]; s += c; if (c === '\\') { i++; if (i < len) { s += src[i]; } } else if (c === "'") break; i++; } out += s; i++; continue; } // String: double quote if (ch === '"') { let s = ch; i++; while (i < len) { const c = src[i]; s += c; if (c === '\\') { i++; if (i < len) { s += src[i]; } } else if (c === '"') break; i++; } out += s; i++; continue; } // Template literal if (ch === '`') { let s = ch; i++; while (i < len) { const c = src[i]; s += c; if (c === '\\') { i++; if (i < len) { s += src[i]; } } else if (c === '`') break; i++; } out += s; i++; continue; } // Single-line comment if (ch === '/' && i + 1 < len && src[i + 1] === '/') { i += 2; while (i < len && src[i] !== '\n') i++; // Replace the comment with a space to avoid merging tokens out += ' '; continue; } // Multi-line comment if (ch === '/' && i + 1 < len && src[i + 1] === '*') { i += 2; while (i + 1 < len && !(src[i] === '*' && src[i + 1] === '/')) i++; i += 2; // skip */ out += ' '; continue; } // Whitespace (space, tab, newline, carriage return) if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') { // Collapse to a single space; skip if last output char was already a space // or is a character where no space is needed const last = out[out.length - 1]; if (last !== undefined && last !== ' ' && last !== '\n') { out += ' '; } i++; continue; } out += ch; i++; } // Collapse multiple spaces and trim out = out.replace(/ {2,}/g, ' '); // Remove spaces around operators that don't need them out = out.replace(/ *([\(\)\[\]\{\};,]) */g, '$1'); out = out.replace(/ *([=!<>+\-*/%&|^~?:]) */g, '$1'); // But don't collapse => // Re-add a single space after keywords that require it before a word character out = out.replace(/\b(var|let|const|function|return|if|else|for|while|do|switch|case|break|continue|new|delete|typeof|instanceof|in|of|void|throw|try|catch|finally|class|extends|import|export|default|from|async|await)\b/g, (m) => m); return out.trim(); } // ─── HTML minifier ──────────────────────────────────────────────────────── function minifyHTML(html) { // Remove HTML comments (but not IE conditionals /g, ''); // Collapse whitespace between tags result = result.replace(/>\s+<'); // Collapse runs of whitespace within text nodes to a single space result = result.replace(/\s{2,}/g, ' '); // Remove optional closing tags for common elements const optional = ['li', 'dt', 'dd', 'p', 'tr', 'td', 'th']; optional.forEach(tag => { const re = new RegExp('\\s*<\\/' + tag + '>', 'gi'); result = result.replace(re, ''); }); return result.trim(); } // ─── Core actions ───────────────────────────────────────────────────────── function doMinify() { const input = document.getElementById('inputArea').value; if (!input.trim()) return; let output; if (currentMode === 'css') output = minifyCSS(input); else if (currentMode === 'js') output = minifyJS(input); else output = minifyHTML(input); document.getElementById('outputArea').value = output; updateStats(input, output); } function doCopy() { const output = document.getElementById('outputArea').value; if (!output) return; navigator.clipboard.writeText(output).then(() => { const btn = document.getElementById('copyBtn'); btn.textContent = 'Copied!'; setTimeout(() => btn.textContent = 'Copy Output', 1600); }); } function doClear() { document.getElementById('inputArea').value = ''; document.getElementById('outputArea').value = ''; resetStats(); } // ─── Stats ──────────────────────────────────────────────────────────────── function byteSize(str) { return new Blob([str]).size; } function fmtBytes(n) { if (n < 1024) return n.toLocaleString() + ' B'; return (n / 1024).toFixed(1) + ' KB'; } function updateStats(original, minified) { const origSize = byteSize(original); const minSize = byteSize(minified); const saved = origSize - minSize; const pct = origSize > 0 ? ((saved / origSize) * 100).toFixed(1) : '0.0'; document.getElementById('statOriginal').textContent = fmtBytes(origSize); document.getElementById('statMinified').textContent = fmtBytes(minSize); document.getElementById('statSavings').textContent = saved > 0 ? fmtBytes(saved) + ' (' + pct + '% smaller)' : '0 B (no change)'; } function resetStats() { document.getElementById('statOriginal').textContent = '— bytes'; document.getElementById('statMinified').textContent = '— bytes'; document.getElementById('statSavings').textContent = '—'; } // ─── Init ───────────────────────────────────────────────────────────────── document.getElementById('inputArea').placeholder = placeholders['css'];