Skip to content

":root" vs. "*" for variable declaration #121

Answered by sebastiano-guerriero
loxy asked this question in Q&A
Discussion options

You must be logged in to vote

Hi!

Yes, we use :root, * if we want a variable to be editable on every element.

In general, if you set a variable in the root:

:root {
  --variable-multiplier: 1;
  --variable: calc(10px + var(--variable-multiplier)); 
}

On a component level, if you do this:

.component {
  --variable-multiplier: 2;
}

--variable won't be 20px, but 10px. However, if you use the :root, * selector, you can edit --variable-multiplier on a component level, and affect --variable on that component.

This technique should be used with caution. You can't declare all the variables using :root, * or your site performance will be affected.

Hope this helps!

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by loxy
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants