CSS custom properties (variables), online

Six activities your students do on their own devices. See who's finished, and their scores.

CSS custom properties (variables) in one minute

What students need to know

The :root pseudo-class corresponds to the html element and serves as the highest-level global scope. A comma inside the var() function separates the variable name from its default fallback value.

CSS custom properties must start with a double dash (--). The dollar sign ($) is used in preprocessors like Sass. Unlike most CSS identifiers, custom property names are case-sensitive.

Mistakes students make

  • Line 1 is incorrect because CSS custom properties must begin with a double dash prefix (--), not a single dash.
  • Line 2 is incorrect because when referencing a custom property, the name inside var() must include the double dash prefix (--text-color).
  • Line 0 is incorrect because custom properties must be declared as property-value pairs inside a normal CSS ruleset selector.
  • Line 3 is incorrect because a custom property value must be retrieved by wrapping its name in the var() function syntax.
PREVIOUS · MODULE 62CSS transforms
YOU ARE HERE · MODULE 63CSS custom properties (variables)

Discover other journeys with this module

What students will learn in this module

Every question in Module 63

Quick questions

  1. Where do you typically declare global CSS custom properties so they are accessible throughout the entire document?A:rootBbody
  2. Which prefix must all CSS custom property names begin with?A--B$
  3. Is the function syntax var(--color, blue) a valid way to provide a fallback value if --color is not defined?ANo, var() only accepts one argumentBYes, separated by a comma
  4. Are CSS custom property names case-sensitive (meaning --main-color and --Main-Color are different)?ANo, CSS is always case-insensitiveBYes, they are case-sensitive
  5. Can you change the value of a CSS custom property dynamically inside a media query or a hover state?AYesBNo

Key terms

Students type the meaning of:

  • var()

Match the pairs

--primary-color: #ff0000;Match it to what it does
font-size: var(--text-lg);Match it to what it does
Declares a global custom property that can be used anywhere in the CSS file.Match it to what it does
Uses a custom property with a fallback value of 10px if the variable is not defined.Match it to what it does
body {
  --main padding: 20px;
}
Match it to what it does
h1 {
  color: --theme-color;
}
Match it to what it does

Fill in the blanks

  1. :root {
      ____________: blue;
    }

    Hint: Type the custom property name with the double dash prefix

  2. body {
      color: ___(--text-color);
    }

    Hint: Type the CSS function name used to read a variable value

  3. _____ {
      --gap-size: 10px;
    }

    Hint: Type the global pseudo-class selector used for global variables

  4. h1 {
      margin: var(_______, 20px);
    }

    Hint: Type the property name argument before the fallback comma

  5. div {
      padding: var(_____________);
    }

    Hint: Type the complete custom property name inside the function

Spot the error

Question 1

A.item {B  -theme-color: red;C}

Question 2

Abody {B  background-color: #fff;C  color: var(text-color);D}

Question 3

Avar(--main-size): 16px; {B  font-size: 16px;C}

Question 4

A:root {B  --main-padding: 15px;C}Ddiv { padding: --main-padding; }

Question 5

Ap {B  font-size: var(14px);C  color: black;D}