CSS Custom Properties (CSS Variables)

14 / Jun / 2017 by Poonam Baveja 0 comments

CSS custom properties are the entities that are defined by CSS author themselves to obtain the feature of reusability of code all over the document. For example, A project may involve very complex CSS, and same color code may get repeated hundred times. So if we want to change that color, then we need to modify the color value hundred times. But using variables, we can change value at one place that can be referenced in multiple places.

Notation of a CSS variable is preceded by double dash “–” as shown below:

--primary-text-color: #cccccc;

And can be accessed as:

color: var(--primary-text-color);

Meaningful variable names are easier to understand like –primary-text-color is more meaningful then #cccccc.

Step1: Declaring a variable:

:root {
--main-bg-color: #f0f0f0;
}

Step2: Using a variable:

#variable-example h1 {
background-color: var(--main-bg-color);
}

Advantages:

  1. Reduce redundancy: When CSS variable values change (e.g. media query or another state), the browser repaints as needed.
  2. Can be accessible and manipulated using JavaScript.
  3. Potential to polyfill future CSS feature.
  4. Fully compatible with LESS and SASS.
  5. No need of a preprocessor.
  6. One of the major advantages of CSS variables is – they cascade means you can leverage custom properties inside of media queries to aid with responsive design. However, CSS preprocessors are unable to define variables inside of media queries.
:root {
--value: 4px;
}

section {
margin: var(--value);
}

@media (min-width: 600px) {
:root {
--value: 16px;
}
}

The syntax may seem strange at first look; a question may arise why it’s not just $gutter or %gutter or &gutter. The answer is to make it different from Sass and Less variables. So that it can be compatible with both. CSS variables are case sensitive and follow standard cascade rules so that you can define the same property at different levels of specificity.

CSS code:

:root { --text-color: blue; }
div { --text-color: green; }
#red-text { --text-color: red; }
* { color: var(--text-color); }

HTML code:

<p>This text is inside the paragraph</p>
<div>This is the text of div whose color is set to be green.</div></br>
<div id="red-text">
  This text is place inside a div with id - 'red-text'. That's why I am red in color.
  <p>This paragraph is place inside red-text div, that's why red in color because of inheritance.</p>
</div>
Just to test color of the body content.

The output of the above code is shown below:

This text is inside the paragraph
This is the text of div whose color is set to be green.
This text is placed inside a div with id – ‘red-texthandled That’s why I am red in color.
This paragraph is placed inside red-text div, that’s why red in color because of inheritance.
Just to test color of the body content.

Click here to see the Live Demo of the above example.

It is also possible to have custom properties that derive their value from other custom properties.

:root {
--basic-color: red;
--logo-text: var(--basic-color);
}

Rules:-
1) Variables cannot be property names.

.demo1 {
--gap: padding-top;
var(--gap): 25px;
}

Above CSS will throw an error.

2) A value can’t be (naively) build where part of it is provided by a variable:

.demo2 {
--side: 22;
margin-top: var(--side)px;
}

Above fall-back can be handled by using calc() function.

 .demo2 {
--side: 22;
margin-top: calc(var(--side) * 1px);
}

Use of CSS variables in JavaScript
To get the value of a custom property at runtime, getPropertyValue() method is used. Similarly, to set the value of a custom property at runtime, setProperty() method is used.

Browser support
Currently Chrome 49, Firefox 42, Safari 9.1, and iOS Safari 9.3 support custom properties.

Happy Learning

FOUND THIS USEFUL? SHARE IT

Leave a comment -