tags:

views:

67

answers:

6

When I declare some base styles for my site I have used to do that on the body tag. Like for example

body {
  font-size: medium;
  line-height: 1.3em;
}

But I have also seen people do things like that on the html tag. And on both. Where should it be done? Should some be at one and some at the other? Should all be on one of them? Or does it simply not matter at all? Or?

+2  A: 

I'd add styling on the body tag as it makes more semantic sense (you're not going to style the head, title and so on).

Also I don't see a lot of people adding styles directly on the html tag anymore except to reset some default styles...

marcgg
Actually, you can style `title` (for instance) to be visible inside the viewport. By default, the `head` element and its descendants are not shown, but that is overridable. `html` is the viewport's root, not `body`. (Not that anyone actually uses this in the wild, of course.)
janmoesen
@janmoesen Shame on you — I still use CSS to display the `HEAD` element’s contents on my totally outdated site! ;)
Mathias Bynens
+1  A: 

For the strict doctype, only body is necessary. However if the browser is in quirks mode, you'll very likely need to target table cells as well.

In both cases you may want to also target form elements, since they generally inherit the platform default.

DisgruntledGoat
+1  A: 

I would either set to body itself. I tend to do that or a use a base div style, depends on what I'm doing, but putting it on the html object seems unintuitive.

Jason M
+1  A: 

html is the container for body, so the latter will inherit from the former. Be careful when mixing:

html, body { font-size: 80%; } will make your body's font size to be 80% of 80%.

I always go for html, but there is an issue with ancient browser support and/or quirks mode.

janmoesen
You always go for something knowing that it leads to an issue ? How come?
marcgg
In what browsers?
janmoesen
+1  A: 

From my personal experience, the only situation where putting certain base styles on both html and body is necessary is when you're doing some funky hacks that rely on 100% width or height ("sticky" divs or some such). In all other situations, it is perfectly OK to declare the base styles only on body. In other words,

html, body {height:100%}

might actually be necessary, but

html, body {font-family:Arial}

certainly won't. After all, all the elements on which you'll need the font-family will be children of body anyway, so there's no point in specifying it for html, too.

RegDwight
+1  A: 

I like applying base declarations to html. Most of the time, I use body as a container, like so:

html {
 /* Base styles go here */
 font: 14px/1.5 Arial, sans-serif;
 background: darkgreen;
}

body {
 background: lightgreen;
 padding: 0 20px;
 width: 920px;
 margin: 0 auto;
}

View the demo: http://jsbin.com/atiso3

Most people tend to use additional DIVs just to accomplish this basic behavior. It’s not necessary when you know how to use html and body in CSS ;)

Mathias Bynens
Yeah, I've always used body and a div... but this is sure cleaner, hehe
Svish