1

I have used

html, body {
 background-image: something;
}

in my CSS for many years. Now I can't remember why really..

Is that method even necessary/beneficial or is body {} enough?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
mowgli
  • 2,796
  • 3
  • 31
  • 68

1 Answers1

8

If you're applying a background, selecting body is usually enough; the browser will know to paint the background over the entire viewport regardless of the body dimensions. The details of how a browser is expected to behave are covered in the spec (CSS2.1, CSS3 module). Even the CSS2.1 spec recommends it (although it doesn't mandate it for reasons mentioned later in the prose):

For HTML documents, however, we recommend that authors specify the background for the BODY element rather than the HTML element.

The only situation in which you would apply a background to both elements is when you want to layer one background over a different one; you could either use the CSS3 syntax of layered backgrounds on just the body element, or have body's background overlay that of html. Even then, the only reason I can think of for choosing the latter is if you need compatibility with older browsers that don't support the CSS3 syntax. I cover both methods in-depth in my answer to this related question.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356