CSS Variables…
- can be defined globally or locally. 🌎🏡
- can be REDFINED 😎
- are scoped to the element they are declared on. 👯
- are inherited by child elements. 🐣
- can be overridden by cascading or specificity rules. 😒
- can pass through the shadow DOM boundary. 🪄
- can not modify content inside of iframes. ☹️
- can be declared within media queries but not as media query rules or values … (YET). 🫢
Scope
CSS scoping works similar to how it works in languages like Javascript:
// index.js =======================================
let color = 'red'; // define color
function changeColor() {
color = 'blue'; // redefine color within scope
console.log(color); // 'blue'
let backgroundColor = 'yellow' // define backgroundColor
console.log(backgroundColor); // 'yellow'
}
console.log(backgroundColor); // Uncaught ReferenceError: backgroundColor is not defined
console.log(color); // 'red'
Similarly, in CSS we might write:
:where(html) {
--color: red;
}
.card {
border-color: var(--color); /* red */
}
.card__header {
--background-color: blue;
--color: yellow;
background-color: var(--background-color); /* blue */
color: var(--color); /* yellow */
}
.card__footer {
background-color: var(--background-color); /* undefined */
color: var(--color); /* red */
}<!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">
<header class="card__header">HEADER</header>
<div class="card-body"></div>
<footer class="card__footer">FOOTER</footer>
</div>
</body>
</html>body {
background-color: hsl(227.1 29.5% 16.7%);
block-size: 100vh;
display: grid;
place-items: center;
}
.card {
aspect-ratio: 2/3;
background: white;
block-size: 75vb;
border: 6px solid;
display: grid;
grid-template-rows: 1fr 4fr 1fr;
padding: 0.25rem;
}
:where(.card__header, .card__footer) {
border: 2px solid hsl(227.1 29.5% 96.7%);
display: grid;
place-items: center;
font-size: 1.5rem;
font-weight: bold;
font-family: 'Silka', sans-serif;
}Did you know?
:root is a pseudo-class that matches the root element of a tree representing the document. It is equivalent to html in CSS.
However :root has higher specificity. When setting global variables, I recommend using :where(html) instead to keep the specificity low.
:where(html) {
--color: red;
}