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 CSS Selectors Advanced Selectors :root and :target

Applying background-color to :root vs body

What factors would lead to applying a background color to the body element and not the :root and vice-versa?

1 Answer

Steven Parker
Steven Parker
231,275 Points

If the body had styles applied that caused it to not cover the entire viewport (such as an explicit width or margins), you might want to apply a background to its parent element instead. And for the same reason, you might want to use the body to limit it only to the body area.

Note that for an HTML document, the html selector will target the same thing as :root, though :root has a higher specificity (class vs. tag).

Kailash Seshadri
Kailash Seshadri
3,087 Points

So in a HTML document,

html {
  background: white;
}

and

:root {
  background: white;
}

will do the same thing to the webpage.