0
0
CSSmarkup~5 mins

Declaring variables in CSS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a CSS variable?
A CSS variable is a custom property that stores a value you can reuse throughout your CSS. It helps keep styles consistent and easy to update.
Click to reveal answer
beginner
How do you declare a CSS variable?
You declare a CSS variable inside a selector using two dashes before the name, like --main-color: blue;. Usually, variables are declared inside the :root selector for global use.
Click to reveal answer
beginner
How do you use a CSS variable in your styles?
Use the var() function with the variable name, like color: var(--main-color);. This applies the stored value wherever you use it.
Click to reveal answer
beginner
Why use CSS variables instead of repeating values?
CSS variables make it easy to update styles in one place. For example, changing --main-color updates all uses of that color, saving time and reducing mistakes.
Click to reveal answer
intermediate
Can CSS variables have fallback values?
Yes! You can provide a fallback value inside var(), like color: var(--unknown-color, black);. If the variable is not defined, the fallback is used.
Click to reveal answer
How do you declare a CSS variable named --font-size with value 16px globally?
A--font-size: 16px;
Bbody { font-size: --font-size: 16px; }
C:root { --font-size: 16px; }
Dvar(--font-size) = 16px;
Which syntax correctly uses a CSS variable named --main-color for text color?
Acolor: var(--main-color);
Bcolor: --main-color;
Ccolor: $main-color;
Dcolor: main-color;
What happens if you use color: var(--unknown-color, red); but --unknown-color is not defined?
AThe browser shows an error.
BThe color will be red.
CThe color will be transparent.
DThe color will be black.
Why is it good to declare CSS variables inside :root?
AIt disables variables.
BIt hides variables from the browser.
CIt makes variables only work on the body.
DIt makes variables available globally.
Which of these is NOT a benefit of using CSS variables?
AVariables automatically change HTML content.
BEasier to update repeated values.
CVariables can store any CSS value.
DVariables help keep styles consistent.
Explain how to declare and use a CSS variable for a background color.
Think about where to put the variable and how to apply it in CSS.
You got /4 concepts.
    Describe the benefits of using CSS variables in a website's style.
    Consider how variables save time and reduce errors.
    You got /4 concepts.