Difference Between Body and * in CSS

difference between body and * in css

The body selector has higher priority, but the * selector applies more broadly, so in <body>foo<p>bar</p></body> the body selector will determine the background of the text foo, but the * selector will determine the background of the <p> element.

Note, also that many browsers create an element around the <body> that includes its margins and scrollbars, so the * selector may also determine the color of that region.

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



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

What is the difference between applying css rules to html compared to body?

There is no real difference (if you're just talking about where to apply background, otherwise BoltClock's answer to this other question is a better fit). html is an element, just like body is.

Both are valid choices, and both will both work in all common browsers.

The YUI Reset for instance, chooses to set a background on the html element instead of body:

http://yui.yahooapis.com/3.3.0/build/cssreset/reset.css

This requires that you set your background on html, for instance see: can't change body background color using CSS reset

See: http://dev.w3.org/csswg/css3-background/#special-backgrounds

The background of the root element becomes the background of the
canvas and its background painting area extends to cover the entire
canvas, although any images are sized and positioned relative to the
root element as if they were painted for that element alone. (In other
words, the background positioning area is determined as for the root
element.) If the root's ‘background-color’ value is ‘transparent’, the
canvas's background color is UA dependent. The root element does not
paint this background again, i.e., the used value of its background is
transparent.

And:

For documents whose root element is an HTML HTML element [HTML401] or
an XHTML html element [XHTML11]: if the computed value of
‘background-image’ on the root element is ‘none’ and its
‘background-color’ is ‘transparent’, user agents must instead
propagate the computed values of the background properties from that
element's first HTML BODY or XHTML body child element. The used values
of that BODY element's background properties are their initial values,
and the propagated values are treated as if they were specified on the
root element. It is recommended that authors of HTML documents specify
the canvas background for the BODY element rather than the HTML
element.

What that wall of text is saying is demonstrated here:

  • background on just body: http://jsfiddle.net/hhtzE/
  • background on html and body: http://jsfiddle.net/hhtzE/1/
  • background only html: http://jsfiddle.net/hhtzE/2/

Why We Use * Selector Instead Of Body Element

* means a property/setting is applied to all elements, whereas not all elements necessarily inherit all properties/settings from body

What's the difference between CSS3's :root pseudo class and html?

From the W3C wiki:

The :root pseudo-class represents an element that is the root of the document. In HTML, this is always the HTML element.

CSS is a general purpose styling language. It can be used with other document types, not only with HTML, it can be used with SVG for example.

From the specification (emphasis mine):

This specification defines Cascading Style Sheets, level 2 revision 1 (CSS 2.1). CSS 2.1 is a style sheet language that allows authors and users to attach style (e.g., fonts and spacing) to structured documents (e.g., HTML documents and XML applications).

What is the difference between body style= background-color:red; and body bgcolor= red ?

Bgcolor is a HTML attribute and is not supported by HTML5. As of now all the latest browsers support <body bgcolor="color"> but it may change in future.

<body style="background:red">
is CSS way of applying background and is better way to do it.



Related Topics



Leave a reply



Submit