What is @csstools/postcss-relative-color-syntax?
@csstools/postcss-relative-color-syntax is a PostCSS plugin that allows you to use relative color syntax in your CSS. This can simplify color manipulations and make your stylesheets more maintainable by enabling relative color adjustments directly in your CSS.
What are @csstools/postcss-relative-color-syntax's main functionalities?
Relative Color Syntax
This feature allows you to use relative color syntax to mix colors directly in your CSS. The example shows how to mix white and black to get a gray background color.
/* Input CSS */
body {
background-color: color-mix(in srgb, white 50%, black);
}
/* Output CSS */
body {
background-color: #808080;
}
Color Adjustments
This feature allows you to adjust colors based on contrast. The example shows how to set a button's background color to blue while ensuring it contrasts well with both white and black.
/* Input CSS */
button {
background-color: color-contrast(white vs black, blue);
}
/* Output CSS */
button {
background-color: #0000ff;
}
Other packages similar to @csstools/postcss-relative-color-syntax
postcss-color-function
postcss-color-function is a PostCSS plugin that allows you to use color functions like color() in your CSS. It provides similar functionality to @csstools/postcss-relative-color-syntax but focuses more on the color() function and its capabilities.
postcss-preset-env
postcss-preset-env is a PostCSS plugin that allows you to use future CSS features today. It includes a variety of plugins, including those for color manipulation, making it a more comprehensive solution compared to @csstools/postcss-relative-color-syntax.
PostCSS Relative Color Syntax
PostCSS Relative Color Syntax lets you use the relative color syntax in CSS color functions following CSS Color Module 5.
.example {
background: oklab(from oklab(54.3% -22.5% -5%) calc(1.0 - l) calc(a * 0.8) b);
}
/* becomes */
.example {
background: rgb(12, 100, 100);
}
Usage
Add PostCSS Relative Color Syntax to your project:
npm install postcss @csstools/postcss-relative-color-syntax --save-dev
Use it as a PostCSS plugin:
const postcss = require('postcss');
const postcssRelativeColorSyntax = require('@csstools/postcss-relative-color-syntax');
postcss([
postcssRelativeColorSyntax()
]).process(YOUR_CSS );
PostCSS Relative Color Syntax runs in all Node environments, with special
instructions for:
Options
preserve
The preserve
option determines whether the original notation
is preserved. By default, it is not preserved.
postcssRelativeColorSyntax({ preserve: true })
.example {
background: oklab(from oklab(54.3% -22.5% -5%) calc(1.0 - l) calc(a * 0.8) b);
}
/* becomes */
.example {
background: rgb(12, 100, 100);
background: oklab(from oklab(54.3% -22.5% -5%) calc(1.0 - l) calc(a * 0.8) b);
}