CSS custom properties (also known as CSS variables) let you store reusable values and control how they inherit through your component tree. This article explains how inheritance works for custom properties, how to set them up for layout components, and how to avoid common pitfalls. You will learn the difference between inherited and non-inherited properties, how to use the inherit, initial, unset, and revert keywords, and how to structure your components for maximum flexibility.
How Custom Properties Inherit
Custom properties (declared with --name) are inherited by default, unlike many other CSS properties. This means if you set --gap: 16px on a parent element, every child element can read that value via var(--gap). This is powerful for theming and component design because you can define a set of design tokens at a high level and have them cascade down.
However, inheritance is not the same as the cascade. The cascade determines which declaration wins when multiple rules target the same element. Inheritance only applies when no other value is set. For custom properties, if you set --gap: 24px on a child, that value overrides the inherited one for that child and its descendants.
Setting Up Reusable Layout Components
When building layout components (grids, flex containers, cards), you can use custom properties to expose configuration options without forcing consumers to write complex CSS. For example, a grid component might accept --grid-columns, --grid-gap, and --grid-padding.
Here is a typical pattern:
.grid {
--grid-columns: 12;
--grid-gap: 24px;
display: grid;
grid-template-columns: repeat(var(--grid-columns), 1fr);
gap: var(--grid-gap);
}
.grid--compact {
--grid-gap: 12px;
}
Now, a consumer can override any of these variables inline or via a utility class. This keeps the component’s internal CSS simple while offering flexibility.
Common Pitfalls and Misconceptions
One common mistake is assuming that custom properties behave like CSS variables in preprocessors (e.g., Sass). They are not compile-time; they are live and can change at runtime, but they also inherit, which can lead to unexpected results if you forget that a variable set on a parent affects all children.
Another pitfall is using var() without a fallback. If a custom property is not defined, the property becomes invalid at computed-value time, and the element’s style may be ignored. Always provide a fallback: color: var(--text-color, #333).
Also, custom properties are case-sensitive. --Gap and --gap are different. Stick to a consistent naming convention.
How to Choose Between Inherited and Non-Inherited
Most standard CSS properties are either inherited (like color, font-family) or not (like margin, padding). Custom properties are always inherited, but you can simulate non-inheritance by resetting them on each component root.
For layout components, you often want to reset custom properties at the component boundary to avoid leakage. For instance, if you have a .card component that uses --card-padding, you should set a default on the .card class itself, not rely on a global value that might be overridden elsewhere.
Use the inherit, initial, unset, and revert keywords to control inheritance for standard properties. For custom properties, you can use initial to reset to the initial value (which is an empty value), but that is rarely useful. Instead, explicitly set a default.
Performance and Browser Support
Custom properties are supported in all modern browsers. There is no significant performance cost to using them in normal quantities. However, using thousands of distinct custom properties on a single page might increase memory usage slightly, but that is rarely a concern for layout components.
One thing to note: custom properties cannot be used in media queries directly (e.g., @media (max-width: var(--breakpoint)) because media queries are evaluated before the cascade. You need to use a JavaScript solution or duplicate the query.
Alternatives to Custom Properties
Before custom properties were widely supported, developers used CSS preprocessors (Sass, Less) for variables. Those still work, but they are compile-time and cannot be changed at runtime. For layout components, you can also use CSS classes to define variations (e.g., .grid--2col), but that multiplies the number of classes.
Another alternative is using the style attribute to set inline custom properties. This is useful when you need to pass values from JavaScript. For example, element.style.setProperty('--gap', '16px').
Frequently Asked Questions
Can I use custom properties in media queries?
No, you cannot use var() in media query conditions. Media queries are evaluated before the cascade, so custom properties are not available. You must duplicate the media query or use JavaScript to change the property based on viewport size.
How do I reset a custom property to its inherited value?
To reset a custom property to the value it would have inherited, you can use the inherit keyword: --gap: inherit. This explicitly sets the property to the inherited value, which is useful when you want to override a previous declaration.
What is the difference between var(--x) and var(--x, fallback)?
The fallback is used if the custom property is not defined or is invalid. Without a fallback, the property using var() becomes invalid at computed-value time, which can cause the entire declaration to be ignored. Always provide a fallback for robustness.
Do custom properties work with CSS Grid and Flexbox?
Yes, you can use custom properties anywhere a CSS value is expected, including grid-template-columns, gap, flex-basis, etc. This makes them ideal for layout components.
Can I change a custom property with JavaScript?
Yes, you can set or change custom properties via the style property: element.style.setProperty('--gap', '16px'). This allows dynamic theming and responsive behavior.
Are custom properties inherited by all descendants?
Yes, custom properties are inherited by default. If you set --color on a parent, all children can read it. To prevent inheritance, you need to explicitly set a new value on the child.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Set a Google Play Budget & Stop Accidental Purchases
- Destiny 2 Error Code Anteater: Fixes & Causes
- View Android Photos on Chromebook: Step-by-Step
- Fix Windows Could Not Prepare the Computer to Boot
- Windows 11 Security Settings: Best Config Guide
- How to Clean Windows Outside: Step-by-Step Guide