Override a variable for a section

Reuse one card style with different colours.

What to build

A variable that is overridden inside a section.

Requirements

  • :root sets --accent to teal.
  • .warning changes --accent to crimson.
  • .card has only a border-left of 4px solid var(--accent).
  • .note has the text colour var(--missing, navy), where --missing is never defined.

Constraints

Write exactly one .card rule, and do not write a rule like .warning .card.

Example

.dark { --bg: black; }

Why it works

Custom properties inherit, so a card reads the value from its nearest ancestor. The fallback provides a value when nothing has been defined.

Hints

Hint 1

Only the variable changes inside .warning.

Hint 2

A fallback is the second argument of var().

Solution

Show one correct solution (try it yourself first)
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Scope</title>
  <style>
    :root { --accent: teal; }
    .warning { --accent: crimson; }
    .card { border-left: 4px solid var(--accent); }
    .note { color: var(--missing, navy); }
  </style>
</head>
<body>
  <div class="card">Normal</div>
  <div class="warning"><div class="card">Danger</div></div>
  <p class="note">A note</p>
</body>
</html>

Lesson: Custom properties (CSS variables) →