46

I would like to use inherit with calc() like this:

#foo {
  animation-duration: 10s;
}
#foo > .bar {
  animation-duration: calc(inherit + 2s); /* =12s */
}

But it don't seem to works.

Is it browsers bug or specs?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Yukulélé
  • 15,644
  • 10
  • 70
  • 94
  • Doesn't work that way ...... – DaniP Feb 06 '14 at 14:49
  • 1
    Seems you're out of luck. I tested if you could write `calc(100% + 2s)` but the duration properties don't take percentages. (Only `120%` by itself doesn't work either.) Sorry. Good question though! – Mr Lister Feb 06 '14 at 15:12
  • 1
    If [Less](http://lesscss.org/) is an option, you can do `@duration:10s; #foo {animation-duration:@duration;} #foo > .bar {animation-duration:@duration + 2s; }`. Of course, that is not exactly the same. There's also [CSS Variables](http://caniuse.com/#feat=css-variables), but that's science fiction for now. – Kobi Feb 06 '14 at 15:33
  • 2
    Hi, did you find a workaround? – oren Oct 06 '15 at 19:27
  • @oren nothink in pure css – Yukulélé Oct 07 '15 at 14:51
  • seconds are now supported in calc since Firefox's Quantum update (new Stylo CSS engine) – brandito Sep 27 '18 at 02:53

1 Answers1

50

The inherit keyword can only exist alone as a value in a property declaration. It cannot be used with anything else as a value, because it's a value in and of itself. This means it cannot be used with functions like calc() either. See CSS2.1 and css3-cascade.

So to put it simply, the spec doesn't allow using inherit that way.

With this in mind, in order to use an inherited property value as part of a child declaration, make a custom property which will be inherited instead:

#foo {
  --duration: 10s;
  animation-duration: var(--duration);
}
#foo > .bar {
  /* --duration is automatically inherited - no need for inherit keyword */
  animation-duration: calc(var(--duration) + 2s); /* =12s */
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 9
    That's right, and it's a shame that 100% isn't an option. – Mr Lister Feb 06 '14 at 20:49
  • 1
    The accepted answer does not provide a solution to the topic task. How to modify an inherited value with a formula based on the inherited value? – meir Nov 29 '20 at 08:09
  • 1
    @meir: Thanks for your comment. This Q&A was written in 2014 when CSS variables weren't yet a thing (as mentioned above), so there *wasn't* a solution to provide. There is now, so I'll add one. – BoltClock Dec 06 '20 at 03:35