Skip to content

[css-values-5] Allow inherit in calc() (and friends), color-mix(), RCS #9757

@LeaVerou

Description

@LeaVerou

In #2864 we resolved to add inherit(property) to CSS, starting from custom properties.

inherit() covers use cases across two main axes:

  1. Using the inherited value of property A in property B (verbatim or with a calculation)
  2. 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

Generic --depth (1, 2)

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

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

#5292

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

#7707

.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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions