On this page12 sections
  1. Learning Objectives
  2. 1. Declaring and Using
  3. 2. Fallbacks
  4. 3. Overriding in a Section
  5. 4. A Light/Dark Theme
  6. 5. Variables in Media Queries
  7. 6. Changing Variables With JavaScript
  8. 7. @property: Typed, Animatable Variables
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 046: Variables & @property

Category7: Advanced Styling
Lecture046 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

A CSS variable (custom property) stores a value once, such as a brand colour, and reuses it everywhere. Change it in one place and the whole site updates. Variables can be overridden per section, changed in media queries, updated from JavaScript, and, with @property, even animated.


Learning Objectives

  • Declare variables with --name and use them with var()
  • Provide fallbacks, and override variables in a section or media query
  • Build a light/dark theme with variables
  • Change variables from JavaScript
  • Register typed, animatable variables with @property

1. Declaring and Using

Example

html
<style>
  :root {
    --brand: #1971c2;
    --radius: 10px;
    --space: 16px;
  }
  .btn  { background: var(--brand); color: white; border: 0; border-radius: var(--radius); padding: 10px var(--space); }
  .card { border: 2px solid var(--brand); border-radius: var(--radius); padding: var(--space); margin-top: 12px; }
</style>

<button class="btn">Brand button</button>
<div class="card">A card with the same brand colour and radius.</div>
  • Variable names start with two dashes: --brand.
  • :root (the <html> element) makes them available everywhere.
  • var(--brand) reads the value.

2. Fallbacks

css
color: var(--accent, #e8590c);   /* use #e8590c if --accent isn't set */

3. Overriding in a Section

Variables inherit, so a section can change one, and everything inside it follows.

Example

html
<style>
  :root { --brand: #1971c2; }
  .btn { background: var(--brand); color: white; border: 0; border-radius: 8px; padding: 10px 16px; margin: 4px; }
  .danger-zone { --brand: #e03131; border: 1px dashed #e03131; padding: 10px; margin-top: 10px; }
</style>

<button class="btn">Normal</button>
<div class="danger-zone">
  <button class="btn">Inside .danger-zone: red</button>
</div>

4. A Light/Dark Theme

Example: click the button:

html
<style>
  :root { --bg: #ffffff; --text: #212529; --card: #f1f3f5; }
  .dark { --bg: #1a1b1e; --text: #e9ecef; --card: #2c2e33; }
  body { background: var(--bg); color: var(--text); font-family: system-ui; transition: background 0.3s, color 0.3s; }
  .card { background: var(--card); padding: 14px; border-radius: 10px; }
</style>

<button onclick="document.body.classList.toggle('dark')">Toggle dark mode</button>
<div class="card"><h3>Themed card</h3><p>Every colour comes from a variable.</p></div>

The theme only swaps three variables; no other rule changes.


5. Variables in Media Queries

Example: drag the preview corner:

html
<style>
  :root { --gap: 8px; --cols: 1; }
  @media (min-width: 500px) { :root { --gap: 16px; --cols: 3; } }
  .grid { display: grid; grid-template-columns: repeat(var(--cols), 1fr); gap: var(--gap); }
  .grid div { background: #d0ebff; padding: 14px; border-radius: 8px; }
</style>

<div class="grid"><div>A</div><div>B</div><div>C</div></div>

6. Changing Variables With JavaScript

Example: move the slider:

html
<style>
  :root { --hue: 210; }
  .swatch { height: 80px; border-radius: 10px; background: hsl(var(--hue) 80% 50%); }
</style>

<input type="range" min="0" max="360" value="210" oninput="document.documentElement.style.setProperty('--hue', this.value)">
<div class="swatch"></div>

setProperty('--name', value) sets a variable; getComputedStyle(el).getPropertyValue('--name') reads it.


7. @property: Typed, Animatable Variables

A normal variable is just text, so the browser can't animate it. @property gives it a type.

Example: hover to spin the gradient:

html
<style>
  @property --angle {
    syntax: '<angle>';
    inherits: false;
    initial-value: 0deg;
  }
  .ring {
    width: 140px; height: 140px; border-radius: 50%;
    background: conic-gradient(from var(--angle), #f76707, #ae3ec9, #1971c2, #f76707);
    transition: --angle 1s;
  }
  .ring:hover { --angle: 360deg; }
</style>

<div class="ring"></div>

Without @property, the gradient would jump instead of turning smoothly.


Common Beginner Mistakes

1. One dash. -brand isn't a variable; it needs two: --brand.

2. Names are case-sensitive. --Brand and --brand are different variables.

3. Variables inside media query conditions. @media (min-width: var(--bp)) doesn't work; you can only set variables inside media queries.

4. Forgetting units. --size: 20 then width: var(--size) is invalid. Store 20px, or use calc(var(--size) * 1px).


Practice Questions

  1. Declare a variable --main with the value teal for the whole page.
  2. Use it as a text colour, falling back to black.
  3. How can one section use a different value?
  4. Change --main to orange from JavaScript.
  5. Why would you use @property?

Practice Answers

  1. :root { --main: teal; }
  2. color: var(--main, black);
  3. Set the variable again on that section, e.g. .promo { --main: crimson; }.
  4. document.documentElement.style.setProperty('--main', 'orange');
  5. To give a variable a type so it can be animated or transitioned smoothly.

Key Takeaways

  • --name: value declares, var(--name, fallback) reads.
  • Variables inherit, so override them per section or in media queries.
  • Themes become a matter of swapping a few variables.
  • JavaScript can change them; @property makes them animatable.