Variables let you store values like colors or sizes to reuse them easily in your styles. Using $ helps you keep your code neat and change things quickly.
Variable declaration with $ in SASS
$variable-name: value;Start variable names with $ followed by letters, numbers, underscores, or hyphens.
Assign a value with a colon : and end with a semicolon ;.
$primary-color with a blue color value.$primary-color: #3498db;
$font-size: 1.5rem;
$spacing-large: 2rem;
This example shows how to declare variables for a main color and padding. Then it uses them inside a button style. The button changes color a bit when hovered.
@charset "UTF-8"; $main-color: #e74c3c; $padding: 1rem; .button { background-color: $main-color; padding: $padding; color: white; border-radius: 0.5rem; font-weight: bold; } .button:hover { background-color: darken($main-color, 10%); }
You can change the variable value once, and all places using it update automatically.
Variables make your code easier to read and maintain.
Use meaningful names so you remember what each variable is for.
Variables start with $ and store reusable values.
They help keep your styles consistent and easy to update.
Use variables for colors, sizes, spacing, and more.
Practice
Solution
Step 1: Recall Sass variable syntax
In Sass, variables always start with the$symbol to distinguish them from other values.Step 2: Identify the correct symbol
Among the options, only$is used for variables in Sass.Final Answer:
$ -> Option AQuick Check:
Variables start with $ in Sass [OK]
- Using # or @ instead of $ for variables
- Confusing CSS IDs (#) with Sass variables
- Omitting the $ symbol before variable names
Solution
Step 1: Check variable declaration syntax
Sass variables start with$, followed by the name, a colon, then the value, ending with a semicolon.Step 2: Compare options
$main-color: #3498db; matches the correct syntax:$main-color: #3498db;. Others use wrong symbols or order.Final Answer:
$main-color: #3498db; -> Option BQuick Check:
Variable declaration uses $name: value; [OK]
- Using = instead of : to assign values
- Placing $ after the variable name
- Using @ instead of $ for variables
$btn-bg: #ff0000;
button {
background-color: $btn-bg;
}Solution
Step 1: Identify the variable value
The variable$btn-bgis assigned the color#ff0000, which is red.Step 2: Check usage in button style
The button's background-color uses$btn-bg, so it will be red.Final Answer:
Red -> Option CQuick Check:
$btn-bg = #ff0000 (red) [OK]
- Confusing hex color codes with color names
- Forgetting to use $ before variable name
- Assuming default color if variable is used
$font-size 16px;
Solution
Step 1: Review correct variable syntax
Sass variables require a colon:between the name and value, like$font-size: 16px;.Step 2: Identify missing colon
The code misses the colon, causing a syntax error.Final Answer:
Missing colon between variable name and value -> Option DQuick Check:
Variable declaration needs colon : [OK]
- Omitting colon between name and value
- Using = instead of :
- Forgetting semicolon at end (less critical)
$spacing: 1rem;
@media (min-width: 600px) {
$spacing: 2rem;
}
.container {
padding: $spacing;
}Solution
Step 1: Understand Sass variable scope
Sass variables are static and cannot change inside media queries because media queries are CSS rules, not Sass logic blocks.Step 2: Evaluate the code behavior
Redefining$spacinginside@mediadoes not work; Sass processes variables before CSS is generated.Step 3: Correct approach
To handle responsive spacing, declare separate variables or use mixins/functions with media queries.Final Answer:
Variables cannot be redefined inside media queries -> Option AQuick Check:
Sass variables are static, not dynamic in media queries [OK]
- Trying to redefine variables inside media queries
- Assuming Sass variables behave like CSS custom properties
- Not using separate variables or mixins for responsiveness
Ready to prove it?
Pass the test and this pattern moves unknown
