At-rule taxonomy and placement
Definition and purpose
At-rules introduce CSS instructions with an @ identifier. Some end in a semicolon, some contain rule blocks, and others contain descriptors rather than ordinary style declarations.
Layer order statement
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } @layer base, theme; @layer theme { #a { color: green; } } @layer base { #a { color: navy; } } </style> </head> <body> <div id="a">Layered text</div> <div id="b">Other text</div> </body></html>The declared theme layer wins normal declarations even though its block occurs earlier.
Syntax and mental model
@layer can declare order as a statement or define a block. @media and @supports group conditional rules. @font-face uses descriptors for a font face; these do not style arbitrary elements. @import must be top-level and precede ordinary rules and layer blocks, allowing the permitted preceding @charset and layer statements. Not every at-rule can be nested everywhere. Check each rule grammar, then inspect document.styleSheets[0].cssRules. The examples avoid external imports; a manual placement experiment can inspect ignored late imports without loading remote resources.
Nested condition
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } @media screen { @supports (display: grid) { #a { display: grid; } } } </style> </head> <body> <div id="a">Layered text</div> <div id="b">Other text</div> </body></html>The inner rule applies only when both conditions hold.
Common mistakes
Do not assume all declarations belong in every descriptor block or that a late @layer order statement can rearrange established layers.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Reading specifications and maturity. Next lesson: Feature detection and progressive enhancement.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
At-rule taxonomy and placement — knowledge check
Five questions on at-rule taxonomy and placement.
Which at-rule has statement and block forms?
Where must @import normally appear?
What does @font-face contain?
When do nested media and supports conditions apply?
What inspects parsed rule survival?