The Before Times…

Before CSS was a thing, the only way to style HTML was to add styling information to each and every HTML element that you wanted to style. If you decided that you wanted all your H1 headings to be blue and all your H2 headings to be green, you had to add that information to each and every h1 and h2 element in your page. If you later decided to change your H1 headings to dark blue and your H2 headings to medium blue, you had to go through your code, line by line, and change each and every h1 and h2 element.

CSS changed all that. Now it was possible to add a simple <style> element to your document's head and declare all your style choices there. By putting this information in a separate stylesheet, you could easily ensure that all the pages across a website would have identical styling.

CSS is amazingly powerful. You can group selectors together, and you can use CSS classes to style multiple selectors or multiple elements, and use CSS ids to style a single element. However, it can still be difficult to change a group of elements.

That's where CSS variables come into play.

Basic Usage

A css variable can be declared in either a page's <style> element, or in an external stylesheet. Its scope will extend to wherever that stylesheet is referenced. A basic definition looks like this:

element {
  --variable-name: value;

A real-life defintion might look like this in the css for the definition:

ul {
  --bg-color: #000000;

…and like this in the use of that variable:

.main {
  background-color: var(--bg-color);

And like this in the html:

<ul class="main">
  <li>First list item</li>
  <li>Second list item</li>

The above variable works because we defined the --bg-color variable to operate under the scope of the <ul> element. To make a variable avaialable to all elements, define it under the root element:

:root {
  --bg-color: #000000;

What use is this?

While CSS classes make it easy to change the style of a number of different elements at once, CSS variables make it easy to change the style of a number of different selectors at once. This is valuable if you are still developing the overall appearance of a website, or if you know that the appearance will change later, and you want certain elements to share a particular style.

A Few Important Things to Note:

  1. Variable names must be prefaced by two hyphens (--).
  2. Varialbe names are case-sensive. --bg-Color is not the same as --bg-color.
  3. Variables are invoked using the var(--variable-name) construct.
  4. Variables must be defined for a given element, which is then their scope.
  5. To make a variable available to multiple elements, define it under the root element (:root). Note the leading colon.

This is an H1 Element

This is an H2 Element

This is an H3 Element

This is an H4 Element

Below is a paragraph

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate a erat id vehicula. Vestibulum viverra pulvinar velit non cursus. Curabitur euismod ullamcorper pellentesque. Quisque ac purus faucibus, dapibus diam suscipit, euismod enim. Curabitur aliquet enim sed metus rhoncus tempus. Etiam eget faucibus quam.

Below is another paragraph

Phasellus vitae convallis nibh. Quisque laoreet nulla tempus accumsan fermentum. Duis consequat velit vel enim cursus, efficitur lobortis enim ultricies. Aliquam vel lacinia turpis, vel eleifend libero. Aenean interdum eros tincidunt congue malesuada. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae.

This is an unordered list

This is an ordered list