How are these three rules different when applied to the same HTML document?
html {
color: black;
background-color: white;
}
body {
color: black;
background-color: white;
}
* {
color: black;
background-color: white;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This rule applies the colors to the
htmlelement. All descendants of thehtmlelement inherit itscolor(but notbackground-color), includingbody. Thebodyelement has no default background color, meaning it’s transparent, sohtml‘s background will show through until and unless you set a background forbody.Although the background of
htmlis painted over the entire viewport, thehtmlelement itself does not span the entire height of the viewport automatically; the background is simply propagated to the viewport. See this answer for details.This rule applies the colors to the
bodyelement. All descendants of thebodyelement inherit itscolor.Similarly to how the background of
htmlis propagated to the viewport automatically, the background ofbodywill be propagated tohtmlautomatically, until and unless you set a background forhtmlas well. See this answer for an explanation. Because of this, if you only need one background (in usual circumstances), whether you use the first rule or the second rule won’t make any real difference.You can, however, combine background styles for
htmlandbodywith other tricks to get some nifty background effects, like I’ve done here. See the above linked answer for how.This rule applies the colors to every element, so neither of the two properties is implicitly inherited. But you can easily override this rule with anything else, including either of the above two rules, as
*has literally no significance in selector specificity.Because this breaks the inheritance chain completely for any property that is normally inherited such as
color, setting those properties in a*rule is considered bad practice unless you have a very good reason to break inheritance this way (most use cases that involve breaking inheritance require you to do it for just one element, not all of them).