Skip to content

[css-variables][css-cascade] When are variables computed? #2793

@chharvey

Description

@chharvey

I’m a bit confused by var() functions are substituted at computed-value time” and how it relates to inheritance.

div {
  --color-accent: lime;
  --color-text: var(--color-base);
  --color-bg: var(--color-accent);
}
figure.theme-american {
  --color-base: red;
  --color-accent: blue;
}
figure.theme-irish {
  --color-base: green;
  --color-accent: orange;
}
p {
  /* isn’t --color-text: var(--color-base); inherited here? */
  /* isn’t --color-bg: var(--color-accent); inherited here? */
  color: var(--color-text);
  background-color: var(--color-bg);
}
<div>
  <figure class="theme-american">
    <p>My text should be red, and my background should be blue.</p>
    <!-- They’re not. The text is black (inherited) and the background is lime. -->
  </figure>
</div>

CodePen

I thought all custom properties inherit, so wouldn’t the paragraph inherit the --color-text and --color-bg properties from the div? Since it also inherits the --color-base and --color-accent from the figure, shouldn’t the color be red and the background-color be blue?

Sources:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closed as Question AnsweredUsed when the issue is more of a question than a problem, and it's been answered.css-variables-1Current Work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions