CSS variables, also known as Custom Properties, allow you to store values that can be reused throughout your CSS.
They are defined with a name that starts with two dashes ( e.g., --my-variable ) and can be accessed using the var() function as the value of any CSS property.
<!DOCTYPE html>
<html>
<head>
<title>ID Sandbox</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="/base.css" />
<link rel="stylesheet" href="/styles.css" />
</head>
<body>
<div class="card">
<h1>Episode IV: A New Hope</h1>
<p>It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire.</p>
<p>During the battle, Rebel spies managed to steal secret plans to the Empire's ultimate weapon, the DEATH STAR, an armored space station with enough power to destroy an entire planet.</p>
<p>Pursued by the Empire's sinister agents, Princess Leia races home aboard her starship, custodian of the stolen plans that can save her people and restore freedom to the galaxy...</p>
</div>
</body>
</html>body {
--heading-color: rebeccapurple;
--paragraph-color: darkcyan;
}
h1 {
color: var(--heading-color);
}
p {
color: var(--paragraph-color);
}body {
background: hsl(227.1 29.5% 16.7%);
block-size: calc(100vh - 12px);
border: 2px solid white;
display: grid;
font-family: 'Silka', sans-serif;
font-size: 1.125rem;
font-size: 1.125rem;
margin: 0.25em;
padding: 0 2rem;
place-content: center;
text-align: center;
h1 {
font-size: 2.5rem;
font-weight: bold;
margin-block-start: 0;
}
.card {
background: hsl(0 100% 100%);
border-radius: 0.25rem;
padding: 4rem 2rem;
}
}Variable values can be a string, keyword, number, color, unit, css function, or even another css variable. Basically any value that a standard css property would accept, or even just part of it.
:root {
// String.
--content-grid-areas: 'sidebar content';
// Keyword.
--callout-justify-content: space-between;
// Number.
--card-index: 3;
// Color.
--card-color-primary: hsl(141.6 72.4% 45.2%);
// Unit.
--card-border-radius: 0.25rem;
// CSS Function.
--icon-size: calc(1.5rem + 1vw);
// CSS Variable.
--card-color-primary: var(--color-blue-300);
}
CSS Variables are most useful when you need to store a value that is going to be reused throughout your CSS.
This becomes incredibly powerful when theming or using calculations to generate values.
They are also self-referencing, which simplifies the debugging process and prevents magic numbers from being used.
<!DOCTYPE html>
<html>
<head>
<title>ID Sandbox</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="/base.css" />
<link rel="stylesheet" href="/styles-variables.css" />
</head>
<body>
<div class="grid">
<article class="card card--hope">
<h2>Episode IV:</br>A New Hope</h2>
<div class="skeleton-text"></div>
<div class="skeleton-text"></div>
<button>Watch</button>
</article>
<article class="card card--empire">
<h2>Episode V:</br>The Empire Strikes Back</h2>
<div class="skeleton-text"></div>
<div class="skeleton-text"></div>
<button>Watch</button>
</article>
<article class="card card--jedi">
<h2>Episode VI:</br>Return of the Jedi</h2>
<div class="skeleton-text"></div>
<div class="skeleton-text"></div>
<button>Watch</button>
</article>
</div>
</body>
</html>body {
border: 2px solid white;
block-size: calc(100vh - 12px);
display: grid;
font-family: 'Silka', sans-serif;
font-size: 1.125rem;
margin: 0.25em;
padding: 0 2rem;
place-items: center;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
h2 {
font-size: 1.25rem;
font-weight: bold;
margin-block-start: 0;
}
.skeleton-text {
block-size: 1.5rem;
background: hsl(0 0% 90%);
border-radius: 2px;
inline-size: 90%;
}
button {
appearance: none;
background: hsl(0 0% 100%);
border: 2px solid hsl(0 0% 50%);
border-radius: 4px;
font-size: 1rem;
font-weight: bold;
letter-spacing: 0.125em;
padding: 0.5rem 1rem;
text-transform: uppercase;
margin-block-start: 1rem;
}
.card {
display: grid;
gap: 1rem;
grid-template-rows: 1fr auto auto auto;
padding: 2rem 1rem;
}body {
background: hsl(227.1 29.5% 16.7%);
}
.card {
background: hsl(0 100% 100%);
border: solid hsl(0 0% 0%);
border-width: 12px 4px 4px;
border-radius: 0.25rem;
&:where(.card--hope) {
border-color: hsl(141.6 72.4% 45.2%);
h2 {
color: hsl(141.6 72.4% 45.2%);
}
button {
border-color: hsl(141.6 72.4% 45.2%);
}
}
&:where(.card--empire) {
border-color: hsl(219.6 100% 50.2%);
h2 {
color: hsl(219.6 100% 50.2%);
}
button {
border-color: hsl(219.6 100% 50.2%);
}
}
&:where(.card--jedi) {
border-color: hsl(0.7 89.6% 57.2%);
h2 {
color: hsl(0.7 89.6% 57.2%);
}
button {
border-color: hsl(0.7 89.6% 57.2%);
}
}
button {
border-radius: 0.25rem;
}
}:root {
--global-border-radius: 0.25rem;
--color-blue: hsl(219.6 100% 50.2%);
--color-green: hsl(141.6 72.4% 45.2%);
--color-red: hsl(0.7 89.6% 57.2%);
--color-slate: hsl(227.1 29.5% 16.7%);
--color-white: hsl(0 0% 100%);
}
body {
background: var(--color-slate);
}
.card {
background: var(--color-white);
border: solid var(--card-color-primary);
border-width: 12px 4px 4px;
border-radius: var(--global-border-radius);
h2 {
color: var(--card-color-primary);
}
button {
border-radius: var(--global-border-radius);
border-color: var(--card-color-primary);
}
&:where(.card--hope) {
--card-color-primary: var(--color-green);
}
&:where(.card--empire) {
--card-color-primary: var(--color-blue);
}
&:where(.card--jedi) {
--card-color-primary: var(--color-red);
}
}