Tech Blog

Coding, Programming

Variables in CSS

By Irshan Ahmed2 min read
← Back to blog

CSS variables (also referred as CSS custom properties) are just like simple variables in other programming languages. They reduce the redundant work. They are used to storing values and reused at multiple places and updated/modified from one place.

Syntax:

CSS variable is any CSS property whose name starts with two dashes. Like

Custom property names are case-sensitive,  –-primary-color  and  –-Primary-Color will be treated as separate. 

Var() Function:

To use custom property value var()function is used.

The var() function retrieves and replaces itself with the custom property value, resulting in color: #212b36; As long as the custom property is defined somewhere in the stylesheet it should be available to the var function.

var()function allows two arguments, 

–custom-name: This parameter accepts the name of the custom property.

value: This is optional parameter and it accepts the fallback value.

Cascade:

Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc

Benefits: 

  • CSS variables remove the redundancy in the code.
  • Cleaner Stylesheets – CSS variables make code more readable and easier to maintain.
  • Flexible – CSS Variables can be declared anywhere.
  • Access in JavaScript: CSS variables values can be easily read and written by JavaScript.
  • Easier theming: CSS variables can be used to theme and skin entire pages, components.

Browser compatibility: 

The browser supported by custom properties are listed below:

  • Google Chrome 49.0
  • Microsoft Edge 15.0
  • Firefox 42.0
  • Safari 9.1
  • opera 36.0

Keep reading

You may be interested in

Browse all articles →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →
Angular constructor vs ngOnInit
September 15, 20223 min read

Angular constructor vs ngOnInit

Overview: If you are new to angular or have been working with it for a while, you will have questions like: Is it necessary to inject dependencies in the constructor and bind data? When should we use ngOnInit ? What is the difference between constructor and OnInit?. So, in this blog we will answer all […]

By Priyanka SRead More →
Angular – Difference between ng-container and ng-template
July 13, 20222 min read

Angular – Difference between ng-container and ng-template

Overview: ng-container and ng-template, both are angular elements which will work like containers with HTML elements, but with different purpose/use cases. Lets see what is the use of ng-container and ng-template below: ng-container: In some cases when you do not want to add an extra HTML element to the DOM , for example like div […]

By Priyanka SRead More →