What does the CSS Minifier & Code Compressor do?
The CSS Minifier & Code Compressor strips CSS comments (/* ... */), collapses extra whitespace and line breaks, normalizes zero units (0px $\to$ 0), and trims trailing property semicolons to produce compact, high-performance stylesheet assets.
Core Concepts
Understanding CSS minification rules and AST compression:
- Comment Stripping: Removes multiline comments (
/* ... */) that are non-functional in production runtimes. - Whitespace & Delimiter Optimization: Collapses redundant line breaks and strips whitespace around CSS syntax characters (
{,},:,;,,). - Zero-Unit Normalization: Replaces units on zero values (
0px,0em,0rem,0%) with unitless0according to CSS specifications.
How to use the tool?
- Paste CSS Code: Enter or paste your unminified CSS rules into the editor or click Load Sample.
- Configure Settings: Toggle Remove comments to purge comment blocks or leave unchecked to keep licensing headers.
- Compress & Export: Click Minify CSS Code, then click Copy or Download to save your compressed stylesheet.
Related Developer Utilities
If you work with web development, stylesheets, and asset optimization, explore these complementary tools:
- CSS Formatter & Beautifier: Re-indent and format minified CSS stylesheets.
- CSS Unit Converter: Convert pixel values to responsive rem, em, and % units.
- HTML Minifier & Compressor: Compress HTML markup by stripping whitespace.
- JSON Minifier Tool: Compact JSON data payloads for production APIs.
REST API Integration
Blueutils provides a free REST API endpoint (POST https://blueutils.com/api/css/css-minifier) to programmatically minify CSS stylesheets, strip comments, collapse whitespace, and optimize property rules.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText |
String | Raw CSS stylesheet payload to minify. | ".card { color: red; }" |
options |
Object | Optional settings (removeComments: boolean). |
{"removeComments": true} |
API Request Payload Examples
cURL
curl -X POST https://blueutils.com/api/css/css-minifier \
-H "Content-Type: application/json" \
-d '{
"rawText": ".container { width: 100%; margin: 0px auto; }",
"options": { "removeComments": true }
}'Python
import requests
url = "https://blueutils.com/api/css/css-minifier"
payload = {
"rawText": ".container { width: 100%; margin: 0px auto; }",
"options": { "removeComments": True }
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"rawText": ".container { width: 100%; margin: 0px auto; }",
"options": { "removeComments": true }
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/css/css-minifier"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether minification succeeded. | true |
minifiedCss |
String | Compressed single-line CSS string. | ".container{width:100%;margin:0 auto}" |
stats |
Object | Reduction metrics (originalBytes, minifiedBytes, reductionPercentage). |
{"originalBytes":45} |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"minifiedCss": ".container{width:100%;margin:0 auto}",
"result": ".container{width:100%;margin:0 auto}",
"stats": {
"originalBytes": 45,
"minifiedBytes": 32,
"reductionBytes": 13,
"reductionPercentage": "28.89%"
}
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "Invalid input: CSS payload to minify cannot be empty."
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to minify CSS?
Integrating the CSS Minifier API into frontend build pipelines, CMS deployment scripts, or AI agent tool calling provides key benefits:
- Rapid Script Validation: Prepares optimized static asset bundles before deploying code to edge CDNs and web servers.
- Optimized Token Efficiency for AI Agents: LLMs generate verbose, multiline CSS styles. Invoking the API compresses stylesheet strings by 30% to 50% without risking invalid property syntax.
- Deterministic Accuracy Without Hallucinations: Ensures 100% accurate zero-unit conversions and comment stripping without dropping essential selector rules.
Native Usage
How to minify CSS files locally in terminal environments or scripts:
Windows (CMD / PowerShell)
# Minify CSS in PowerShell
(Get-Content -Path .\style.css -Raw) -replace '/\*[\s\S]*?\*/', '' -replace '\s+', ' ' -replace '\s*([{\}:;,])\s*', '$1' | Set-Content style.min.cssLinux / Unix (Bash)
# Minify CSS in Linux using tr and sed
tr -d '\n' < style.css | sed -E 's/\/\*.*\*\///g; s/ */ /g; s/ *([{:;,]) */\1/g' > style.min.cssPython
Using Python re:
import re
with open("style.css") as f:
css = f.read()
css = re.sub(r'/\*[\s\S]*?\*/', '', css)
css = re.sub(r'\s+', ' ', css)
css = re.sub(r'\s*([{\}:;,])\s*', r'\1', css)
css = re.sub(r';}', '}', css)
with open("style.min.css", "w") as f:
f.write(css.strip())
print("CSS minified successfully.")Java
Using Java regex:
import java.nio.file.Files;
import java.nio.file.Paths;
public class MinifyCssExample {
public static void main(String[] args) throws Exception {
String css = new String(Files.readAllBytes(Paths.get("style.css")));
String min = css.replaceAll("/\\*[\\s\\S]*?\\*/", "")
.replaceAll("\\s+", " ")
.replaceAll("\\s*([{\\}:;,])\\s*", "$1")
.replaceAll(";}", "}")
.trim();
Files.write(Paths.get("style.min.css"), min.getBytes());
System.out.println("CSS minified successfully.");
}
}