Default values let you set a variable only if it hasn't been set before. This helps avoid overwriting important settings.
Default values with !default in SASS
$variable: value !default;The !default flag sets the variable only if it is not already set.
If the variable has a value, the !default assignment is ignored.
$primary-color to blue only if it is not set yet.$primary-color: blue !default;$font-size to 16px. The second is ignored because $font-size is already set.$font-size: 16px !default; $font-size: 18px !default;
$margin is already 1rem, the !default assignment to 2rem does nothing.$margin: 1rem; $margin: 2rem !default;
This example shows how !default keeps the user's $primary-color and does not overwrite it with the default blue. The background uses the user's color, and the text color is a darker shade.
@use 'sass:color'; // User sets a color before importing defaults $primary-color: #ff6347; // Default color set with !default $primary-color: #3498db !default; body { background-color: $primary-color; color: color.scale($primary-color, $lightness: -40%); }
Use !default to make your Sass variables flexible and easy to override.
Place default variables in a separate file to keep your code organized.
Remember, !default only works if the variable is not set yet.
Default values let you set variables only if they are not already set.
Use !default to avoid overwriting user or previously set values.
This helps make your styles flexible and customizable.
Practice
!default flag do when used with a Sass variable?Solution
Step 1: Understand the purpose of
The!default!defaultflag in Sass is used to assign a value to a variable only if it has not been assigned a value before.Step 2: Compare with other options
The other options describe behaviors that!defaultdoes not perform. It does not make the variable global regardless of scope, force overwrites of previous values, or delete the variable after use.Final Answer:
It sets the variable only if it has not been set before. -> Option BQuick Check:
!defaultmeans set if unset [OK]
- Thinking !default always overwrites values
- Confusing !default with variable scope
- Assuming !default deletes variables
blue to a Sass variable $color?Solution
Step 1: Recall Sass variable assignment syntax
Sass variables are assigned using$variable-name: value;. To set a default value, add!defaultafter the value.Step 2: Check each option
$color: blue; assigns a value but does not use!default. $color: blue !default; correctly uses!default. $color = blue !default; uses an invalid assignment operator=. $color: blue !important; uses!importantwhich is CSS, not Sass variable syntax.Final Answer:
$color: blue !default; -> Option DQuick Check:
Correct syntax uses colon and !default [OK]
- Using = instead of : for variable assignment
- Confusing !default with !important
- Omitting !default when intending default value
$font-size after compilation?$font-size: 16px !default; $font-size: 18px;
Solution
Step 1: Understand the effect of !default on first assignment
The first line sets$font-sizeto 16px only if it is not already set. Since it is the first assignment, it sets$font-sizeto 16px.Step 2: Analyze the second assignment without !default
The second line assigns$font-sizeto 18px unconditionally, overwriting the previous value.Final Answer:
18px -> Option AQuick Check:
Later assignment overwrites default [OK]
- Assuming !default prevents any overwrite
- Thinking duplicate assignments cause errors
- Ignoring order of variable declarations
$primary-color: red !default $primary-color: blue;
Solution
Step 1: Check syntax of variable declarations
In Sass, each variable declaration must end with a semicolon. The first line is missing a semicolon after!default.Step 2: Verify other options
Assigning two values to the same variable is allowed; the later overwrites the earlier. The use of!defaultis correct. Variables can be declared globally, not only inside selectors.Final Answer:
Missing semicolon after first variable declaration. -> Option CQuick Check:
Every Sass statement ends with semicolon [OK]
- Omitting semicolon after !default
- Thinking multiple assignments cause errors
- Believing variables must be inside selectors
!default to achieve this?Solution
Step 1: Understand how !default works with user overrides
The!defaultflag sets a variable only if it is not already set. To allow user override, the user must set the variable before this file is loaded.Step 2: Analyze each option
$button-color: green; $button-color: blue !default; sets$button-colorfirst without!default, then tries to set default, which won't work as intended. $button-color: blue !default; $button-color: green; sets default first, then overwrites it, so user cannot override easily. $button-color: blue !default; sets default but does not mention user override. $button-color: blue !default; // User sets $button-color: green before importing this file correctly shows default with comment explaining user sets variable before import, enabling override.Final Answer:
Use !default and let user set variable before import. -> Option AQuick Check:
User sets variable before default with !default [OK]
- Setting default after user value
- Not allowing user to set variable before import
- Confusing order of assignments
Ready to prove it?
Pass the test and this pattern moves unknown
