Directive

Term side of the Card.
Topic: Basics
Unit title: Directive
Categories: Concept, Syntax
Explanation side of the Card

Explanation: The special instruction that starts with an '@' symbol and controls how styles are applied. Examples include `@import` for including other stylesheets and `@media` for applying styles based on conditions like screen size. These instructions help manage and organize CSS more effectively.

Example

Wrongcss
.nav {
  display: flex;
}
@import url("theme.css");
Rightcss
@import url("theme.css");
.nav {
  display: flex;
}

@import only works before all other rules except @charset and @layer statements.

Common mistakes

  • An @import placed after other rules is ignored.
  • Writing @media (max-width: 600) without a unit, so it never matches.

Key points

  • @charset and @import sit at the top of the stylesheet, before other rules.
  • Media queries use @media with valid conditions, like (width >= 48rem).
  • Feature checks use @supports instead of browser sniffing in JS.

Interview questions

What is the difference between an at-rule and a selector rule?

A selector rule styles elements. An at-rule starts with @ and gives instructions, such as conditions, imports, fonts or animations.

Can at-rules be nested?

Yes, conditional at-rules like @media, @supports and @container can be nested inside each other and inside style rules.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy