Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

inherit(): a draft function for a parent custom property

Distinguish the experimental inherit() function from the established inherit keyword and var().

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

margin-left: inherit(--space, 5px);

The draft inherit() function reads the inherited computed value of a named custom property. This is useful when the child also declares that custom property: var(--space) reads the child's current value, whereas inherit(--space) is intended to access the inherited value. It is not the same syntax as the ordinary inherit keyword, which applies inheritance to the property being assigned.

The current draft accepts custom-property names such as --space, with an optional fallback for a missing or invalid inherited value. It does not provide general access to a parent's standard properties such as width. Nor does reading a computed value imply reading the final used pixel dimensions of the parent box. This is experimental syntax, so keep a working alternative.

The parent in the example declares --space: 12px and the child overrides it with 30px. Chromium 157 rejects the tested inherit() declaration, so the earlier margin of 7px survives; var(--space) resolves to 30px. The missing-property branch also retains 7px because the entire unsupported declaration is rejected, not because the function selected that fallback.

Examples

const parent=document.createElement('div');parent.style.setProperty('--space','12px');
const child=document.createElement('div');child.style.setProperty('--space','30px');child.style.marginLeft='7px';
child.style.marginLeft='inherit(--space, 5px)';parent.append(child);document.body.append(parent);
const fromParent=getComputedStyle(child).marginLeft;
child.style.marginLeft='var(--space)';const fromSelf=getComputedStyle(child).marginLeft;
child.style.marginLeft='7px';child.style.marginLeft='inherit(--missing, 5px)';
console.log('supported='+CSS.supports('margin-left','inherit(--space, 5px)')+'; parent='+fromParent+'; self='+fromSelf+'; missing='+getComputedStyle(child).marginLeft+'; standard-property='+CSS.supports('width','inherit(width)'));

Result

supported=false; parent=7px; self=30px; missing=7px; standard-property=false

Do not interpret the 7px results as successful inheritance. The support check is false; the example demonstrates a safe fallback in this browser.

Sources