the code golfer’s color minifier for CSS!
A PostCSS plugin that aggressively minifies CSS color values:
- Shortens hex codes (
#aabbcc
→#abc
) - Converts
rgb()
/rgba()
to hex when shorter - Replaces color names with their shortest possible equivalent
- Designed for the most compact CSS output—perfect for code golf!
npm install postcss-color-golf --save-dev
import postcss from "postcss";
import postcssColorGolf from "postcss-color-golf";
postcss([
postcssColorGolf()
]).process(YOUR_CSS).then(result => {
console.log(result.css);
});
No
.default
needed! The dual build makes this work as expected.
const postcss = require("postcss");
const postcssColorGolf = require("postcss-color-golf");
postcss([
postcssColorGolf()
]).process(YOUR_CSS).then(result => {
console.log(result.css);
});
module.exports = {
plugins: [
require("postcss-color-golf")()
// ...other plugins
]
};
Type definitions are included for a great developer experience:
import postcss from "postcss";
import postcssColorGolf from "postcss-color-golf";
postcss([postcssColorGolf()]).process(cssString).then(result => {
console.log(result.css);
});
-
Shortens hex codes:
#aabbcc
→#abc
-
Converts rgb/rgba to hex:
rgb(255,0,0)
→#f00
rgba(0,255,0,0.5)
→#0f080
-
Replaces color names:
blue
→#00f
,fuchsia
→#f0f
- Aggressive color minification: Optimizes every color value it finds for the shortest possible output.
Input:
a {
color: rgb(255,0,0);
background: aliceblue;
border: 1px solid #aabbcc;
box-shadow: 0 0 3px rgba(0,255,0,0.5);
}
Output:
a {
color:#f00;
background:#f0f8ff;
border:1px solid #abc;
box-shadow:0 0 3px #0f080;
}
Currently, there are no options—just plug it in and go! Future versions may add configuration for specific minification strategies.
CC0 1.0 Universal (Public Domain Dedication) Use it for anything, commercial or personal, with or without attribution.