Selectors & the Cascade Lesson 28 of 33
CSS Variables (Custom Properties) and calc()
Store colors, sizes and spacing in CSS variables, reuse them everywhere, build a light and dark theme, and do math with calc().
Imagine your website uses the color #0f5c4d in 40 places, and the client asks for a different green. With normal CSS, you'd search and replace 40 times, hoping you don't miss one. With CSS variables, you change it in one place.
Declaring and using a variable
A CSS variable (officially called a custom property) is a name that starts with two dashes. You usually declare them on :root, which means the whole page:
:root {
--brand: #0f5c4d;
--accent: #f2b33d;
--radius: 12px;
}
Then use them anywhere with var():
.btn {
background: var(--brand);
border-radius: var(--radius);
}
.badge {
background: var(--accent);
}
<style>
:root {
--brand: #0f5c4d;
--accent: #f2b33d;
--radius: 12px;
}
body { font-family: Arial, sans-serif; }
h2 { color: var(--brand); }
.card { border: 2px solid var(--brand); border-radius: var(--radius); padding: 14px; }
.btn { background: var(--brand); color: white; border: 0; padding: 10px 16px; border-radius: var(--radius); }
.tag { background: var(--accent); padding: 2px 8px; border-radius: var(--radius); font-size: 13px; }
</style>
<div class="card">
<h2>Sari-Sari Store <span class="tag">Open</span></h2>
<p>Load, snacks, and everyday needs.</p>
<button class="btn">Order</button>
</div>
In the editor, change --brand to #2f6fd6 or --radius to 0. Every part using that variable updates.
Fallback values
var() accepts a second value to use if the variable isn't defined:
color: var(--text-color, #333); /* use #333 if --text-color doesn't exist */
Variables follow the cascade
Variables are inherited like color or font-family, and they can be overridden for one part of the page. This is great for variations of a component:
<style>
.btn {
--bg: #0f5c4d;
background: var(--bg);
color: white;
border: 0;
padding: 10px 16px;
border-radius: 10px;
font-size: 16px;
margin: 4px;
}
.btn.danger { --bg: #b4372b; }
.btn.info { --bg: #2f6fd6; }
</style>
<button class="btn">Save</button>
<button class="btn danger">Delete</button>
<button class="btn info">Details</button>
Light and dark themes
Variables make dark mode simple: define the colors as variables, then change the variables in a media query. The rest of the CSS stays the same.
<style>
:root {
--bg: #ffffff;
--text: #1d2321;
--card: #f3f6f4;
--brand: #0f5c4d;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121615;
--text: #e6ebe8;
--card: #1f2523;
--brand: #5cc7a8;
}
}
body { background: var(--bg); color: var(--text); font-family: Arial, sans-serif; padding: 12px; }
.card { background: var(--card); padding: 16px; border-radius: 12px; }
h3 { color: var(--brand); margin-top: 0; }
</style>
<div class="card">
<h3>Theme demo</h3>
<p>Switch your device to dark mode and this card follows. Only the variables changed.</p>
</div>
This website's own dark mode works the same way.
calc(): math in CSS
calc() lets you do math with different units, which normal values can't do:
.main { width: calc(100% - 260px); } /* full width minus a 260px sidebar */
.hero { min-height: calc(100vh - 60px); } /* screen height minus the header */
.gap { margin: calc(var(--space) * 2); } /* double a variable */
You can use +, -, * and /. Put spaces around + and -: calc(100% - 20px) works, but calc(100%-20px) doesn't.
<style>
:root { --space: 8px; }
.box { background: #e3f1ec; border-left: 4px solid #0f5c4d; margin-bottom: var(--space); font-family: monospace; }
.s1 { padding: var(--space); }
.s2 { padding: calc(var(--space) * 2); }
.s3 { padding: calc(var(--space) * 4); }
</style>
<div class="box s1">padding: var(--space) = 8px</div>
<div class="box s2">padding: calc(var(--space) * 2) = 16px</div>
<div class="box s3">padding: calc(var(--space) * 4) = 32px</div>
A good starter set
Many projects begin with a block like this at the top of the CSS:
:root {
/* colors */
--brand: #0f5c4d;
--accent: #f2b33d;
--text: #1d2321;
--muted: #5b6662;
--bg: #fbfaf7;
--line: #e3e0d6;
/* shapes and spacing */
--radius: 12px;
--space: 8px;
--shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
/* text */
--font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04