CSS Minifier & Beautifier
Paste CSS and it is minified on the spot: comments and extra whitespace go, the last semicolon of each block goes, and numbers and colors are shortened only where that cannot change the result. The example is 589 bytes; minified it is 420 bytes (28.7% smaller), and beautified with 2 spaces it is 507. The text is read by a real CSS tokenizer, not by search and replace, so a string that contains /* */, a url() with a space, the spaces inside calc(1px + 2px) and custom property values all come out exactly as they went in. Rules are never reordered. You also get the gzip estimate, and you can copy or download the result. Your CSS never leaves the browser.
Updated Runs in your browser: nothing you enter is uploadedHow this tool works
Replace the example: paste or type CSS. It is processed as you type.
Your CSS is processed on this device and never uploaded. It is not saved either: reload the page and it is gone.
Result
Minified: 589 → 420 bytes, −169 bytes (−28.7%)
Problems found in the CSS
The result is still produced; the odd parts are copied as written.
Size
- Before (UTF-8)
- 589 bytes
- After (UTF-8)
- 420 bytes
- Change
- −169 bytes (−28.7%)
- Before, gzip estimate
- —
- After, gzip estimate
- —
What gets changed and what is left alone
| CSS in | Minified | Why |
|---|---|---|
| a { color : red ; margin : 0 auto ; } | a{color:red;margin:0 auto} | Whitespace around punctuation goes, and so does the last semicolon of the block. |
| a { margin: 0px 0.50em; top: -0.0em } | a{margin:0 .5em;top:0} | Numbers lose trailing zeros and a leading 0; a zero length drops its unit where a bare 0 is allowed. |
| a { color: #AABBCC; border-color: #112234 } | a{color:#abc;border-color:#112234} | A color shortens only when every pair of digits repeats. |
| a::before { content: "/* not a comment */" } | a::before{content:"/* not a comment */"} | Strings are copied exactly, including text that looks like a comment. |
| a { background: url( "my photo.png" ) } | a{background:url("my photo.png")} | A quoted url() keeps its inner space; only the padding around the quotes goes. |
| a { width: calc( 100% - ( 2 * 10px ) ) } | a{width:calc(100% - (2 * 10px))} | Spaces around + and - inside calc() are required by the syntax, so they stay. |
| a { width: calc(0px + 1em); height: max(0px, 2vh) } | a{width:calc(0px + 1em);height:max(0px,2vh)} | No unit is dropped inside math functions: calc(0 + 1em) would be invalid. |
| :root { --gap : 0px ; --c: #aabbcc } | :root{--gap:0px;--c:#aabbcc} | Custom property values are kept as written, apart from surrounding spaces. |
| a { transition: opacity 0s; transform: rotate(0deg) } | a{transition:opacity 0s;transform:rotate(0deg)} | Times and angles keep their unit: a bare 0 is not valid there. |
| a { flex: 1 1 0px } | a{flex:1 1 0px} | In the flex shorthand 0px and 0 are not interchangeable, so it is left alone. |
| a b , c > d { x: y } | a b,c>d{x:y} | The descendant space stays; spaces around commas and combinators go. |
| a :hover, a:hover { x: y } | a :hover,a:hover{x:y} | A space before a colon changes the selector, so it stays. |
| :is( h1 , h2 ) > p:nth-child( 2n + 1 ) { x: y } | :is(h1,h2)>p:nth-child(2n + 1){x:y} | Selector lists compact, but 2n + 1 keeps its operator spaces. |
| @media (min-width : 600px) and (max-width:900px) { a { x: y } } | @media (min-width:600px) and (max-width:900px){a{x:y}} | The words of a media query stay apart; the colon spacing goes. |
| @font-face { unicode-range: U+0025-00FF , U+4?? } | @font-face{unicode-range:U+0025-00FF,U+4??} | unicode-range is never reinterpreted: U+0025 is not the number 25. |
| a { color: red ! important } | a{color:red!important} | !important is kept, with its spacing removed. |
Options
| Option | Effect |
|---|---|
| Minify | Removes every comment and all optional whitespace, and the last semicolon of each block. |
| Beautify | Puts one declaration per line, indents nested blocks, and starts every selector of a list on its own line. |
| 2 spaces / 4 spaces | The indent of each nesting level when beautifying. |
| Remove all comments | The default: every /* … */ comment is dropped when minifying. |
| Keep /*! license */ comments | Comments that start with /*! are kept in place; all others are dropped. |
| Keep all comments | Nothing is removed; comments stay where they were. Choose this when beautifying. |
| Shorten colors and zero lengths | Switches the number, 0px and #aabbcc shortening described above on or off. Whitespace and comments are handled either way. |
The example in each layout
| Layout | Bytes | Change from the original |
|---|---|---|
| Original | 589 | — |
| Minified | 420 | −169 bytes (−28.7%) |
| Beautified, 2 spaces | 507 | −82 bytes (−13.9%) |
| Beautified, 4 spaces | 539 | −50 bytes (−8.5%) |
How to minify or beautify CSS online
- Paste your CSS. Replace the example with your own stylesheet. It is processed as you type; for very large files, press Run instead.
- Pick minify or beautify. Minify gives the smallest text. Beautify lays the CSS out with 2 or 4 space indents so it is easy to read and diff.
- Choose what to keep. Keep /*! license */ comments if your file needs its notice, or keep all comments when beautifying. Turn off shortening if you want only whitespace and comments handled.
- Check the sizes. The bytes before and after show what you saved; the gzip estimate shows roughly what a server would send.
- Copy or download. Copy the result, or download it as style.min.css or style.css. Nothing is uploaded at any step.
Frequently asked questions
- Is it safe to minify my CSS here?
- The minifier only makes changes that cannot alter what the browser reads. It tokenizes your CSS the way a browser does, copies strings, url() addresses, escapes and custom property values unchanged, and removes whitespace only next to punctuation where it never matters. It never reorders, merges or deletes rules. If your CSS is malformed the page warns you and still returns the best output it can, with the odd parts copied as written.
- Why are spaces inside calc() kept?
- In calc(), the + and - operators must have whitespace on both sides: calc(1px+2px) is invalid, while calc(1px + 2px) is valid. The minifier therefore never removes the spaces around them. It also never turns calc(0px + 1em) into calc(0 + 1em), which would add a bare number to a length and be invalid.
- When is 0px shortened to 0, and when not?
- A bare 0 is accepted in place of a zero length, so 0px, 0em and 0rem become 0 in ordinary declarations such as margin and padding. Times and angles (0s, 0deg) and percentages (0%) keep their unit, the flex shorthand is left alone, nothing changes inside calc(), min(), max(), clamp() or var() fallbacks, and custom properties are never touched. The option can be switched off entirely.
- Why is #aabbcc shortened but #aabbcd not?
- A six-digit hex color can be written with three digits only when each pair of digits is the same, so #aabbcc becomes #abc and #112234 stays as it is. The same holds for eight digits with an alpha value: #aabbccdd becomes #abcd. Hash values in selectors (#main), strings and url() are not colors and are never rewritten.
- What happens to /*! license */ comments?
- By default every comment is removed. Choose “Keep /*! license */ comments” and the comments that begin with /*! stay where they were, with a line break after a top-level one, while all other comments are dropped. “Keep all comments” leaves every comment in place, which is the sensible choice for beautifying.
- Does it work with nested CSS, @media and @layer?
- Yes. Rules are read as nested lists of statements, so @media, @supports, @layer, @container, @keyframes and native CSS nesting (including & selectors) are all minified and beautified in place, at any depth.
- What does the gzip size mean?
- It is an estimate made with your browser’s built-in compression, shown for the text before and after. A server may use another gzip level or Brotli, so the real transfer size differs somewhat, but the comparison between before and after is a good guide. If your browser has no compression support, the line is simply left out.
- Is my CSS uploaded or saved?
- No. Everything happens in your browser, the text is not stored anywhere (not even in local storage), and it is gone when you reload the page. The only thing recorded, if you allow analytics, is that a button was used, never any of the text.
- How big a file can I paste?
- Several megabytes work. Up to 500,000 characters the result updates a moment after you stop typing; above that the page waits for you to press Run so typing stays smooth.
How it works
The text is first preprocessed as CSS Syntax Level 3 describes (line endings normalized, NUL replaced) and split into tokens by an in-house tokenizer that follows its tokenization algorithm: identifiers, functions, at-keywords, hashes, strings, url tokens, numbers, percentages, dimensions, delimiters and brackets. Comments are kept as tokens so that the chosen ones can be preserved. The tokens are grouped into rules and statements by brace and parenthesis nesting; a statement with a block is a rule, one that ends in a semicolon is a declaration or an at-rule such as @import. Each statement is written back from its own tokens. A space is kept wherever it could change the meaning: between values, before a pseudo-class (a :hover is not a:hover), around the operators in calc() and in An+B expressions, and between the words of a media query. It is dropped only next to commas, semicolons, braces, brackets, selector combinators, the colon of a declaration and !important. Number shortening works on the number token as written and never on strings, url() addresses, selectors, custom properties, unicode-range or IE filter values. Rules are never sorted, merged or removed. Sizes are UTF-8 byte counts.
Sources
More free tools
- PX to REM ConverterConvert px to rem, em, pt, % and vw/vh with an editable root font size, see a px to rem table, or paste CSS and convert every px value to rem at once.
- XML Formatter & ValidatorCheck XML is well-formed with the exact line and column of any error, then pretty-print with 2 spaces, 4 spaces or a tab, or minify. Runs in your browser.
- SVG OptimizerMinify SVG code or an .svg file safely: removes metadata, editor data and unused ids, rounds numbers, previews both. Runs in your browser.
- SQL FormatterFormat and beautify SQL: one clause per line, one column per line, indented subqueries and CASE, keywords in your case. Or minify. Runs in your browser.
- HTML Formatter & BeautifierBeautify messy HTML with 2 spaces, 4 spaces or tabs, or minify it. Script, style, pre and textarea content stays exactly as typed. Runs in your browser.
- CSS Border Radius GeneratorSet four corners together or apart in px, % or rem, use the elliptical a / b syntax, preview the shape live and copy the shortest border-radius CSS.
More: Formatters and data convertersAll developer toolsAll toolsHome & DIY calculators