Variables in CSS help you reuse colors, sizes, or other values easily. They make changing styles faster and keep your code neat.
Using variables in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
:root {
--variable-name: value;
}
selector {
property: var(--variable-name);
}Variables start with two dashes -- and are usually defined inside :root for global use.
Use var(--variable-name) to apply the variable value.
:root {
--main-color: #3498db;
}
body {
background-color: var(--main-color);
}:root {
--padding-size: 1.5rem;
}
.container {
padding: var(--padding-size);
}:root {
--font-stack: 'Arial', sans-serif;
}
h1 {
font-family: var(--font-stack);
}This example shows how to define variables for colors and padding, then use them in the page styles. Changing the variable values in :root updates the whole page easily.
<!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: #2ecc71; --text-color: #333333; --padding: 1rem; } body { background-color: var(--primary-color); color: var(--text-color); font-family: Arial, sans-serif; padding: var(--padding); margin: 0; } h1 { font-size: 2rem; margin-bottom: var(--padding); } p { font-size: 1rem; line-height: 1.5; } </style> </head> <body> <h1>Welcome to CSS Variables</h1> <p>This page uses variables to keep colors and spacing consistent.</p> </body> </html>
Variables are case-sensitive, so --Main-Color and --main-color are different.
If a variable is not defined, the browser ignores it and uses the default or nothing.
You can override variables inside specific selectors to create themes or variations.
CSS variables store reusable values like colors and sizes.
Define variables inside :root for global use.
Use var(--variable-name) to apply the variable value in styles.
Practice
Solution
Step 1: Understand what CSS variables do
CSS variables hold values that can be reused throughout the stylesheet, such as colors or font sizes.Step 2: Compare options with this purpose
Only To store reusable values like colors and sizes describes storing reusable values; others describe unrelated tasks.Final Answer:
To store reusable values like colors and sizes -> Option AQuick Check:
CSS variables = reusable values [OK]
- Thinking CSS variables create HTML elements
- Confusing CSS variables with JavaScript
- Believing CSS variables add comments
Solution
Step 1: Recall CSS variable syntax
Variables are defined with two dashes and a colon inside a selector, usually :root for global scope.Step 2: Check each option's syntax
:root { --primary-color: #3498db; } uses correct syntax: :root { --name: value; }. Others have missing dashes, wrong selectors, or wrong assignment.Final Answer:
:root { --primary-color: #3498db; } -> Option AQuick Check:
Define variables with --name: value; inside :root [OK]
- Missing double dashes before variable name
- Using = instead of : for assignment
- Defining variables outside :root for global use
:root { --main-color: #ff0000; } p { color: var(--main-color); }Solution
Step 1: Identify the variable value
The variable --main-color is set to #ff0000, which is red.Step 2: Check how the variable is used
The paragraph's color is set using var(--main-color), so it uses the red color.Final Answer:
Red -> Option DQuick Check:
var(--main-color) = #ff0000 (red) [OK]
- Ignoring the variable and using default color
- Confusing hex codes with other colors
- Not using var() to apply variable
:root { --font-size 16px; } h1 { font-size: var(--font-size); }Solution
Step 1: Check variable definition syntax
The variable definition is missing a colon after --font-size; it should be --font-size: 16px;Step 2: Verify usage of var()
The usage var(--font-size) is correct in h1 font-size property.Final Answer:
Missing colon after --font-size in variable definition -> Option BQuick Check:
Variable definitions need colon after name [OK]
- Forgetting colon in variable definition
- Thinking var() usage is wrong
- Believing variable names can't start with --
Solution
Step 1: Define variables globally with correct syntax
Variables must be defined inside :root with double dashes and colon, e.g., --primary: #0000ff;Step 2: Use variables with var() in button styles
Apply variables using var(--primary) for background and var(--secondary) for border color.Final Answer:
:root { --primary: #0000ff; --secondary: #888888; } button { background-color: var(--primary); border: 2px solid var(--secondary); } -> Option CQuick Check:
Define with --name: value; use with var(--name) [OK]
- Omitting -- in variable names
- Not using var() when applying variables
- Defining variables inside button instead of :root
