-
Notifications
You must be signed in to change notification settings - Fork 756
Description
In #2864 we resolved to add inherit(property) to CSS, starting from custom properties.
inherit() covers use cases across two main axes:
- Using the inherited value of property A in property B (verbatim or with a calculation)
- Doing math on the inherited value
However, for 2 we don’t actually need inherit(), simply allowing inherit in calc() would work fine, is more elegant and familiar than inherit(), and being more limited it could likely ship earlier, possibly even for regular properties.
One downside is that unlike inherit(), it does not allow for providing a fallback value.
Use cases
(These are a subset of the use cases for inherit())
Custom property value that depends on parent value
With inherit():
* {
--depth: calc(inherit(--depth, 0) + 1);
}With inherit (note that the code had to be restructured due to the lack of fallback value):
:root {
--depth: 1;
* {
--depth: calc(inherit + 1);
}
}See also: #1962
Font metrics relative to parent
- [css-fonts-4] Percentages in font-weight for relative weights #2690
- [css-fonts] font-weight: bolder and lighter are counter-intuitive #2764
With inherit():
strong {
font-weight: clamp(600, 1.2 * inherit(font-weight), 999);
}With inherit:
strong {
font-weight: clamp(600, 1.2 * inherit, 999);
}And any other numerical typographic metric that can be specified in CSS, e.g. font-stretch, font-style (for variable fonts) etc
A few of the currentBackgroundColor use cases
Way fewer use cases than inherit() but it could still be useful for creating variations of the parent background color like so:
background-color: color-mix(in oklch, inherit, white 90%);Matching nested radii for simple cases
.child {
padding: 1em;
border-radius: calc(inherit - 1em);
}Shipping
Clearly, this doesn't make sense for every property, but then again, calc() also doesn't. Also, we should define that inherit in calc() computes to a value that would be useful, which would be different per property (though we could define a hierarchy, e.g. <number> over <length> if both are options), and IACVT if the computation doesn't make sense. We could ship for custom properties and an allowlist of regular properties to make this more tractable.