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
Theming with Mixins in Sass
📖 Scenario: You are building a simple website that needs two color themes: light and dark. You want to use Sass mixins to apply these themes easily to different parts of your site.
🎯 Goal: Create a Sass file that defines two color themes using mixins. Then apply these themes to CSS classes .light-theme and .dark-theme so that the background and text colors change accordingly.
📋 What You'll Learn
Create a Sass map called $themes with keys light and dark and their color values
Create a mixin called theme-colors that takes a theme name and sets background and text colors
Use the mixin to style .light-theme and .dark-theme classes
Ensure the code is valid Sass and compiles to correct CSS
💡 Why This Matters
🌍 Real World
Websites often need multiple color themes for user preferences or branding. Using Sass mixins and maps helps manage these themes cleanly and reuse styles.
💼 Career
Front-end developers use Sass mixins and theming to build scalable, maintainable stylesheets that adapt to different design requirements.
Progress0 / 4 steps
1
Create the themes map
Create a Sass map called $themes with two keys: light and dark. Set light to a map with background: #ffffff and color: #000000. Set dark to a map with background: #000000 and color: #ffffff.
SASS
Hint
Use nested maps inside the main $themes map for each theme's colors.
2
Create the theme-colors mixin
Create a mixin called theme-colors that takes one parameter $theme-name. Inside the mixin, use background and color properties set to the values from the $themes map for the given $theme-name. Use map-get to access nested map values.
SASS
Hint
Use map-get twice: first to get the theme map, then to get the color value.
3
Apply mixin to .light-theme and .dark-theme classes
Create two CSS classes: .light-theme and .dark-theme. Inside each class, include the theme-colors mixin with the correct theme name: light for .light-theme and dark for .dark-theme.
SASS
Hint
Use @include theme-colors(light); inside .light-theme and similarly for .dark-theme.
4
Add responsive font size for themes
Inside both .light-theme and .dark-theme classes, add a CSS rule to set font-size to 1.2rem for screens wider than 600px using a media query.
SASS
Hint
Use a media query inside each theme class to set font-size: 1.2rem for screens wider than 600px.
Practice
(1/5)
1. What is the main purpose of using @mixin in Sass theming?
easy
A. To write plain CSS without nesting
B. To create reusable style blocks that can accept parameters
C. To import external CSS files
D. To define variables for colors and fonts
Solution
Step 1: Understand the role of @mixin
@mixin defines reusable style blocks that can be included multiple times.
Step 2: Recognize parameter use in mixins
Mixins can accept parameters to customize styles like colors or fonts.
Final Answer:
To create reusable style blocks that can accept parameters -> Option B
Quick Check:
@mixin = reusable styles [OK]
Hint: Mixins reuse styles with parameters, not variables or imports [OK]
Common Mistakes:
Confusing mixins with variables
Thinking mixins import files
Assuming mixins write plain CSS only
2. Which of the following is the correct syntax to include a mixin named theme-colors with a parameter $main-color set to blue?
easy
A. @include theme-colors($main-color: blue);
B. @mixin theme-colors($main-color: blue);
C. @include theme-colors blue;
D. @mixin theme-colors($main-color) { blue; }
Solution
Step 1: Identify the correct way to apply a mixin
Mixins are applied using @include, not @mixin.
Step 2: Check parameter passing syntax
Parameters are passed inside parentheses with variable names and values, like ($main-color: blue).
Final Answer:
@include theme-colors($main-color: blue); -> Option A
The mixin is included without any parameter, which causes an error.
Final Answer:
Mixin is included without required parameter -> Option D
Quick Check:
Missing parameter in @include = Mixin is included without required parameter [OK]
Hint: Always pass required parameters when including mixins [OK]
Common Mistakes:
Forgetting to pass parameters to mixins
Assuming default parameters without defining them
Ignoring error messages about missing arguments
5. You want to create a theme mixin that sets background and text colors, but if no text color is provided, it should default to black. Which mixin definition correctly implements this behavior?
C. @mixin theme($bg-color) {
background-color: $bg-color;
color: black;
}
D. @mixin theme($bg-color, $text-color) {
background-color: $bg-color;
color: $text-color;
}
Solution
Step 1: Understand default parameter usage
In Sass, default values for parameters are set using $param: default syntax.
Step 2: Check each option for default text color
@mixin theme($bg-color, $text-color: black) {
background-color: $bg-color;
color: $text-color;
} sets $text-color default to black, so if omitted, black is used.
Step 3: Verify other options
@mixin theme($bg-color, $text-color) {
background-color: $bg-color;
color: if($text-color, $text-color, black);
} tries to use if() function incorrectly; @mixin theme($bg-color) {
background-color: $bg-color;
color: black;
} lacks text color parameter; @mixin theme($bg-color, $text-color) {
background-color: $bg-color;
color: $text-color;
} has no default.