Amazing Uses for CSS Custom Properties

What are css properties?

Variables are the unit in computer programming that holds a value.

CSS variables are, therefore, unique style variables that can be used anywhere in a CSS document. What makes this different from standard CSS, though?

When writing a CSS declaration, you usually start with the property, which is the style that the declaration is targeting, and then the value, which is the sort of styling applied. This declaration is linked to a CSS selector in the following way:

Because CSS did not contain variables until recently, this was the only way to give values to properties in plain CSS for a long. You’d have to use a CSS preprocessing language like Sass to get anything that looked like variables in CSS (which is incorporated into frameworks like Bootstrap).

However, CSS variables now provide an alternative means of style to our page elements without needing an add-on solution.CSS variables, also known as custom properties, allow us to construct our unique values and make our code easier to comprehend and change.

See Also: How to Use CSS’s Clip Property

Primary Usage of CSS Properties

A custom property name that begins with a double hyphen (–) and a property value that can be any valid CSS value is used to declare it. This, like every other property, is written in a ruleset as follows:

(you can go into more detail about roots later)

var() can be used to get the value of a variable:

Any CSS value which is valid can be used as the variable value, for example:

Uses of Custom Properties in CSS

Cascading Variables

Setting the variable on the root element signifies that the variable is accessible to the root’s descendants. Variables can be declared on more precise selectors instead.

Every element would have access to the first variable. Only divs and their descendants come before divs and their descendants, and the latter only applies to items with the box class and their descendants. The standard cascade and specificity rules decide which variable is utilized on a given selection.

For example, if we have a div in our HTML with the class box and we set the box class in our css.

Now have a look at the resultant color.

See Also: CSS’s word-wrap Property

Make Calculation Easier

Custom properties are great for storing computed values (using the calc() method), which may also be calculated from other custom properties. As previously indicated, one example is determining complementary colors, and another example is when you need to find the inverse of a property. I just created an article for CSS Tricks on using custom properties to calculate the reverse of an easing curve.

I frequently utilize custom properties with clip-path when I need to compute a route relative to another or known variables. The clip-path locations for two pseudo-elements are calculated in the following code from a recent demo to provide the impression of an element being bisected.

Shorthand Properties

If you’re using a shortcut property like animation and need to update one value for a different element, rewriting the entire property can be error-prone and add to the maintenance effort. We can quickly change a single value in the shorthand property using custom properties:

To Break Up Multi-Part Values

Let’s suppose you’re utilizing the RGB color function (). This offers many options, such as adjusting the alpha value for a particular use case or establishing color themes.


We can break it down into pieces and move the pieces around as needed. Perhaps we’re dealing with a button’s background color. When the switch is hovered, in focus, or disabled, we may alter select sections of its HSL composition without having to declare a background in any of those situations.

See Also: How to Use CSS’s vertical-align Property


Because there is no such thing as targeting just one value of a comma-separated list and modifying it alone, any property that allows multiple comma-separated values might be a potential candidate for splitting values.


These aren’t the only reasons for custom properties, but they’re the ones I frequently use in my workflow, and they may help you write more efficient and maintainable code. You’ll undoubtedly come up with a slew of new ideas!

Share and Enjoy !

0 0