CSS3 & Modern Layouts (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What are CSS Custom Properties (Variables) and how do they cascade?
junior LevelCode ExamplecssBasic
In Simple Words:
Declared with -- prefix and read via var():
css
:root {
--primary-color: #3b82f6;
--spacing-md: 16px;
}
.card {
background-color: var(--primary-color);
padding: var(--spacing-md);
}
/* Scoped override for dark mode */
.dark-theme {
--primary-color: #60a5fa;
}
- Unlike Sass variables (which are static and compiled away at build time), CSS Custom Properties are live in the browser DOM.
- They inherit through the DOM tree, can be modified via JavaScript (
element.style.setProperty('--primary', '#ff0000')), and dynamically update all matching elements in real time.