How To Use CSS Custom Properties To Write More Readable Code

Before being hindered by the birthday of the US, I was discussing CSS custom properties, which on the off chance that you recall, permit you to make factors legitimately in CSS as opposed to utilizing preprocessors.

CSS custom properties can do a couple of things preprocessed factors can’t on the grounds that they aren’t aggregated before arriving at programs. This permits custom properties to course just as connect with the DOM/CSSOM powerfully.

In my past post I indicated you the punctuation to characterize custom properties just as how to utilize them through the var() work. What I didn’t offer was much in the method of viable models and I need to spend the following scarcely any weeks offering a few.

I’ll delineate a couple of fundamental ideas and give you thoughts for how you may utilize custom properties practically speaking and I’ll direct you toward more models I found while exploring this theme. I’ve separated the models into the accompanying ideas cssminifiers.


Intelligible Property Names

Logical styling

Responsive: powerful changes in @media questions

Non-Javascript dynamic changes

Javascript dynamic changes


I’ll show you the initial not many today and get again one week from now with models including @media inquiries.

Check for Program Backing with @supports

While custom properties have genuinely great help, it isn’t general. Worldwide help for custom properties is over 70% as I compose this, including from the most recent variant of every significant program. Notwithstanding, 70% is still under 100% and it’s a smart thought to verify that a program can deal with custom properties before utilizing them.

You can utilize @supports (92% program uphold with IE being the primary holdout) to physically check for help and afterward compose distinctive code dependent on the outcome. In the event that you’ve never worked with @supports, you can follow this overall example.

@supports ( (- – variable: 0)) {

/* upheld */


@supports ( not (- – variable: 0)) {

/* not upheld */


Here’s a model written in Backtalk. I characterized a Backtalk variable $color and furthermore a custom property – shading on the :root selector. I relegated both an estimation of red.

The h1 selector tests for help of the custom property through the @supports rule. The condition tried is setting an incentive on a custom property. On the off chance that it is upheld, the shade of the h1 utilizes the var() capacity to set the estimation of the custom property. If not, it utilizes the incentive after assemble of the Backtalk $color variable.

$color: red;

:root {

– shading: red;


h1 {

@supports ( – var: 0 ) {

shading: var(- – shading);


@supports ( not (- – var: 0)) {

shading: $color;



You may ask for what reason you’d do this and why not stay with the Backtalk variable since you realize it will work in all cases. The explanation is so the custom property can course and collaborate with the DOM where conceivable.

Related posts

Leave a Comment