Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reference a :root css variable within another :root variable?

:root {
    --foo: #fff;
    --bar: --foo;
}

A use case would be - I allow a primary colour to be set and be re-used within other variables.

I cant seem to find any information on this and i'm starting to think its not possible.

like image 934
Mike Hague Avatar asked Oct 20 '25 07:10

Mike Hague


1 Answers

Simply like this

:root {
    --foo: #fff;
    --bar: var(--foo);
}

You can also have a more complex case:

:root {
    --foo: #fff;
    --bar:3px solid var(--foo); /* Define a border using the --foo color*/
}

But you should notice that such thing is useless in most of the cases because you are evaluating a variables inside :root using another one. If you change the main variable later, nothing will happen.

Related to get more details: CSS scoped custom property ignored when used to calculate variable in outer scope

like image 110
Temani Afif Avatar answered Oct 22 '25 20:10

Temani Afif



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!