# :root

> The `:root` CSS pseudo-class selects the root element of the document, typically the `<html>` element.

## What is the difference between :root and html in CSS?

The pseudo-class that targets the highest-level parent of the document, typically the `<html>` element. It's often used to define global CSS variables that can be accessed throughout the entire stylesheet, making it easier to manage consistent design values.

- Topic: Pseudo Classes
- Type: pseudo-class
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 1+, Safari iOS 1+

## Example

Wrong:

```css
body {
  --text: #222;
}
html {
  color: var(--text);
}
```

Right:

```css
:root {
  --text: #222;
  color: var(--text);
}
```

Custom properties inherit downward, so tokens on `:root` are visible everywhere, including the `html` element.

## Common mistakes

- An `html` rule losing to a `:root` rule, because `:root` has class-level specificity.
- Defining custom properties on `body`, so `html`-level styles can't read them.

## Key points

- Global design tokens are declared on `:root`, like `:root { --brand: #0055ff; }`.
- Theme overrides use `:root[data-theme='dark']` or a `prefers-color-scheme` media query.

## Interview questions

### Is :root the same as html?

In HTML documents they match the same element, but `:root` has higher specificity, like a class.

### Why are CSS variables defined on :root?

Because `:root` is the top element, so every element in the page inherits its custom properties.

## Related cards

- [var()](https://wonderium.dev/css/var.md)
- [Variables](https://wonderium.dev/css/variables.md)
- [Specificity](https://wonderium.dev/css/specificity.md)
- [color-scheme](https://wonderium.dev/css/color-scheme.md)
- [`<html>`](https://wonderium.dev/html/html.md)

---

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