📖 Scenario: You are building a simple card layout for a website. The cards should look good on phones, tablets, and desktops. To make your CSS easier to manage, you will create a media query mixin in Sass. This mixin will help you write responsive styles quickly and clearly.
🎯 Goal: Create a Sass mixin called respond-to that accepts a device type (phone, tablet, or desktop) and applies the correct media query. Use this mixin to make the card background color change on different screen sizes.
📋 What You'll Learn
Create a Sass map called breakpoints with keys phone, tablet, and desktop and their respective max-width values.
Create a mixin called respond-to that takes a $device parameter and applies a media query using the breakpoints map.
Use the respond-to mixin to change the card background color for each device type.
Write semantic and clean Sass code that compiles to valid CSS.
💡 Why This Matters
🌍 Real World
Web developers often need to make websites look good on many devices. Using Sass mixins for media queries saves time and reduces mistakes.
💼 Career
Knowing how to write reusable media query mixins is a valuable skill for front-end developers working on responsive designs.
Progress0 / 4 steps
1
Create the breakpoints map
Create a Sass map called breakpoints with these exact entries: phone: 600px, tablet: 900px, and desktop: 1200px.
SASS
Hint
Use parentheses ( ) to create a Sass map. Separate each key-value pair with a comma.
2
Create the respond-to mixin
Create a mixin called respond-to that takes a parameter $device. Inside, write a media query using @media (max-width: map-get($breakpoints, $device)) and include @content inside the media query block.
SASS
Hint
Use @mixin to define the mixin and @content to include nested styles inside the media query.
3
Add base styles for the card
Create a CSS class .card with a fixed width of 20rem, padding of 1rem, and a background color of #eee.
SASS
Hint
Use rem units for size and write the styles inside .card { }.
4
Use the respond-to mixin to change background colors
Inside the .card class, use the @include respond-to(desktop) mixin to set background color to #99ccff. Then use @include respond-to(tablet) to set background color to #b3d7ff. Finally, use @include respond-to(phone) to set background color to #cce5ff.
SASS
Hint
Use @include respond-to(device) with curly braces to add styles inside the media query.
Practice
(1/5)
1. What is the main benefit of using a media query mixin in Sass?
easy
A. You write the media query once and reuse it multiple times
B. It automatically fixes all layout bugs
C. It converts Sass to JavaScript
D. It removes the need for CSS altogether
Solution
Step 1: Understand what a mixin does
A mixin lets you write reusable code blocks in Sass.
Step 2: Apply this to media queries
Using a media query mixin means you write the query once and reuse it for different styles.
Final Answer:
You write the media query once and reuse it multiple times -> Option A
Quick Check:
Reusable media queries = A [OK]
Hint: Mixins save repeating code by reusing media queries [OK]
Common Mistakes:
Thinking mixins fix layout bugs automatically
Confusing Sass with JavaScript
Believing mixins remove CSS need
2. Which of the following is the correct syntax to define a media query mixin for max-width 600px in Sass?
A. Mixin name 'tablet' is invalid; must start with a capital letter
B. @content should be outside @media block
C. Missing parentheses around max-width value in @media; fix with @media (max-width: 768px)
D. @media cannot be used inside mixins
Solution
Step 1: Check @media syntax
The media query must have parentheses around conditions, like (max-width: 768px).
Step 2: Correct the syntax
Change @media max-width: 768px to @media (max-width: 768px) to fix the error.
Final Answer:
Missing parentheses around max-width value in @media; fix with @media (max-width: 768px) -> Option C
Quick Check:
@media needs parentheses = A [OK]
Hint: Always wrap media conditions in parentheses [OK]
Common Mistakes:
Omitting parentheses in @media
Placing @content outside media block
Thinking mixin names must be capitalized
5. You want to create a Sass mixin that accepts a max-width value as a parameter and applies styles inside that media query. Which of these mixins correctly implements this pattern?