Sass lets you nest @media queries directly inside a selector and use Sass variables inside their conditions. This lesson focuses specifically on writing media queries in Sass, before the next lessons build reusable mixins and a breakpoint system around them.
How Sass Improves on Plain CSS Media Queries
Plain CSS requires writing a full media query block separately from the selector it affects, often far away in the file. Sass lets you nest a media query directly inside the selector's own rule, keeping related styles together and letting the compiler restructure the output correctly.
Media queries can be nested inside any selector's block.
A Sass variable can be used directly as (or inside) a media query condition.
and, or, and comma-separated queries all work exactly as in plain CSS.
Interpolation (#{}) is needed only when building a condition from a partial value, not when using a full variable directly.
Sass Media Queries Cheatsheet
Common media query patterns and how Sass simplifies them.
Pattern
Example
Notes
Nested query
.el { @media (min-width: 768px) { ... } }
Keeps rule and query together
Variable width
@media (min-width: $bp-md)
No interpolation needed for a full value
Range query
(min-width: 768px) and (max-width: 1199px)
Standard CSS range syntax
Feature query
@media (prefers-color-scheme: dark)
Non-width based conditions work identically
Interpolated condition
(min-width: #{$bp}px)
Needed only for partial, string-built values
Nesting Media Queries Inside Components
Keeping a media query nested inside its component's own rule is one of the biggest ergonomic wins Sass offers over plain CSS, there is no need to scroll to a separate part of the file to see how a component behaves at a given breakpoint.
Sass media queries support the exact same combinators as plain CSS, and for combining conditions, and commas for an "or" relationship between multiple full queries.
.banner {
@media (min-width: 768px) and (max-width: 1199px) {
// only tablets and smaller desktops
}
@media (prefers-reduced-motion: reduce), (max-width: 480px) {
transition: none;
}
}
Using Sass Variables in Query Conditions
A Sass variable can be dropped directly into a media query condition without any interpolation, as long as it represents the entire value being compared, this is what makes the shared breakpoint map approach from the Responsive Architecture lesson possible.
Writing media queries far away from the component they affect instead of taking advantage of Sass's nesting support.
Adding unnecessary interpolation (#{$bp}) around a variable that's already a complete value.
Mixing min-width and max-width queries inconsistently within the same component.
Repeating the same raw pixel value across many components instead of centralizing it in a shared breakpoint map.
Key Takeaways
Sass lets you nest @media directly inside the selector it affects, and restructures the output correctly.
Media queries support the same combinators (and, commas) as plain CSS.
A Sass variable can be used directly inside a query condition without interpolation.
Keeping media queries nested inside components avoids scattering related responsive logic across a file.
Pro Tip
Get comfortable nesting media queries directly inside a component's own rule rather than collecting them at the bottom of a file, it makes each component's full responsive behavior visible in one place while reading the code.
You now understand how Sass improves on writing plain CSS media queries. Next, learn how to build reusable responsive mixins around your breakpoints.