Responsive breakpoints help your website look good on all screen sizes. Mixins make it easy to reuse these breakpoints in your styles.
Responsive breakpoint mixin patterns in SASS
@mixin breakpoint($point) { @if $point == small { @media (max-width: 599px) { @content; } } @else if $point == medium { @media (min-width: 600px) and (max-width: 1023px) { @content; } } @else if $point == large { @media (min-width: 1024px) { @content; } } }
The @mixin defines reusable code blocks.
@content inserts the styles you write inside the mixin call.
@include breakpoint(small) { body { background-color: lightblue; } }
@include breakpoint(medium) { p { font-size: 1.2rem; } }
@include breakpoint(large) { nav { display: flex; } }
This example uses a mixin to change the background and text color of the page depending on the screen size. Small screens get a light blue background, medium screens a lighter blue, and large screens stay white.
@use 'sass:color'; @mixin breakpoint($point) { @if $point == small { @media (max-width: 599px) { @content; } } @else if $point == medium { @media (min-width: 600px) and (max-width: 1023px) { @content; } } @else if $point == large { @media (min-width: 1024px) { @content; } } } body { background-color: white; color: black; font-family: Arial, sans-serif; } @include breakpoint(small) { body { background-color: #cceeff; color: #003366; } } @include breakpoint(medium) { body { background-color: #e6f2ff; color: #004080; } } @include breakpoint(large) { body { background-color: #ffffff; color: #000000; } }
Use meaningful breakpoint names like small, medium, and large for clarity.
Keep your breakpoints consistent across your project for easier maintenance.
Test your site on different devices or use browser DevTools to resize and see the changes.
Responsive breakpoint mixins help you write clean, reusable media queries.
They make your website adapt smoothly to different screen sizes.
Using mixins saves time and keeps your styles organized.
Practice
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 DQuick Check:
Responsive breakpoint mixins = reusable media queries [OK]
- Confusing mixins with animations
- Thinking mixins only define colors
- Believing mixins import files
Solution
Step 1: Review basic mixin syntax
A mixin is defined with @mixin name { ... } and can include @media queries.Step 2: Check the media query syntax
The correct media query syntax requires parentheses around conditions, like (max-width: 600px).Final Answer:
@mixin breakpoint { @media (max-width: 600px) { @content; } } -> Option CQuick Check:
Mixin with @media (max-width: 600px) = correct syntax [OK]
- Omitting parentheses in media queries
- Trying to pass parameters without defining them
- Incorrect media query syntax without parentheses
@mixin respond-to-mobile {
@media (max-width: 480px) {
@content;
}
}
.container {
width: 100%;
@include respond-to-mobile {
width: 90%;
}
}Solution
Step 1: Understand the mixin usage
The mixin wraps the included styles inside a media query for max-width 480px.Step 2: Translate Sass to CSS
The container has width 100% normally, and inside the media query, width changes to 90%.Final Answer:
.container { width: 100%; } @media (max-width: 480px) { .container { width: 90%; } } -> Option AQuick Check:
Normal styles + media query override = .container { width: 100%; } @media (max-width: 480px) { .container { width: 90%; } } [OK]
- Swapping widths inside and outside media query
- Putting media query before base styles
- Forgetting to nest styles inside media query
@mixin breakpoint($size) {
@media max-width: $size {
@content;
}
}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 AQuick Check:
Media queries need parentheses [OK]
- Omitting parentheses in media queries
- Thinking parameters can't be used in media queries
- Confusing mixin naming conventions
Solution
Step 1: Understand the requirement
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.Step 3: Identify incorrect options
@mixin min-width($size) { @media min-width: $size { @content; } } misses parentheses, @mixin min-width { @media (min-width: $size) { @content; } } lacks parameter, @mixin min-width($size) { @media (max-width: $size) { @content; } } uses max-width instead of min-width.Final Answer:
@mixin min-width($size) { @media (min-width: $size) { @content; } } -> Option BQuick Check:
Parameter + (min-width: $size) + @content = @mixin min-width($size) { @media (min-width: $size) { @content; } } [OK]
- Forgetting parentheses in media query
- Not defining parameter in mixin
- Using max-width instead of min-width
Ready to prove it?
Pass the test and this pattern moves unknown
