Sass Media Queries & Breakpoints
Responsive design means changing styles at different screen widths. In plain CSS each media query is a separate block far away from the component it changes. Sass lets you nest media queries inside rules and wrap breakpoints in mixins, so each component keeps its responsive behaviour in one place.
Nesting @media inside a rule
.sidebar {
width: 100%;
@media (min-width: 768px) {
width: 260px;
float: left;
}
}.sidebar {
width: 100%;
}
@media (min-width: 768px) {
.sidebar {
width: 260px;
float: left;
}
}
Sass "bubbles" the media query up to the root and repeats the selector inside it.
Variables in media queries
$tablet: 768px;
$desktop: 1200px;
.container {
padding: 0 16px;
@media (min-width: $tablet) {
padding: 0 32px;
}
@media (min-width: $desktop) {
max-width: 1140px;
margin: 0 auto;
}
}.container {
padding: 0 16px;
}
@media (min-width: 768px) {
.container {
padding: 0 32px;
}
}
@media (min-width: 1200px) {
.container {
max-width: 1140px;
margin: 0 auto;
}
}
A breakpoint mixin
Hard-coding numbers in every query is error-prone. Store breakpoints in a map and write a mixin with @content:
@use "sass:map";
$breakpoints: (
sm: 576px,
md: 768px,
lg: 1024px,
xl: 1280px,
);
@mixin up($name) {
$width: map.get($breakpoints, $name);
@if not $width {
@error "Unknown breakpoint `#{$name}`.";
}
@media (min-width: $width) {
@content;
}
}
.gallery {
display: grid;
gap: 12px;
grid-template-columns: 1fr;
@include up(sm) { grid-template-columns: repeat(2, 1fr); }
@include up(lg) { grid-template-columns: repeat(4, 1fr); }
}.gallery {
display: grid;
gap: 12px;
grid-template-columns: 1fr;
}
@media (min-width: 576px) {
.gallery {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1024px) {
.gallery {
grid-template-columns: repeat(4, 1fr);
}
}
Tip: Resize the preview area (or your browser window) to see the gallery change from one column to two to four.
Mobile-first vs desktop-first
| Approach | Query | Base styles target |
|---|---|---|
| Mobile-first (recommended) | min-width |
Small screens; add complexity as space grows |
| Desktop-first | max-width |
Large screens; undo things for small ones |
A down mixin for the occasional max-width case subtracts a tiny amount so the ranges don't overlap:
@use "sass:map";
$breakpoints: (md: 768px);
@mixin down($name) {
@media (max-width: map.get($breakpoints, $name) - 0.02px) {
@content;
}
}
@mixin between($min, $max) {
@media (min-width: $min) and (max-width: $max - 0.02px) {
@content;
}
}
.menu-toggle {
display: none;
@include down(md) { display: block; }
}
.promo {
@include between(600px, 900px) { font-size: 1.1rem; }
}.menu-toggle {
display: none;
}
@media (max-width: 767.98px) {
.menu-toggle {
display: block;
}
}
@media (min-width: 600px) and (max-width: 899.98px) {
.promo {
font-size: 1.1rem;
}
}
Other media features
Mixins work for any media feature, not just width:
@mixin dark-mode { @media (prefers-color-scheme: dark) { @content; } }
@mixin reduced-motion { @media (prefers-reduced-motion: reduce) { @content; } }
@mixin hover-capable { @media (hover: hover) { @content; } }Nested media queries combine
If you nest one @media inside another, Sass merges them with and:
.box {
@media screen {
@media (min-width: 600px) { color: red; }
}
}
// @media screen and (min-width: 600px) { .box { color: red; } }Note: Sass outputs one media block per use, so the same query may appear many times in the CSS. That's normal and costs very little after gzip. Container queries (
@container) nest the same way.
Test yourself
What happens to a @media block nested inside .card { }?
- It is an error
- It's moved to the root with
.cardrepeated inside it - It is ignored
- It applies to the whole page
Which Sass feature lets a breakpoint mixin receive the styles to wrap?
- @return
- @content
- @forward
- @extend
In a mobile-first stylesheet, which media feature do breakpoints usually use?
- max-width
- min-width
- orientation
- resolution