- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS At-rules
CSS Reference
CSS At-rules
A practical map of the at-rules used to import, condition, organize, animate, and describe CSS. The reference groups statement and block at-rules for imports, conditions, layers, scope, fonts, animation, properties, counters, and paged output.
At-rules are the CSS instructions that start with @: @media, @import, @font-face, @keyframes and so on. They do not style elements directly; they set conditions, load things, or define resources that other rules use.
Each one has its own rules about where it can go and what goes inside. The best-known ones have their own lessons, such as CSS Media Queries and CSS Animations.
Two shapes
Some at-rules are a single statement ending in a semicolon, like @import url("theme.css");. Others wrap a block of rules or descriptors in curly braces, like @media and @font-face.
@charsetdeclares encoding and, when used, must come first.@importloads another stylesheet before ordinary rules.@namespacedeclares namespaces used by selectors.@media,@supports, and@containerapply conditional rules.@layerestablishes cascade priority groups.@scopelimits selectors to a subtree.@font-facedescribes downloadable fonts.@keyframesdefines animation stages.@propertyregisters a typed custom property.@pagecontrols paged-media dimensions and margins.@counter-styledefines a custom list marker system.
Example
css
@media (width >= 48rem) {
@supports (display: grid) {
.layout { display: grid; }
}
}Conditional at-rules can be nested. This layout only becomes a grid on screens at least 48rem wide, and only in browsers that support Grid.
Common At-rules
| At-rule | Purpose | Typical form |
|---|---|---|
| @media | Applies rules according to a media condition. | @media (width >= 48rem) |
| @supports | Tests CSS syntax support. | @supports (display: grid) |
| @container | Queries an ancestor container. | @container card (width > 30rem) |
| @keyframes | Defines animation stages. | @keyframes fade { ... } |
| @font-face | Describes a downloadable font. | @font-face { ... } |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS At-rules example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
@media (width >= 48rem) {
@supports (display: grid) {
.layout { display: grid; }
}
}
</style>
</head>
<body>
<main class="layout"><h1>Nested conditional rules</h1><p>The layout becomes a grid only when both conditions are understood.</p></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| @supports | 28 | 12 | 22 | 9 |
| @container | 105 | 105 | 110 | 16 |
| @property | 85 | 85 | 128 | 16.4 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Placement matters
@importmust come before all normal rules, or it is ignored.@charset, if used, must be the very first thing in the file.- Inside
@font-faceand@propertyyou write descriptors, which look like properties but only work there.font-displayis valid in@font-faceand nowhere else. @media,@supports,@containerand@layercan now be nested inside normal rules too, thanks to CSS nesting.
CSS At-rules exercises
@media
Make the card teal inside @media (min-width: 1px), which every screen matches.
The HTML your code works on
<style>
.wrap { width: 600px; }
</style>
<div class="wrap">
<div class="card">Card</div>
</div>Result
