On this page12 sections
Lecture 046: Variables & @property
| Category | 7: Advanced Styling |
| Lecture | 046 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
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
--nameand use them withvar() - 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
<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
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
<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:
<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:
<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:
<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:
<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
- Declare a variable
--mainwith the valuetealfor the whole page. - Use it as a text colour, falling back to black.
- How can one section use a different value?
- Change
--mainto orange from JavaScript. - Why would you use
@property?
Practice Answers
:root { --main: teal; }color: var(--main, black);- Set the variable again on that section, e.g.
.promo { --main: crimson; }. document.documentElement.style.setProperty('--main', 'orange');- To give a variable a type so it can be animated or transitioned smoothly.
Key Takeaways
--name: valuedeclares,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;
@propertymakes them animatable.