📖 Scenario: You are building a simple webpage that needs to look good on phones, tablets, and desktops. To do this, you will create a Sass mixin that helps you write styles that change at different screen widths (called breakpoints).
🎯 Goal: Create a Sass mixin called respond-to that accepts a breakpoint name and applies styles only when the screen width matches that breakpoint. Use this mixin to change the background color of a .box element for phone, tablet, and desktop sizes.
📋 What You'll Learn
Create a Sass map called $breakpoints with keys phone, tablet, and desktop and values 480px, 768px, and 1024px respectively.
Create a mixin called respond-to that takes a parameter $breakpoint and uses the $breakpoints map to apply a media query with min-width.
Use the respond-to mixin to set the background color of the .box class to lightblue on phones, lightgreen on tablets, and lightcoral on desktops.
Ensure the Sass code compiles correctly and the styles apply responsively in the browser.
💡 Why This Matters
🌍 Real World
Responsive design is essential for websites to look good on all devices. Using Sass mixins for breakpoints helps developers write cleaner and reusable code.
💼 Career
Front-end developers often use Sass and responsive design techniques to build flexible, user-friendly websites that work well on phones, tablets, and desktops.
Progress0 / 4 steps
1
Create the breakpoints map
Create a Sass map called $breakpoints with these exact entries: phone: 480px, tablet: 768px, and desktop: 1024px.
SASS
Hint
Use parentheses ( ) to create a Sass map. Separate keys and values with a colon and entries with commas.
2
Create the respond-to mixin
Create a Sass mixin called respond-to that takes a parameter $breakpoint. Inside, write a media query using @media (min-width: map-get($breakpoints, $breakpoint)) and include @content inside the media query block.
SASS
Hint
Use @mixin to create a mixin and @content to insert styles inside the media query.
3
Use the mixin for the .box background colors
Write styles for the .box class. Use the respond-to mixin three times with phone, tablet, and desktop to set the background colors to lightblue, lightgreen, and lightcoral respectively.
SASS
Hint
Use @include respond-to(breakpoint) inside the .box block to apply styles for each breakpoint.
4
Add base styles and complete the Sass file
Add a base style for the .box class that sets width to 100%, height to 10rem, and transition for background color changes. Keep the responsive background colors inside the respond-to mixin calls.
SASS
Hint
Set the base size and smooth color change for the box outside the media queries.
Practice
(1/5)
1. What is the main purpose of using a responsive breakpoint mixin in Sass?
easy
A. To import external CSS files
B. To create animations for buttons
C. To define color variables
D. To write reusable media queries for different screen sizes
Solution
Step 1: Understand what a mixin does
A mixin in Sass is used to group CSS rules that can be reused.
Step 2: Identify the role of breakpoint mixins
Breakpoint mixins specifically help write media queries that adapt styles for different screen sizes.
Final Answer:
To write reusable media queries for different screen sizes -> Option D
Quick Check:
Responsive breakpoint mixins = reusable media queries [OK]
A. Missing parentheses around media query condition: should be @media (max-width: $size)
B. Incorrect mixin name, should be @mixin break-point
C. Missing semicolon after @content
D. Parameters cannot be used in media queries
Solution
Step 1: Check media query syntax
Media queries require parentheses around conditions, e.g., (max-width: $size).
Step 2: Identify the fix
Add parentheses around max-width: $size to correct the syntax.
Final Answer:
Missing parentheses around media query condition: should be @media (max-width: $size) -> Option A
Quick Check:
Media queries need parentheses [OK]
Hint: Always wrap media query conditions in parentheses [OK]
Common Mistakes:
Omitting parentheses in media queries
Thinking parameters can't be used in media queries
Confusing mixin naming conventions
5. You want to create a flexible breakpoint mixin that accepts a minimum width and applies styles only if the screen is wider than that. Which of the following mixin definitions correctly achieves this?
The mixin must accept a parameter for minimum width and use it in a media query with min-width.
Step 2: Check each option's syntax
@mixin min-width($size) { @media (min-width: $size) { @content; } } correctly defines a parameter and uses parentheses in the media query with min-width.