CSS At-Rules Reference: @import, @namespace, @media, @supports, @layer and the Rest
Every CSS at-rule in one place: statement and block forms, the required order at the top of a stylesheet, @charset, @import with layer() and supports(), @namespace, conditional, cascade and definition rules, experimental ones, and the CSSOM.
kvi5hnuCode Sprout team member9 min readOn this page
Style rules select elements and set properties. At-rules, the statements that start with @, do everything else: import other stylesheets, apply rules only under conditions, organize the cascade, and define things like fonts, animations, counters and custom property types. This article gathers all of them in one reference, covers the few that have no article of their own, and links to the rest.
Two shapes
An at-rule is an @ and a name, then a prelude, then either a semicolon or a block:
@import url("base.css") layer(base); /* statement: ends with ; */
@layer reset, base, components; /* statement */
@media (width >= 40rem) { /* block containing rules */
.grid { grid-template-columns: 1fr 1fr; }
}
@font-face { /* block containing descriptors */
font-family: "Inter";
src: url("inter.woff2") format("woff2");
}
Blocks hold one of two things:
- rules, in the conditional and cascade at-rules, such as
@media,@supports,@container,@layerand@scope; - descriptors, in the definition at-rules, such as
@font-face,@propertyand@counter-style. Descriptors look like properties, but they describe the thing being defined and only exist inside their at-rule:font-displayin@font-face,syntaxin@property.
A browser that doesn't recognize an at-rule skips it, up to the end of its block or the next semicolon, and continues with the next rule (article 3).
The order at the top of a stylesheet

Three at-rules must come before everything else, in this order: @charset, then @import (with @layer statements allowed before and between imports), then @namespace. An @import or @namespace that appears later is invalid and ignored:

