Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use CSS calc() with inherit?

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?

like image 743
Yukulélé Avatar asked Feb 06 '14 14:02

Yukulélé


People also ask

Can I use CSS inherit?

The inherit CSS keyword causes the element to take the computed value of the property from its parent element. It can be applied to any CSS property, including the CSS shorthand property all . For inherited properties, this reinforces the default behavior, and is only needed to override another rule.

How does the CALC () function work on values in CSS?

CSS calc() is a function used for simple calculations to determine CSS property values right in CSS. The calc() function allows mathematical expressions with addition (+), subtraction (-), multiplication (*), and division (/) to be used as component values.

How do CSS styles get inherited?

The color property is also inherited. Inheritance in CSS occurs when an inheritable property is not set on an element. It goes up in its parent chain to set the property value to its parent value.

Where do we use Calc in CSS?

The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length> , <frequency> , <angle> , <time> , <percentage> , <number> , or <integer> is allowed.


1 Answers

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 */ } 
like image 192
BoltClock Avatar answered Sep 20 '22 13:09

BoltClock