CSS Variables
In this page:
:root {
--variable-name: value;
}
selector {
property: var(--variable-name, fallback);
}
Declaring Variables in :root
To make your variables available globally across your stylesheets, you declare them inside the ':root' selector. This selector represents the highest-level element in the document structure, making variables accessible to all other selectors.
Example: Declaring Variables in :root
<style>
:root {
--brand-color: teal;
}
</style>
<p>Variable declared globally in :root</p>
Using the var() Function
To apply a declared variable value to a property, you use the var() function, passing your custom variable name inside the parentheses.
Example: Using the var() Function
<style>
:root {
--brand-color: teal;
}
p {
color: var(--brand-color);
}
</style>
<p>Uses the variable via var()</p>
Custom Property Fallback Values
The var() function allows you to specify a fallback value as a second argument. If the custom variable fails to load, the browser will automatically use the fallback value.
Example: Custom Property Fallback Values
<style>
p {
color: var(--undefined-color, gray);
}
</style>
<p>Falls back to gray since --undefined-color isn't set</p>
Variable Inheritance and Scope
Variables follow standard CSS inheritance and scoping rules. When declared inside a specific selector instead of :root, the variable is local and only accessible to that element and its children.
Example: Variable Inheritance and Scope
<style>
.card {
--card-color: navy;
color: var(--card-color);
}
</style>
<div class="card">Local variable, only accessible here and inside</div>
Theming with Variables
CSS variables make it easy to build dynamic website theme changers. By defining your page color variables inside :root and updating them under a custom class (like '.dark-mode'), you can toggle themes instantly.
Example: Theming with Variables
<style>
:root {
--bg: white;
--text: black;
}
.dark-mode {
--bg: black;
--text: white;
background: var(--bg);
color: var(--text);
}
</style>
<div class="dark-mode">Theme toggled by swapping the variables</div>
- Forgetting to write the double hyphen prefix (--) when naming or calling custom CSS variables.
- Attempting to reference local variables outside of their parent container scope.
- Misspelling variable names, which are case-sensitive.
- Declare global custom variables inside the high-level :root selector for global access.
- Apply variable values to properties using the var() function, passing custom variable names inside the parentheses.
- Provide fallback values inside your var() functions, and leverage variable inheritance to make toggling themes clean and easy.
Standard CSS custom properties (variables) are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline