Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating new value of CSS variable based on existing value

How do I recalculate a new value of a CSS variable (custom property) in a sub-element using calc() to modify the value inherited from a parent element?

This doesn’t work:

div {
  height: var(--foo);
  border: 1px solid #f00;
}

.a {
  --foo: 100px;
}

.b {
  --foo: calc(var(--foo) - 25px);
}
<div class=a>
  <div class=b>
    foo box
  </div>
</div>

The browser seems to completely unset the value of --foo on .b.

Using the inherit keyword in the calc() expression also doesn’t work.

like image 934
Daphne Preston-Kendal Avatar asked Oct 16 '25 07:10

Daphne Preston-Kendal


1 Answers

As other commenters have noted, this is not currently possible. However, the CSS WG recently voted to add inherit as a function to the CSS Values spec, which will make this possible once it’s supported by browsers.

like image 111
Daphne Preston-Kendal Avatar answered Oct 18 '25 23:10

Daphne Preston-Kendal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!