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.
What students will learn in this module
Every question in Module 63
Quick questions
- Where do you typically declare global CSS custom properties so they are accessible throughout the entire document?A:rootBbody
- Which prefix must all CSS custom property names begin with?A--B$
- 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
- 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
- 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 doesfont-size: var(--text-lg);Match it to what it doesDeclares a global custom property that can be used anywhere in the CSS file.Match it to what it doesUses a custom property with a fallback value of 10px if the variable is not defined.Match it to what it doesbody {
--main padding: 20px;
}Match it to what it doesh1 {
color: --theme-color;
}Match it to what it doesFill in the blanks
:root { ____________: blue; }Hint: Type the custom property name with the double dash prefix
body { color: ___(--text-color); }Hint: Type the CSS function name used to read a variable value
_____ { --gap-size: 10px; }Hint: Type the global pseudo-class selector used for global variables
h1 { margin: var(_______, 20px); }Hint: Type the property name argument before the fallback comma
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}
