Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Jonah Shi
Jonah Shi
10,140 Points

Can I use html instead of :root to declear css variable? This is the question in course "Declaring and Using CSS Variabl

Can I use html instead of :root to declear css variable? Why? This is the question in course "Declaring and Using CSS Variable" with Guil

:root{ --color-primary: #278da4; --color-secondary:#b13c69; --color-bg: #3acec2; --color-bg-light: #009fe1;

--gradient: var(--color-bg-light), var(--color-bg);

--font-stack-primary: 'Raleway', sans-serif; --font-stack-secondary: 'Bree Serif', serif;

--max-width: 1000px; --gutter: 12px; }

html{ --color-primary: #278da4; --color-secondary:#b13c69; --color-bg: #3acec2; --color-bg-light: #009fe1;

--gradient: var(--color-bg-light), var(--color-bg);

--font-stack-primary: 'Raleway', sans-serif; --font-stack-secondary: 'Bree Serif', serif;

--max-width: 1000px; --gutter: 12px; }

1 Answer

Steven Parker
Steven Parker
231,269 Points

You can use any rule that applies to any ancestor of the element(s) that the variables will be used in. The reason that ":root" is suggested is because it is the ancestor of all page elements.

Using "html" is equavalent to ":root", so for practial purposes the effect will be identical.