w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.46k stars 658 forks source link

[css-variables] define custom property based on parent's (inherited) value #1962

Open dubrowsky opened 6 years ago

dubrowsky commented 6 years ago

According to the Resolving Dependency Cycles section of the spec it's not possible to use var(--property) while defining the --property itself.

Here's an illustration of the problem I'd like to solve:

.table {
    width: 60vw;
    --width: 60vw;
}

.column {
    width: 25%;
   // should be (60vw / 4) = 15vw
    --width: calc( var(--width) / 4);
}

.something-inside-the-column {
     // should be (60vw / 4) * 0.1 = 6vw
     font-size: calc( var(--width) * 0.1 ); 
}

So I'd like to use the inherited value if the own is not yet defined. If this behavior is hard to implement, maybe the specification can introduce another function to use inherited values explicitly, something like this:

    --width: calc( inherited-var(--width) * 0.1 );

The solution suggested here (just use two different props) is not the answer, because I want my components to be "context-agnostic". Please tell me if my case is not clear enough, I'll try to provide some better example then. Thank you!

LeaVerou commented 3 years ago

I have definitely needed this too. I'd support a parent-var() (or similar) function, though it's possible to have a more general form that works for any property (see #2864).