CSS Variables Complete Guide
Tags: css, webdev, html, softwebtuts
Published: 2020-10-30 07:40:19

The variable is a term that is used in every programming or designing or markup language. Variables are some text strings that can be used throughout our programs and can be changed according to the needs of the programmer.<br/>

CSS variables are variables that can be used in CSS style sheets. If you don't know what is CSS and what CSS stylesheets are then you should know it first, take help from google.<br/><br/>

 Like other programming languages in CSS, variables can be used <mark>locally</mark> and <mark>globally</mark> depends upon how they are defined.<br/><br/>

 If you define your CSS variable using <strong>root pseudo-class</strong> then you can use that CSS variable globally but if you define a CSS variable for a specific element then you can use that variable only in that element's style code.

<h2>Defining &amp; Using CSS Variables:</h2>
To Define CSS variable you simply have to put 2 dashes (--) before the variable name. For example ( <mark>--background: #fc801c </mark>).<br/><br/>

Here you can see it clearly that <strong>--background</strong> is a variable and hex colour code is the value of that variable. If you see the variable name clearly then you will see that I have added 2 dashes <mark>(--)</mark> before the variable name. <br/><br/>

So calling CSS variable you have to follow some rules and to use CSS variables anywhere you have to enter the variable name with dashes (<mark>--</mark>) inside <strong>var function ( <mark>var()</mark> )</strong>. <br/><br/>

This function is used to call any variable at any place in CSS style code. Below you can see the examples of defining and using variables in CSS style sheets.<br/><br/>

For Example : <mark>background: var(--background)</mark>

<h2>Assigning &amp; Using CSS Variable Locally:</h2>
To use the CSS variable locally you have to see the example code provided below. In the example below, you can clearly see how I have used the CSS variable to use locally for one element style code.<br/><br/>

```/* 

Assigning & Using CSS Variables 
Locally for one element style. 

*/

#MyID {
  --background: #fc801c;
/*    Defining Variable ( --background  ) with value    */

  background: var(--background);
/*    Calling Variable ( var(--background) )    */
}
```
In the example code above I have defined a variable <strong>--background</strong> locally and this variable cannot be used in any other elements style code.<br/><br/>

Let's take a look at how we can define and use variables globally

<h2>Defining and Using CSS Variables Globally:</h2>
To use any CSS variable globally, you have to define inside CSS variables in <mark>:root pseudo-class</mark> selector in CSS.<br/><br/>

Variables defined inside the :root pseudo-class selector in CSS can be used globally in any elements style code. Take a look at the example below.

```
/* 

Assigning & Using CSS Variables 
Globally. 

*/

:root{
    --background: #fc801c;     /* Defining Variable */
}

#MyID {
  background: var(--background);    /*Calling Variable*/
}

#MyID2 {
  background: var(--background);    /*Calling Variable*/
}
```

<h2>Conclusion:</h2>
This is how you can define and use CSS variables locally and globally I think it's clear to you that how you can assign CSS variables in your stylesheets.

Want to see More? <a href='https://softwebtuts.blogspot.com/2020/10/css-variables-complete-guide.html' target='_blank'>Come Here</a>