CSS Variables (Custom Properties)
Goal: Use CSS custom properties for reusable styling.
1. Define variables
:root {
--main-color: #007bff;
--padding-base: 10px;
}
2. Use variables
button {
background: var(--main-color);
padding: var(--padding-base);
}
3. Change by theme
.dark-theme {
--main-color: #222;
}
4. Calculations
.card {
padding: calc(var(--padding-base) * 2);
}
Tips:
- CSS variables inherit through DOM.
- Useful for theme switching via JavaScript.