Media query mixin patterns help you write reusable code to make your website look good on different screen sizes.
Media query mixin patterns in SASS
Introduction
Syntax
SASS
@mixin respond-to($breakpoint) { @if $breakpoint == small { @media (max-width: 600px) { @content; } } @else if $breakpoint == medium { @media (min-width: 601px) and (max-width: 1024px) { @content; } } @else if $breakpoint == large { @media (min-width: 1025px) { @content; } } }
The @mixin defines reusable code blocks.
The @content placeholder inserts styles inside the media query.
Examples
SASS
@mixin respond-to($breakpoint) { @if $breakpoint == small { @media (max-width: 480px) { @content; } } }
SASS
@include respond-to(small) { body { background-color: lightblue; } }
SASS
@include respond-to(large) { nav { display: flex; } }
Sample Program
This example changes the background color and font size of the page depending on screen size using the media query mixin.
SASS
@mixin respond-to($breakpoint) { @if $breakpoint == small { @media (max-width: 600px) { @content; } } @else if $breakpoint == medium { @media (min-width: 601px) and (max-width: 1024px) { @content; } } @else if $breakpoint == large { @media (min-width: 1025px) { @content; } } } body { font-family: Arial, sans-serif; background-color: white; color: black; } @include respond-to(small) { body { background-color: lightyellow; font-size: 1.2rem; } } @include respond-to(medium) { body { background-color: lightgreen; font-size: 1.4rem; } } @include respond-to(large) { body { background-color: lightblue; font-size: 1.6rem; } }
Important Notes
Use meaningful names for breakpoints like small, medium, and large to keep code clear.
Mixins help avoid repeating media query code everywhere.
Test your site on different screen sizes to see the changes.
Summary
Media query mixins make responsive design easier and cleaner.
You write the media query once and reuse it with different styles.
This helps your website look good on phones, tablets, and desktops.
Practice
1. What is the main benefit of using a media query mixin in Sass?
easy
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 AQuick 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?
easy
Solution
Step 1: Recall Sass mixin syntax
A mixin is defined with @mixin name { ... } and uses @content to insert styles.Step 2: Check media query inside mixin
The media query should wrap @content inside the mixin block.Final Answer:
@mixin respond { @media (max-width: 600px) { @content; } } -> Option AQuick Check:
Mixin with media and @content = B [OK]
Hint: Use @mixin name { @media (...) { @content; } } for media queries [OK]
Common Mistakes:
- Placing @media outside the mixin
- Wrong order of @mixin and @media
- Missing @content inside media query
3. Given this Sass code, what CSS will be generated?
@mixin phone { @media (max-width: 480px) { @content; } }
.box {
width: 100%;
@include phone {
width: 50%;
}
}medium
Solution
Step 1: Understand mixin usage
The mixin wraps included styles inside the media query for max-width 480px.Step 2: Translate Sass to CSS
.box has width 100% normally, and inside the media query it changes to width 50%.Final Answer:
.box { width: 100%; } @media (max-width: 480px) { .box { width: 50%; } } -> Option DQuick Check:
Normal style + media query override = D [OK]
Hint: Mixin wraps included styles inside media query block [OK]
Common Mistakes:
- Swapping widths inside and outside media query
- Putting media query styles outside the block
- Combining widths without media query
4. Identify the error in this Sass mixin and how to fix it:
@mixin tablet {
@media max-width: 768px {
@content;
}
}medium
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 CQuick 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?
hard
Solution
Step 1: Check parameter usage in mixin
The mixin must accept a parameter $max and use it inside parentheses in the media query.Step 2: Verify correct media query syntax
@media (max-width: $max) is correct; missing parentheses or wrong media feature cause errors.Final Answer:
@mixin respond($max) { @media (max-width: $max) { @content; } } -> Option BQuick Check:
Parameterized media query with parentheses = C [OK]
Hint: Use parameters with parentheses in media queries [OK]
Common Mistakes:
- Missing parentheses around media condition
- Using min-width instead of max-width by mistake
- Not passing parameter to mixin
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
