Sass CSS At-Rules

Beginner
10 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

Sass supports every plain CSS at-rule. Most of them are written exactly as in CSS and compiled straight through. A few—@media and @supports—also understand Sass features such as nesting, SassScript (variables, maps, math), and interpolation with #{}.

Any at-rule Sass does not recognise is treated as an unknown at-rule and is emitted as written, with interpolation allowed in its parameters.

Nesting swap.

When you nest @media or @supports inside a style rule, Sass bubbles the at-rule to the top and moves the style rule inside it. You write selector-first; the CSS comes out at-rule-first.

Merged queries.

Nested @media rules combine into one query with and (for example (min-width: 640px) and (orientation: landscape)).

Not the same as Sass at-rules.

Rules like @use, @mixin, @if, and @each are Sass features and never reach the CSS. This page covers the CSS at-rules Sass passes through. See the at-rules hub for the Sass ones.

Syntax

SCSS
$tablet: 768px;
$query: "screen and (max-width: 600px)";

.card {
  padding: 1rem;

  // Nested @media bubbles to the root
  @media (min-width: $tablet) {
    padding: 2rem;
  }

  // Whole query via interpolation
  @media #{$query} {
    padding: 0.5rem;
  }

  // Nested @supports bubbles the same way
  @supports (display: grid) {
    display: grid;
  }
}

@keyframes fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

Pattern: write the at-rule where it belongs next to its selector → let Sass bubble and merge → use $variables, map.get(), and #{} inside queries when values need to be reused.

At-Rule Behavior at a Glance

How Sass treats the common CSS at-rules:

At-ruleInside a style ruleSassScript / interpolation
@mediaBubbles to the root; nested queries merge with andVariables, maps, and math in feature values; #{} for whole queries
@supportsBubbles to the root like @mediaValues and #{} in the condition
@keyframesEmitted at the stylesheet root#{} in the animation name; variables inside frames
@font-faceWrite at the top levelVariables in declaration values
@container / @layerBubble like @media (selector wrapped inside)#{} in the parameters
Other unknown at-rulesPassed through as written#{} in the parameters
Selectors vs queries.

Interpolation is the right tool for whole queries or names. For a single value inside parentheses, plain SassScript like (min-width: $tablet) is enough.

SCSS Examples

Each example compiles with Dart Sass. Open View Compiled CSS to see where Sass places the at-rule and the selector.

Example 1 — Nested @media Bubbles to the Root

Write the breakpoint next to the rule it changes; Sass moves it out.

SCSS
$tablet: 768px;

.card {
  padding: 1rem;

  @media (min-width: $tablet) {
    padding: 2rem;
  }
}

How It Works

  1. Declare $tablet once and reuse it in the query.
  2. Nest the @media rule inside .card beside the base styles.
  3. Sass bubbles the at-rule to the top level.
  4. .card is repeated inside the query so the selector still matches.

Example 2 — Nested Queries Merge with and

A @media inside another @media becomes one combined query.

SCSS
@media (min-width: 640px) {
  .nav {
    color: red;

    @media (orientation: landscape) {
      color: blue;
    }
  }
}

@media screen {
  .hero {
    @media (min-width: 100px) {
      padding: 2rem;
    }
  }
}

How It Works

  1. The outer query (min-width: 640px) keeps its own .nav rule.
  2. The inner query is merged with and into a second block.
  3. Media types merge too: screen + a feature becomes screen and (min-width: 100px).
  4. No manual rewrite—you never write the combined query yourself.

Example 3 — Variables, Maps, and Interpolation in @media

Pull breakpoints from a map, or interpolate a whole query string.

SCSS
@use "sass:map";

$breakpoints: (sm: 480px, md: 768px);
$query: "screen and (max-width: 600px)";
$type: screen;
$feature: -webkit-min-device-pixel-ratio;
$ratio: 1.5;

.panel {
  @media (min-width: map.get($breakpoints, md)) {
    width: 50%;
  }

  @media #{$query} {
    width: 100%;
  }

  @media #{$type} and ($feature: $ratio) {
    border-width: 0.5px;
  }

  @media screen and (min-width: map.get($breakpoints, sm) + 100px) {
    margin: 0 auto;
  }
}

