PostCSS Color Functional Notation lets you use space and slash separated color notation in CSS, following the CSS Color specification.
:root {
--firebrick: rgb(178 34 34);
--firebrick-a50: rgb(70% 13.5% 13.5% / 50%);
--firebrick-hsl: hsla(0 68% 42%);
--firebrick-hsl-a50: hsl(0 68% 42% / 50%);
}
/* becomes */
:root {
--firebrick: rgb(178, 34, 34);
--firebrick-a50: rgba(178, 34, 34, .5);
--firebrick-hsl: hsl(0, 68%, 42%);
--firebrick-hsl-a50: hsla(0, 68%, 42%, .5);
}
Add PostCSS Color Functional Notation to your build tool:
npm install postcss-color-functional-notation --save-dev
Use PostCSS Color Functional Notation to process your CSS:
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
postcssColorFunctionalNotation.process(YOUR_CSS, /* processOptions */, /* pluginOptions */);
Add PostCSS to your build tool:
npm install postcss --save-dev
Use PostCSS Color Functional Notation as a plugin:
import postcss from 'gulp-postcss';
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
postcss([
postcssColorFunctionalNotation(/* pluginOptions */)
]).process(YOUR_CSS);
Add PostCSS Loader to your build tool:
npm install postcss-loader --save-dev
Use PostCSS Color Functional Notation in your Webpack configuration:
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader', options: {
ident: 'postcss',
plugins: () => [
postcssColorFunctionalNotation(/* pluginOptions */)
]
} }
]
}
]
}
}
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Use PostCSS Color Functional Notation in your Gulpfile:
import postcss from 'gulp-postcss';
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
gulp.task('css', () => gulp.src('./src/*.css').pipe(
postcss([
postcssColorFunctionalNotation(/* pluginOptions */)
])
).pipe(
gulp.dest('.')
));
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Use PostCSS Color Functional Notation in your Gruntfile:
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
use: [
postcssColorFunctionalNotation(/* pluginOptions */)
]
},
dist: {
src: '*.css'
}
}
});
The preserve
option determines whether the original functional color notation
is preserved. By default, it is not preserved.
postcssImageSetFunction({ preserve: true })
:root {
--firebrick: rgb(178 34 34);
--firebrick-a50: rgb(70% 13.5% 13.5% / 50%);
--firebrick-hsl: hsla(0 68% 42%);
--firebrick-hsl-a50: hsl(0 68% 42% / 50%);
}
/* becomes */
:root {
--firebrick: rgb(178, 34, 34);
--firebrick: rgb(178 34 34);
--firebrick-a50: rgba(178, 34, 34, .5);
--firebrick-a50: rgb(70% 13.5% 13.5% / 50%);
--firebrick-hsl: hsl(0, 68%, 42%);
--firebrick-hsl: hsla(0 68% 42%);
--firebrick-hsl-a50: hsla(0, 68%, 42%, .5);
--firebrick-hsl-a50: hsl(0 68% 42% / 50%);
}