Design tokens help keep colors, fonts, and sizes consistent across your website. They make it easy to change styles in one place and see updates everywhere.
Design token management in SASS
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
SASS
$token-name: value;Design tokens are usually stored as variables in Sass using the $ sign.
Use clear names for tokens to know what they control, like $color-primary or $font-size-base.
Examples
SASS
$color-primary: #3498db;
SASS
$font-size-base: 1rem;
SASS
$spacing-small: 0.5rem;
Sample Program
This example shows how to define design tokens as Sass variables and use them in CSS rules. The button changes color slightly on hover using a Sass color function.
SASS
@use 'sass:color'; // Design tokens $color-primary: #3498db; $color-secondary: #2ecc71; $font-size-base: 1rem; $spacing-base: 1rem; // Using tokens in styles body { font-size: $font-size-base; color: $color-primary; margin: $spacing-base; } button { background-color: $color-secondary; color: white; padding: $spacing-base $spacing-base * 2; border: none; border-radius: 0.25rem; font-size: $font-size-base; cursor: pointer; } button:hover { background-color: color.scale($color-secondary, $lightness: -10%); }
Important Notes
Use relative units like rem for font sizes and spacing to improve accessibility and responsiveness.
Keep token names simple and meaningful to make your code easier to understand.
Design tokens can be grouped in separate files for better organization in bigger projects.
Summary
Design tokens are variables that store style values like colors and sizes.
They help keep your website's look consistent and easy to update.
Use Sass variables to create and manage design tokens in your stylesheets.
Practice
1. What is the main purpose of design tokens in Sass?
easy
Solution
Step 1: Understand design tokens concept
Design tokens are variables that hold style values such as colors, fonts, and sizes.Step 2: Identify their purpose in Sass
They help keep styles consistent and easy to update by reusing these values.Final Answer:
To store reusable style values like colors and sizes -> Option CQuick Check:
Design tokens = reusable style values [OK]
Hint: Design tokens store style values for reuse [OK]
Common Mistakes:
- Thinking design tokens are for animations
- Confusing design tokens with JavaScript code
- Believing design tokens manage HTML
2. Which of the following is the correct way to declare a design token for a primary color in Sass?
easy
Solution
Step 1: Recall Sass variable syntax
Sass variables start with a dollar sign ($) and use a colon to assign values.Step 2: Check each option
$primary-color: #3498db; uses correct Sass syntax:$primary-color: #3498db;. Others use JavaScript or invalid syntax.Final Answer:
$primary-color: #3498db; -> Option DQuick Check:
Sass variables start with $ and use : [OK]
Hint: Sass variables start with $ and use colon : [OK]
Common Mistakes:
- Using JavaScript variable syntax in Sass
- Omitting the $ sign before variable name
- Using = instead of : for assignment
3. Given the Sass code:
What will be the computed font size for the body element?
$font-size-base: 1.6rem;
$font-size-large: $font-size-base * 1.5;
body {
font-size: $font-size-large;
}What will be the computed font size for the body element?
medium
Solution
Step 1: Calculate $font-size-large value
$font-size-large is $font-size-base multiplied by 1.5, so 1.6rem * 1.5 = 2.4rem.Step 2: Apply value to body font-size
The body font-size uses $font-size-large, so it will be 2.4rem.Final Answer:
2.4rem -> Option AQuick Check:
1.6rem * 1.5 = 2.4rem [OK]
Hint: Multiply base size by factor for large size [OK]
Common Mistakes:
- Using 1.5rem instead of multiplying
- Confusing base size with large size
- Forgetting to multiply the values
4. Identify the error in this Sass code for managing design tokens:
$color-primary: #ff0000
$color-secondary: #00ff00;
.button {
background-color: $color-primary;
}medium
Solution
Step 1: Check variable declarations
$color-primary is missing a semicolon at the end of the line, which is required in Sass.Step 2: Verify other parts
Variable names and background-color property are correct. Hex colors are valid.Final Answer:
Missing semicolon after $color-primary declaration -> Option AQuick Check:
Each Sass variable line must end with ; [OK]
Hint: Always end Sass variable lines with semicolon ; [OK]
Common Mistakes:
- Omitting semicolons after variable declarations
- Thinking hex colors are invalid in Sass
- Misnaming variables without $ sign
5. You want to create a design token system in Sass that allows easy theme switching between light and dark modes. Which approach below best manages color tokens for this purpose?
hard
Solution
Step 1: Understand theme switching needs
Theme switching requires grouping colors so you can easily swap them based on mode.Step 2: Evaluate options for managing tokens
Using separate Sass maps for light and dark themes allows selecting the active theme map with a variable, making switching easy and maintainable.Step 3: Reject other options
Hardcoding colors or manual changes are error-prone and not scalable. JavaScript alone ignores Sass benefits.Final Answer:
Define separate maps for light and dark colors, then use a variable to select the active map -> Option BQuick Check:
Use maps and variables for theme switching [OK]
Hint: Use Sass maps and a variable to switch themes [OK]
Common Mistakes:
- Hardcoding colors instead of using variables
- Ignoring Sass variables and relying only on JavaScript
- Manually changing colors everywhere instead of grouping
