Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,900 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,085 Points

So in a HTML document,

html {
  background: white;
}

and

:root {
  background: white;
}

will do the same thing to the webpage.