How It Works

  1. map.get() reads a breakpoint, just like any value.
  2. Math works inside parentheses: 480px + 100px → 580px.
  3. #{$query} drops a full query string into the at-rule.
  4. Feature names and values can be variables when written as ($feature: $ratio).

Example 4 — @supports with SassScript and Nesting

Feature queries bubble and accept interpolated declarations.

SCSS
$prop: display;
$val: grid;

.layout {
  float: left;

  @supports (#{$prop}: $val) {
    display: grid;
  }

  @supports not (display: grid) {
    float: left;
  }
}

@supports (position: sticky) {
  .header {
    position: sticky;
  }
}

How It Works

  1. #{$prop} builds the property name; $val is a normal SassScript value.
  2. Nested @supports bubbles out of .layout.
  3. not (...) conditions pass through untouched.
  4. Top-level @supports compiles as written.

Example 5 — @keyframes, @font-face, and @container

Name an animation with interpolation; plain at-rules pass straight through.

SCSS
$name: pulse;
$duration: 2s;
$container-query: "(min-width: 400px)";

@font-face {
  font-family: "Demo Sans";
  src: url("demo.woff2") format("woff2");
}

@keyframes #{$name} {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

.dot {
  animation: #{$name} $duration infinite;

  @container #{$container-query} {
    animation-duration: 1s;
  }
}

How It Works

  1. @font-face compiles exactly as written.
  2. @keyframes #{$name} interpolates the animation name; frames use normal CSS selectors.
  3. The animation shorthand reuses the same $name and $duration.
  4. Unknown-style at-rules like @container bubble and accept #{} in their parameters.

Edge Cases & Pitfalls

Most at-rule surprises come from output order, merged queries, or confusing CSS at-rules with Sass at-rules.

order

A style rule can split around a bubbled at-rule

Declarations written after a nested @media land in a second block of the same selector, after the query. Keep that in mind for cascade order.

merge

Impossible combinations disappear

A @media print nested inside @media screen can never match, so Sass omits the inner rule instead of emitting a useless query.

interp

Use #{} for whole queries

A query stored in a variable must be interpolated: @media #{$query}. Writing @media $query is not a valid query.

sass

Sass at-rules never reach CSS

@use, @mixin, @include, @if, @each are compiled away. Only CSS at-rules appear in the output.

Sass Support

These behaviors are part of the core Sass language.

FeatureStatus
Nested @media / @supportsSupported (bubbling and merging)
SassScript in @media featuresSupported
#{} in at-rule parametersSupported
Unknown at-rulesEmitted as written (bubble inside style rules)
LibSass / Ruby SassOlder behavior; some newer CSS at-rules may differ

Compile with the Dart Sass CLI.

Key Takeaways

  • All CSS at-rules work in Sass; unknown ones pass through.
  • @media and @supports bubble out of style rules and merge when nested.
  • SassScript and #{} let variables and maps drive queries.
  • Sass at-rules (@use, @mixin, …) never appear in the CSS.

One line: write CSS at-rules next to your selectors and let Sass bubble, merge, and interpolate them.

Compile Notes

Practical checklist:

checklist.txt
1. Nest @media / @supports beside the rule they affect
2. Store breakpoints in variables or a map
3. Use #{$query} when a whole query lives in a variable
4. Expect nested queries to merge with "and"
5. Check the compiled order if cascade matters

Frequently Asked Questions

Yes. Plain CSS at-rules such as @media, @supports, @keyframes, @font-face, @container, and @layer are all written as normal. Sass emits unknown at-rules as written.
Sass bubbles the @media rule to the top level and places the style rule inside it, so you can write breakpoints next to the selectors they change.
Yes. Sass merges them into a single query joined with and, for example (min-width: 640px) and (orientation: landscape).
Yes. Variables, map lookups, and math work inside feature values like (min-width: $tablet). Use #{} to interpolate a whole query string.
Yes. Values in a @supports declaration are SassScript, and you can interpolate property names or the condition with #{}.
CSS at-rules are emitted into the compiled stylesheet. Sass at-rules such as @use, @mixin, @if, and @each run at compile time and never appear in the CSS.

Next: Sass @if Rule

Choose styles with conditions and flow control.

@if →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful