CSS variables, also known as custom properties, provide a powerful way to store and reuse values in your stylesheets. We’ll explore how to create, use, and manipulate custom properties, offering clear explanations, practical code examples, and exercises to help you master this valuable CSS feature.
Introduction to CSS Variables
CSS variables, represented by double hyphens --
, allow you to define and store values that can be reused throughout your stylesheet. They provide a level of abstraction, making your code more maintainable, flexible, and consistent.
Creating CSS Variables
To create CSS variables, use the :root
selector (which refers to the highest-level parent element, typically the html
element) and define your variables within it.
1 2 3 4 |
:root { --primary-color: #3498db; --font-size: 16px; } |
Here, we’ve defined two custom properties, --primary-color
and --font-size
, along with their initial values.
Using CSS Variables
Once you’ve defined your variables, you can reference and use them within your stylesheet using the var()
function.
For example, to apply the primary color to a button’s background:
1 2 3 |
button { background-color: var(--primary-color); } |
Using CSS variables enhances maintainability, as changes to the variable values propagate throughout your styles, ensuring consistency.
Benefits of CSS Variables
CSS variables offer several advantages:
- Reusability: You can reuse values across your stylesheet, making it easier to maintain and update your design.
- Consistency: CSS variables help maintain consistency in your design by ensuring that all instances of a specific value change together when the variable is updated.
- Readability: They enhance the readability of your CSS by providing descriptive variable names, making your code more self-explanatory.
- Responsive Design: CSS variables can be dynamically updated using JavaScript, which is beneficial for creating responsive designs.
CSS Variables in Detail
Variable Scope
CSS variables are scoped to the element where they are defined. However, if a variable is not found within a particular scope, the browser will traverse up the DOM tree to the :root
element to find the variable. This allows for fine-grained control over variable usage.
Default Values
You can assign default values to CSS variables using the var()
function.
For example, if --primary-color
is not defined, you can provide a default value:
1 2 3 |
button { background-color: var(--primary-color, #3498db); } |
In this example, if --primary-color
is not defined, the button’s background color defaults to #3498db
.
Practical Examples
Example 1: Create a Theme
Define custom properties for a light and dark theme, and apply these variables to your stylesheet. Create a button that toggles between the two themes using JavaScript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
:root { --primary-color-light: #3498db; --primary-color-dark: #2ecc71; --background-light: #f5f5f5; --background-dark: #333; } body { background-color: var(--background-light); } button { background-color: var(--primary-color-light); color: white; padding: 10px 20px; border: none; cursor: pointer; } |
Example 2: Dynamic Sizing
Use a CSS variable to control the font size of multiple elements. Create a button that increases the font size when clicked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
:root { --font-size: 16px; } p { font-size: var(--font-size); } button { background-color: #3498db; color: white; padding: 10px 20px; border: none; cursor: pointer; } |
Exercises
Exercise 1: Customizing a Website
Take a simple webpage with multiple elements and create a set of CSS variables for its colors, typography, and spacing. Apply these variables to the elements, and use JavaScript to provide users with options to customize the site’s appearance.
Exercise 2: Responsive Typography
Define CSS variables for different font sizes based on screen width. As the screen size changes, update the font size accordingly using media queries and JavaScript.
Browser Compatibility
CSS variables, or custom properties, are supported in modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. However, be cautious when using them in older browser versions, as they may not support custom properties.
Conclusion
CSS variables, or custom properties, are a valuable addition to your CSS toolkit. They promote reusability, consistency, and maintainability in your stylesheets, allowing you to create dynamic, responsive, and easily customizable web designs. Practice using CSS variables, experiment with different scenarios and see how they can enhance your web development projects.