CSS variables let you store values you want to reuse. This makes changing styles easier and faster.
Declaring variables in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
:root {
--variable-name: value;
}
.element {
property: var(--variable-name);
}Variables start with two dashes --.
Use var(--variable-name) to access the variable's value.
Examples
CSS
:root {
--main-color: #3498db;
}
h1 {
color: var(--main-color);
}CSS
:root {
--padding-size: 1.5rem;
}
.container {
padding: var(--padding-size);
}CSS
:root {
--font-stack: 'Arial', sans-serif;
}
body {
font-family: var(--font-stack);
}Sample Program
This example shows how to declare CSS variables for color, padding, and font size. The variables are used in the body and button styles. The media query changes padding and font size on small screens, showing how variables help with responsive design.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CSS Variables Example</title> <style> :root { --primary-color: #e67e22; --padding: 2rem; --font-size: 1.25rem; } body { font-family: Arial, sans-serif; background-color: var(--primary-color); padding: var(--padding); font-size: var(--font-size); color: white; } button { background-color: white; color: var(--primary-color); border: none; padding: 0.5rem 1rem; font-size: var(--font-size); cursor: pointer; border-radius: 0.25rem; } button:hover { background-color: #d35400; color: white; } @media (max-width: 600px) { :root { --padding: 1rem; --font-size: 1rem; } } </style> </head> <body> <h1>Welcome to CSS Variables</h1> <p>This page uses variables for colors, padding, and font size.</p> <button>Click Me</button> </body> </html>
Important Notes
CSS variables are case-sensitive and must start with --.
You can override variables inside selectors or media queries for flexible designs.
Variables help keep your CSS DRY (Don't Repeat Yourself).
Summary
Declare CSS variables inside :root for global use.
Use var(--variable-name) to apply the variable's value.
Variables make styling easier to maintain and update.
Practice
1. What is the main purpose of declaring CSS variables inside
:root?easy
Solution
Step 1: Understand the role of
The:rootin CSS:rootselector targets the highest-level element in the document, usually the<html>element.Step 2: Recognize variable scope
Declaring variables inside:rootmakes them global, so they can be used anywhere in the CSS.Final Answer:
To make variables available globally throughout the CSS -> Option CQuick Check:
Global variables =:rootdeclaration [OK]
Hint: Declare variables in
:root for global access [OK]Common Mistakes:
- Thinking variables declared in
:rootare local - Confusing CSS variables with JavaScript variables
- Assuming variables only work inline
2. Which of the following is the correct syntax to declare a CSS variable named
--main-color with the value #3498db inside :root?easy
Solution
Step 1: Recall CSS variable declaration syntax
CSS variables start with two dashes--and are declared with a colon:inside a selector block.Step 2: Check each option
:root { --main-color: #3498db; } uses correct syntax:--main-color: #3498db;. Others use invalid symbols or keywords.Final Answer:
:root { --main-color: #3498db; } -> Option AQuick Check:
Correct syntax =--name: value;[OK]
Hint: Use
--name: value; inside :root [OK]Common Mistakes:
- Using = instead of : to assign values
- Missing the double dash
--prefix - Trying to use
var()in declaration
3. Given the CSS below, what color will the paragraph text be?
:root {
--text-color: #ff0000;
}
p {
color: var(--text-color);
}medium
Solution
Step 1: Identify the variable value
The variable--text-coloris set to#ff0000, which is red.Step 2: Check how the variable is used
The paragraph usescolor: var(--text-color);, so it will use the red color.Final Answer:
Red -> Option AQuick Check:
Variable value applied = red [OK]
Hint: Match variable value with
var(--name) usage [OK]Common Mistakes:
- Confusing hex codes with color names
- Forgetting to use
var()to apply variables - Assuming default color if variable is declared
4. What is wrong with this CSS code?
:root {
--bg-color #ffffff;
}
body {
background-color: var(--bg-color);
}medium
Solution
Step 1: Check variable declaration syntax
The declaration--bg-color #ffffff;is missing a colon:between the variable name and value.Step 2: Verify usage of variable
The usagebackground-color: var(--bg-color);is correct, so the error is only in declaration.Final Answer:
Missing colon ':' after variable name in declaration -> Option DQuick Check:
Declaration syntax requires ':' [OK]
Hint: Always put ':' between variable name and value [OK]
Common Mistakes:
- Forgetting colon ':' in variable declaration
- Using var() without parentheses
- Misspelling property names
5. You want to create a theme with two colors:
--primary-color and --secondary-color. You want --secondary-color to be 50% transparent version of --primary-color. Which CSS variable declaration correctly achieves this?hard
Solution
Step 1: Understand CSS variable limitations
CSS variables hold values as strings; you cannot directly usevar(--primary-color)insidergba()expecting it to split into RGB components.Step 2: Check each option
:root { --primary-color: #0000ff; --secondary-color: rgba(var(--primary-color), 0.5); } tries to usergba(var(--primary-color), 0.5)but--primary-coloris a hex string, so this won't work.
:root { --primary-color: 0, 0, 255; --secondary-color: rgba(var(--primary-color), 0.5); } declares--primary-coloras RGB components but CSS variables cannot hold multiple values like that easily.
:root { --primary-color: #0000ff; --secondary-color: #0000ff80; } uses a hex color with alpha channel#0000ff80which is blue with 50% opacity, correctly representing a transparent version.
:root { --primary-color: #0000ff; --secondary-color: var(--primary-color, 0.5); } misusesvar()with a second parameter that is not opacity.Final Answer:
:root { --primary-color: #0000ff; --secondary-color: #0000ff80; } -> Option BQuick Check:
Use hex with alpha for transparency in variables [OK]
Hint: Use hex with alpha channel for transparent colors [OK]
Common Mistakes:
- Trying to use var() inside rgba() with hex colors
- Declaring RGB as comma-separated string in variable
- Misusing var() fallback parameter as opacity
