# Directive

> The Directive in CSS is an instruction, like `@import` or `@media`, that provides metadata or conditions for applying styles.

## What are at-rules in CSS?

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.

- Topic: Basics
- Type: concept, syntax

## Example

Wrong:

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

Right:

```css
@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.

## Related cards

- [@media](https://wonderium.dev/css/media.md)
- [@supports](https://wonderium.dev/css/supports.md)
- [@import](https://wonderium.dev/css/import.md)
- [@layer](https://wonderium.dev/css/layer.md)
- [CSS Rule](https://wonderium.dev/css/css-rule.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/directive
- Last updated: 2026-11-25
