A media query mixin in Sass is a reusable block of code that helps you write media queries easily by wrapping CSS rules inside a function-like structure. It makes your styles cleaner and easier to maintain.
Click to reveal answer
beginner
How do you define a simple media query mixin for a max-width of 600px?
@mixin max-width-600 {
@media (max-width: 600px) {
@content;
}
}
This mixin wraps any CSS passed inside it with a max-width 600px media query.
Click to reveal answer
intermediate
Why use media query mixins instead of writing media queries directly?
Using mixins avoids repeating the same media query code. It keeps your CSS DRY (Don't Repeat Yourself), makes updates easier, and improves readability by giving meaningful names to breakpoints.
Click to reveal answer
intermediate
Show an example of a responsive font size using a media query mixin.
@mixin respond-to($breakpoint) {
@if $breakpoint == small {
@media (max-width: 600px) {
@content;
}
}
}
p {
font-size: 1.6rem;
@include respond-to(small) {
font-size: 1.2rem;
}
}
This changes paragraph font size on small screens.
Click to reveal answer
beginner
What is the role of @content inside a Sass mixin for media queries?
@content is a placeholder inside a mixin where you put the CSS rules that should be wrapped by the media query. It lets you insert custom styles when you use the mixin.
Click to reveal answer
What does the @content directive do inside a Sass mixin?
AEnds the mixin
BDefines a new variable
CStarts a media query
DInserts the CSS rules passed to the mixin
✗ Incorrect
@content inserts the CSS rules you write inside the mixin call, wrapping them as needed.
Which of these is a correct way to write a media query mixin for max-width 768px?
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?