The ignored @import doesn't just fail to load: it's dropped while parsing and doesn't exist in the CSSOM at all. Nothing warns you, so check the order when an import seems to do nothing.
@charset
@charset "UTF-8";
It tells the browser the stylesheet's character encoding. It's almost never needed: an encoding declared by the server's Content-Type header or a byte-order mark takes priority, and stylesheets linked from a UTF-8 page default to UTF-8. Its syntax is unusually strict: it must be the very first bytes of the file, written exactly like this, with one space and double quotes. Modern browsers only read it as an encoding hint; it doesn't appear in the CSSOM. Save your files as UTF-8 and you can forget it.
@import
@import url("theme.css");
@import "theme.css"; /* a plain string works too */
@import url("print.css") print; /* media query list */
@import url("grid.css") supports(display: grid); /* only if the condition is true */
@import url("vendor.css") layer(vendor); /* into a named layer */
@import url("reset.css") layer; /* into a new anonymous layer */
@import url("fancy.css") layer(theme) supports(color: oklch(0% 0 0)) (width >= 40rem);
The conditions come in a fixed order: layer(), then supports(), then the media query list. Tested in Chrome, writing supports(display: grid) layer(x) instead made layer(x) part of the media query list, as a query that never matches, so the stylesheet was never applied. supports() works in all browsers (Chrome 122, Firefox 115, Safari 17.5); older browsers ignore the whole @import.
The imported rules behave as if written at the position of the @import (article 17), and layer() places them in a cascade layer (article 20).
Performance: the browser only discovers an imported file after downloading and parsing the file that imports it, so chains of @import load one after another. On production sites, use several <link> elements, which download in parallel, or let a build tool inline the imports into one file (article 120). Article 2 compares the ways of adding CSS to a page.
@namespace
XML-based documents can mix elements from different vocabularies that share names. In an HTML page, an inline SVG's <a>, <title>, <style> and <script> elements are in the SVG namespace, not the HTML one. @namespace gives a namespace a prefix to use in selectors with a |:
@namespace svg url(http://www.w3.org/2000/svg);
a { color: blue; } /* no prefix: elements in any namespace */
svg|a text { fill: red; } /* only <a> elements in the SVG namespace */
*|title { … } /* any namespace, explicitly */

The URL is only an identifier: the browser never fetches it. Without a prefix, @namespace url(…); sets a default namespace, and then unprefixed type selectors match only elements in it. Tested in Chrome, an @namespace after a style rule was dropped, and so was the rule that used its prefix. In plain HTML, you rarely need any of this, since class selectors work regardless of namespace.
Conditional rules
These apply the rules inside them only when a condition is true, and re-evaluate when it changes. They can be nested in each other and, with nesting, inside style rules (article 21).
| At-rule | Condition | Article |
|---|---|---|
@media | the device, viewport and user preferences | 76 |
@supports | whether the browser understands a property, value, selector or font technology | 80 |
@container | the size, style or scroll state of an ancestor container | 78, 79 |
@supports (display: grid) {
@media (width >= 40rem) {
.cards { display: grid; grid-template-columns: repeat(3, 1fr); }
}
}
Rules that organize the cascade
| At-rule | Does | Article |
|---|---|---|
@layer | creates cascade layers, or declares their order | 20, 117 |
@scope | limits rules to a subtree, with an optional lower boundary, and adds proximity to the cascade | 22 |
@starting-style | gives the values to transition from when an element first appears | 108 |
Rules that define things
Each holds descriptors and defines something that properties then refer to by name:
| At-rule | Defines | Used by | Article |
|---|---|---|---|
@font-face | a downloadable font | font-family | 83 |
@font-feature-values | names for a font's alternate glyph sets | font-variant-alternates | 86 |
@font-palette-values | a color palette for a color font | font-palette | below |
@counter-style | a list-numbering system | list-style-type, counter() | 97 |
@property | a typed custom property | var() | 28 |
@keyframes | an animation's keyframes | animation-name | 109 |
@page | page size and margins for print, with margin boxes like @top-center | printing | 81 |
@view-transition | opts a page into cross-document view transitions | navigation | 114 |
@position-try | a fallback position for an anchored element | position-try-fallbacks | 59 |
@function | a custom function that returns a value | --name() in values | 30 |
@font-palette-values
Color fonts in the COLR format can contain several palettes. This at-rule selects one, or overrides individual colors:
@font-palette-values --brand {
font-family: "Bungee Spice"; /* the font this palette applies to */
base-palette: 1; /* start from the font's palette number 1 */
override-colors: 0 #8a3b12, 1 #f4c95d; /* replace color slots 0 and 1 */
}
h1 { font-family: "Bungee Spice"; font-palette: --brand; }
font-palette also accepts normal, light and dark, which pick the font's own palette meant for light or dark backgrounds, if it has one. It works in all browsers (Chrome 101, Firefox 107, Safari 15.4), and only affects color fonts with palettes. Check @supports font-tech(palettes) (article 80) if the design depends on it.
Experimental and obsolete at-rules
@custom-medianames a media query for reuse:@custom-media --wide (width >= 60rem);, then@media (--wide). Only Firefox 148 supports it; build tools such as Lightning CSS can compile it away (article 76).@mixin,@apply,@contents(CSS Mixins draft) are only behind Chrome's experimental features flag.@function, from the same draft, has shipped.@document(matching rules to URLs) was dropped from the standards; only Firefox ever supported a prefixed version, and only in user stylesheets.@viewportwas removed in favor of the<meta name="viewport">tag (article 75).
At-rules in the CSSOM
Each at-rule becomes an object in sheet.cssRules, with its own class:
| At-rule | CSSOM class | Useful members |
|---|---|---|
@import | CSSImportRule | href, media, layerName, supportsText, styleSheet |
@media | CSSMediaRule | media, cssRules |
@supports | CSSSupportsRule | conditionText, cssRules |
@container | CSSContainerRule | containerName, containerQuery |
@layer | CSSLayerBlockRule, CSSLayerStatementRule | name, nameList |
@font-face | CSSFontFaceRule | style (its descriptors) |
@keyframes | CSSKeyframesRule | name, findRule(), appendRule() |
@property | CSSPropertyRule | name, syntax, inherits, initialValue |
@namespace | CSSNamespaceRule | prefix, namespaceURI |
sheet.insertRule("@media print { … }", 0) adds one at runtime. The same rules about order apply: inserting an @import after a style rule throws an error.
@layer base, components;
@property --accent {
syntax: "<color>";
inherits: true;
initial-value: #8a3b12;
}
@layer base {
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
}
@layer components {
.card { padding: 0.8rem; border-radius: 10px; border: 2px solid var(--accent); container-type: inline-size; }
.card h3 { margin: 0; color: var(--accent); }
.card p { margin: 0.3rem 0 0; }
@container (width < 280px) { .card h3 { font-size: 1rem; } }
}
@media (prefers-color-scheme: dark) {
:root { --accent: #f4c95d; color-scheme: dark; }
}
@supports (color: oklch(0% 0 0)) {
.card { background: oklch(97% 0.02 70); }
@media (prefers-color-scheme: dark) { .card { background: oklch(25% 0.02 70); } }
}
@keyframes pop { from { scale: 0.96; } }
.card { animation: pop 0.3s ease-out; }
#out { font: 0.72rem/1.4 ui-monospace, monospace; background: #f6f8fa; color: #1f2328; padding: 0.5rem; border-radius: 6px; white-space: pre-wrap; }
The script walks the stylesheet and prints each rule's CSSOM class, nested rules indented. Each kind of at-rule has its own class, and @media rules show ✓ or ✗ for whether their query currently matches.
What's current
@import … supports()works in all browsers since 2024;@scopesince Firefox 146 (December 2025);@position-trysince Firefox 147 and Safari 26.@view-transitionis in Chrome and Safari, with Firefox in preview;@functionin Chrome 139 and Safari 27.3;@custom-mediaonly in Firefox.- Mixins remain experimental.
Common questions
Why does my @import do nothing?
Usually it's below another rule; move it to the top, after @charset and alongside @layer statements. Also check the URL, since a missing file fails silently.
Is @import bad for performance?
Imports load one after another, delaying rendering. Use <link> elements or bundle the files for production.
Do I need @charset?
Almost never. Serve UTF-8, and declare it in the server's Content-Type header.
Can I nest @media inside a rule?
Yes, with CSS nesting: .card { @media (width >= 40rem) { padding: 2rem; } }. Conditional rules can also nest in each other.
Summary
- At-rules are statements ending in
;or blocks holding rules or descriptors. @charset, then@importand@layerstatements, then@namespacemust come first; a late@importis silently dropped.@importtakeslayer(),supports()and media queries, in that order, but chains load slowly.@namespacedistinguishes elements of different XML vocabularies, such as SVG's<a>.- Conditional rules (
@media,@supports,@container), cascade rules (@layer,@scope,@starting-style) and definition rules (@font-face,@property,@keyframesand more) cover the rest. - Each at-rule has its own CSSOM class.
Practice questions
Why isn't theme.css applied?
:root { --brand: #8a3b12; }
@import url("theme.css");
Answer
@import must come before any style rule. After :root { … }, it's invalid and dropped. Move it to the top.
Write an @import that loads grid.css into the layout layer, only in browsers that support subgrid, and only on screens at least 40rem wide.
Answer
@import url("grid.css") layer(layout) supports(grid-template-columns: subgrid) screen and (width >= 40rem);
The parts must come in that order: layer(), supports(), media query.
Is this valid at the top of a stylesheet?
@layer reset, base;
@import url("reset.css") layer(reset);
@namespace svg url(http://www.w3.org/2000/svg);
@import url("base.css") layer(base);
Answer
No. The @layer statement and first @import are fine, but @import can't come after @namespace, so the second import is dropped. Put both imports before the @namespace rule.
Which at-rules hold descriptors rather than rules?
Answer
The definition at-rules: @font-face, @font-feature-values (inside its blocks), @font-palette-values, @counter-style, @property, @page (alongside margin at-rules), @view-transition and @position-try. @keyframes holds keyframe blocks, and @function holds local variables and a result descriptor.
Previous: Preprocessors and Tooling: Sass, PostCSS, Lightning CSS and What Native CSS Replaced
Next: Debugging CSS and Best Practices: A Troubleshooting Guide

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.