Difference in applying CSS to html, body, and the universal selector *?
Asked Answered
O

1

51

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;
}
Ocrea answered 25/8, 2011 at 8:54 Comment(3)
same except no mention of * (which is "trivial"): #4566442Schoolmistress
same on programmers asked later: programmers.stackexchange.com/questions/178049/…Schoolmistress
related: https://mcmap.net/q/121988/-why-doesn-39-t-background-color-on-body-work-duplicate/3597276Broadway
S
51
  1. html {
        color: black;
        background-color: white;
    }
    

    This rule applies the colors to the html element. All descendants of the html element inherit its color (but not background-color), including body. The body element has no default background color, meaning it's transparent, so html's background will show through until and unless you set a background for body.

    Although the background of html is painted over the entire viewport, the html element itself does not span the entire height of the viewport automatically; the background is simply propagated to the viewport. See this answer for details.

  2. body {
        color: black;
        background-color: white;
    }
    

    This rule applies the colors to the body element. All descendants of the body element inherit its color.

    Similarly to how the background of html is propagated to the viewport automatically, the background of body will be propagated to html automatically, until and unless you set a background for html as 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 html and body with other tricks to get some nifty background effects, like I've done here. See the above linked answer for how.

  3. * {
        color: black;
        background-color: white;
    }
    

    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).

Steiger answered 25/8, 2011 at 8:55 Comment(5)
Note that by default, the body also usually has a small margin, which html does not.Separator
Which type of element is <body>? Block Level Or inline?Resupinate
@SteveJorgensen, it's actually a small padding; otherwise, the background wouldn't expand to the whole page.Jeanejeanelle
@zneak: Actually, it is a margin. The body element doesn't really "expand" to cover the whole page; rather, the html element uses the body element's background if its own background color is computed as transparent and/or its background image is computed as none. This is described in the spec. This means if you take a default page and set different backgrounds for html and body, you'll notice that the body is in fact being offset by a margin rather than padding its content.Steiger
@zneak: You can also apply a background and a border to body alone to see the margin: jsfiddle.net/BoltClock/vddbhSteiger

© 2022 - 2024 — McMap. All rights reserved.