Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Token-driven color palettes
📖 Scenario: You are creating a simple style system for a website. You want to use color tokens to keep your colors organized and easy to change later.
🎯 Goal: Build a Sass file that defines color tokens, sets a threshold for light or dark colors, creates a map of colors filtered by that threshold, and finally applies these colors to CSS custom properties for easy use in your styles.
📋 What You'll Learn
Define a Sass map called $color-tokens with specific color names and hex values.
Create a variable $light-threshold to decide which colors are considered light.
Use a Sass @each loop and @if condition to create a new map $light-colors containing only colors lighter than the threshold.
Output CSS custom properties for each color in $light-colors inside the :root selector.
💡 Why This Matters
🌍 Real World
Token-driven color palettes help keep website colors consistent and easy to update by changing values in one place.
💼 Career
Understanding how to manage design tokens with Sass maps and output CSS variables is useful for frontend developers working on scalable and maintainable stylesheets.
Progress0 / 4 steps
1
Define the color tokens map
Create a Sass map called $color-tokens with these exact entries: primary: #3498db, secondary: #2ecc71, danger: #e74c3c, warning: #f1c40f, dark: #34495e.
SASS
Hint
Use parentheses to create a Sass map and separate each key-value pair with commas.
2
Set the light color threshold
Create a variable called $light-threshold and set it to 50% to represent the minimum lightness for colors to be considered light.
SASS
Hint
Use a simple variable assignment with a percentage value.
3
Create a map of light colors
Use a Sass @each loop with variables $name and $color to iterate over $color-tokens. Inside the loop, use an @if condition to check if lightness($color) is greater than $light-threshold. If true, add the color to a new map called $light-colors.
SASS
Hint
Initialize $light-colors as an empty map. Use map-merge() to add entries inside the loop.
4
Output CSS custom properties for light colors
Write a :root selector and inside it use a Sass @each loop with variables $name and $color to iterate over $light-colors. For each color, output a CSS custom property named --color-#{$name} with the value #{$color}.
SASS
Hint
Use interpolation #{$name} and #{$color} to insert variable values inside CSS.
Practice
(1/5)
1. What is the main purpose of using token-driven color palettes in Sass?
easy
A. To store colors in variables for easy reuse and consistency
B. To write colors directly in CSS without variables
C. To create animations with colors
D. To import images as color backgrounds
Solution
Step 1: Understand what tokens are in Sass
Tokens are variables that hold values, like colors, to reuse easily.
Step 2: Identify the benefit of using tokens
Using tokens keeps colors consistent and easy to update across the project.
Final Answer:
To store colors in variables for easy reuse and consistency -> Option A
Quick Check:
Color tokens = variables for consistent colors [OK]
Hint: Tokens are variables holding colors for reuse [OK]
Common Mistakes:
Thinking tokens are for animations
Confusing tokens with direct CSS colors
Assuming tokens import images
2. Which of the following is the correct way to define a color token in Sass?
easy
A. primary-color = #3498db;
B. $primary-color: #3498db;
C. var(--primary-color: #3498db);
D. color primary-color: #3498db;
Solution
Step 1: Recall Sass variable syntax
Sass variables start with a dollar sign ($) followed by the name and value.
Sass variables require a colon (:) between name and value.
Step 2: Locate the error
The code has $accent-color #00ff00; missing the colon after $accent-color.
Final Answer:
Missing colon after variable name -> Option D
Quick Check:
Variable declaration needs colon : [OK]
Hint: Variable declarations need colon : after name [OK]
Common Mistakes:
Forgetting colon in variable declaration
Removing $ from variable name
Assuming color value is wrong
5. You want to create a token-driven color palette with light and dark modes using Sass variables. Which approach correctly switches colors based on a data-theme attribute on the body, with light mode as the default?