CSS Variables (the basics)
Tags: css, beginners, codenewbie
Published: 2021-03-18 15:16:05

##Terminology:
The following terminology will be used in this article when talking about CSS declarations:
* <u>**Selector**</u>: Part of the CSS rule outside the curly brackets- tells the browser which HTML elements should have the CSS declarations applied to them. *Eg:* `h1`
* <u>**Declaration**</u>: A property and value pair, the two are separated by a colon. *Eg:* `color: green;`
* <u>**Property**</u>: The left side of the declaration, this specifies what to actually style. *Eg:* `color`
* <u>**Value**</u>: Right side of the declaration, the value can be made up of keywords/values that determine the style applied by the property. *Eg:* `green;`
![CSS Declaration Diagram](https://henessyceballos.files.wordpress.com/2018/02/css-syntax.gif)
*source*:[<u>*henessyceballos*</u>](https://henessyceballos.com/2018/02/09/day-39-cascade/)

&nbsp;
&nbsp;


##What Are CSS Variables? 
CSS variables (*also known as CSS custom properties or cascading variables*), are entities that have been assigned a specific CSS value. 
This can be any valid CSS value including, but not limited to:

* Fonts (eg: `times new roman'`).
* Colors (eg: `#50E3C2`).
* Sizes (eg: `50px`).

<p>A CSS variable is normally declared for a value that appears often in the document. Here is an example where many selectors that have the value `blue` assigned to their properties:</P>
```CSS
div {
 border: 1px solid blue;
 color: blue;
}

h1 {
 background: blue;
}

p {
 color: blue;
}
```
A CSS variable can store the value `blue`. The variable can then be reused throughout the document. Should the value `blue` ever need to be updated, it then only has to be updated in one place- where the variable is declared.

&nbsp;
&nbsp;

##CSS Variables Syntax
```CSS
:root {
 --my-variable: red;
}

p {
 color: var(--my-variable);
}
```
*An example of a CSS variable being declared and used- this code renders a p tag with red text.*

&nbsp;

###Declaring and Assigning CSS variable:
```CSS
:root {
  --my-variable: red;
}
```
CSS variables can be declared on any CSS selector, in the example `:root` has been used, the reasoning behind this is explained later on. More than one CSS variable can be declared on a selector.

The whole statement to declare and assign a CSS variable should look something like this: `--my-property: 200px;`

This can be broken down into 3 parts:
1. The variable **name**(*left side*):
 - Variable names must start with the double-hyphen(--). 
 - After the double hyphen, any naming convention/casing can be used. 
 - `--myVariable`, `--MY_VARIABLE` and `--my-variable` are all valid names.
&nbsp;
⚠️<u>Note:</u> CSS variable names are *case sensitive*, so these names **cannot** be used interchangeably.

2. The **colon**(*separator*):
 - The variable name must be followed by a colon(:) before the value can be declared.
3. The variable's **value**(*right side*):
 - Any valid CSS value can be set.
 - Once the value is declared it must be followed by a semi-colon(;)

&nbsp;

###Accessing a CSS variable:
```CSS
p {
 color: var(--my-variable);
}
```
When writing a CSS declaration that uses a CSS variable, the *property* in question (`color`) does not change. However, the way the *value* is written does.
The var() function is used to assign the value from a CSS variable to a CSS property, this is done like so:`var(--my-variable)`. 
The var() function takes a CSS variable as a parameter, this function call then replaces what would normally be written on the right side of the colon in a CSS declaration.
For example: `color: red;` would be replaced with `color: var(my-variable);`

&nbsp;

###CSS Variables- Scope
CSS variables can be declared on any CSS selector. The selector they are declared on determines the scope of the variable. Only the selector (parent) in question, and any child elements of it, will have access to that variable.

For this reason, common best practice is to declare variables in the `:root` pseudo class. This gives the variables global scope, meaning they can be accessed by any elements in the document.

&nbsp;

###CSS Variables- Inheritance:
CSS variables follow the typical cascade, meaning they inherit values from their parent and can be overwritten further down the document. Below is an example of how CSS variables can be used to overwrite color of link on hover:
```CSS
:root {
 --link-color: green;
}

a {
 color: var(--link-color);
}
a:hover{
 --link-color: red;
}
```
If no value is set for a variable on an element, the value will be inherited from that element's parent.

&nbsp;
&nbsp;

##Why CSS Variables are Useful:
* They help to avoid repetition, making the code DRYer.
* It makes it easier to update code - by simply updating the value assigned to the variable in the root, everywhere that variable is used will be updated.
* They can make code more readable - at a glance it's easier to understand the purpose of `--main-bg-color` than `rgba(23, 24, 56, .8)`.
* They can be used to make cool effects pretty easily, in very few lines of code- especially if you throw in a bit of JavaScript *like in this CodePen where I made a simple dark-mode toggle:*
{% codepen https://codepen.io/hibernathan/pen/VwmRpzd %}

&nbsp;
&nbsp;

##Conclusion:
CSS variables allow a value to be stored in one place, so it can be referenced in many other places throughout a document.
These can be really handy in making your CSS cleaner, more efficient and easier to read. You might want to consider using them in your code if you don't already!

&nbsp;
&nbsp;

##Some Resources:
Some places to find out more about CSS variables:
* W3schools: [CSS Variables - The var() Function] (https://www.w3schools.com/css/css3_variables.asp) [*article*]
* MND Web Docs: [Using Custom Properties] (https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties) [*article*]
* Rob Dodson: [CSS Variables: Why Should You Care?](https://developers.google.com/web/updates/2016/02/css-variables-why-should-you-care) [*article*]
* Kevin Powell: [CSS Variables - An introduction to CSS custom properties](https://www.youtube.com/watch?v=PHO6TBq_auI&t=31s) [*video*]
* MadeByMike: [Using CSS Variables Correctly] (https://www.madebymike.com.au/writing/using-css-variables/) [*article*]