<!doctype html>
<html>
<head>
<title>CSS Variables</title>
<link href="/assets/reset.css" rel="stylesheet">
<link href="setup.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<section>
<p>This is some text in the first element.</p>
<p>There can be multiple elements within it, maybe even that wrap to multiple lines.</p>
</section>
<section>
<p>And some more in the second element.</p>
</section>
<section>
<p>Then a third one, too.</p>
</section>
</body>
</html>
/* Special “entire document” selector, akin to `html`. */
:root {
--spacing: 1rem; /* So we can reuse, below. */
--border: solid black calc(var(--spacing) / 5); /* Any value, including `calc()`. */
}
/* Same as before, but now using the `var()`. */
body {
font-family: sans-serif;
padding: var(--spacing);
}
section {
/* Second value here is a “fallback,” used if a variable hasn’t been declared. */
background-color: var(--background, deepskyblue);
border-block-start: var(--border);
padding: var(--spacing);
&:nth-child(2) { --background: gold; }
&:nth-child(3) { --background: tomato; }
&:not(:first-child) { margin-block-start: var(--spacing); }
}
section:nth-child(2) {
/* Overries our `:root` one, within this selector. */
--spacing: 3rem;
}