Skip to main content

CSS Reference

CSS At-rules

Written by Updated CSS At-rules exercises

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.

  • @charset declares encoding and, when used, must come first.
  • @import loads another stylesheet before ordinary rules.
  • @namespace declares namespaces used by selectors.
  • @media, @supports, and @container apply conditional rules.
  • @layer establishes cascade priority groups.
  • @scope limits selectors to a subtree.
  • @font-face describes downloadable fonts.
  • @keyframes defines animation stages.
  • @property registers a typed custom property.
  • @page controls paged-media dimensions and margins.
  • @counter-style defines 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-rulePurposeTypical form
@mediaApplies rules according to a media condition.@media (width >= 48rem)
@supportsTests CSS syntax support.@supports (display: grid)
@containerQueries an ancestor container.@container card (width > 30rem)
@keyframesDefines animation stages.@keyframes fade { ... }
@font-faceDescribes 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
@supports2812229
@container10510511016
@property858512816.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

  • @import must come before all normal rules, or it is ignored. @charset, if used, must be the very first thing in the file.
  • Inside @font-face and @property you write descriptors, which look like properties but only work there. font-display is valid in @font-face and nowhere else.
  • @media, @supports, @container and @layer can 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

Check your answer to see it here.