Careeroza
Custom Properties
Declaring and Using Variables
Example
Declare variables with --name (usually on :root) and read them with var(). Change the color in one place and every use updates:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
:root {
--brand: #04AA6D;
--radius: 8px;
--space: 12px;
}
.btn { background: var(--brand); color: white; border: 0; padding: var(--space) calc(var(--space) * 2); border-radius: var(--radius); font: 15px Arial; }
.card { border: 2px solid var(--brand); border-radius: var(--radius); padding: var(--space); margin-top: var(--space); font-family: Arial; }
</style>
</head>
<body>
<button class="btn">Brand button</button>
<div class="card">Card using the same variables</div>
</body>
</html>Result
Scope and Fallbacks
Example
Variables are inherited, so they can be overridden for one part of the page. var(--x, fallback) is used if --x is not set:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
:root { --accent: #1971c2; }
.danger { --accent: crimson; }
p { color: var(--accent); font-family: Arial; }
.missing { color: var(--not-defined, darkorange); }
</style>
</head>
<body>
<p>Default accent</p>
<div class="danger"><p>Accent overridden inside .danger</p></div>
<p class="missing">Fallback color</p>
</body>
</html>Result