Variable Composer — CSS

Play with CSS variables: 10 levels of custom properties, scoping, calc and theming. A CSS variables game for mastering dynamic design systems.

What you learn in this game

CSS Custom Properties (CSS variables) start with --. They're defined in a selector (usually :root) and used with var(--name). They work like variables in a programming language.

CSS variables can be overridden in a more specific selector. The override value applies to that element and all its descendants. That makes per-component theming easy.

var(--name, fallback) takes a second parameter as the fallback. If the variable isn't defined, the fallback is used. Useful for default values.

calc() does calculation in CSS. Combined with a variable: calc(var(--x) * 2). Useful for spacing systems and responsive typography.

currentColor is a keyword that refers to the current color value. Useful for making a border, shadow, or SVG that automatically follows the text color.

What the levels ask

  1. Define the variable --warna-utama with the value "crimson" in :root, then use it in #box with the color property.
  2. Define --bg: #f0f0f0 and --text: #333 in :root. Apply them in #card as background-color and color.
  3. Define --accent: blue in :root. Then override --accent to "red" specifically in #special. Apply color: var(--accent) in .item.
  4. Use var() with a fallback: apply color on #text using the variable --heading-color, which is NOT defined, with the fallback "green".
  5. Define --base-size: 16px in :root. Apply font-size in #heading using calc(var(--base-size) * 2) so it becomes 32px.
  6. Build a spacing system: --space: 8px in :root. Apply padding: calc(var(--space) * 3) in #card (= 24px).
  7. Set color: blue on #icon-box. Then use currentColor for border-color so the border is also blue.
  8. Make a theme: --bg: white and --text: #222 in :root. Override in .dark: --bg: #1a1a1a and --text: #eee. Apply it to #page.
  9. Define --radius: 12px in :root. Apply it as border-radius in #pill.
  10. Make a design system: --primary: #2563eb, --radius: 8px, --space: 16px in :root. Apply them in #card: background-color, border-radius, and padding.