tags:

views:

1026

answers:

5

I am currently learning html/css, and have noticed a common technique is to place a generic container div in the root of the body tag:

<html>
  <head>
    ...
  </head>
  <body>
    <div id="container">
      ...
    </div>
  </body>
</html>

Is there a valid reason for doing this? Why can't the css just reference the body tag?

+3  A: 

THis method allows you to have more flexibility of styling your entire content. Effectivly creating two containers that you can style. THe HTML Body tag which serves as your background, and the div with an id of container which contains your content.

This then allows you to position your content within the page, while styling a background or other effects without issue. THink of it as a "Frame" for the content.

Mitchel Sellers
+16  A: 

The container div, and sometimes content div, are almost always used to allow for more sophisticated CSS styling. The body tag is special in some ways. Browsers don't treat it like a normal div; its position and dimensions are tied to the browser window.

But a container div is just a div and you can style it with margins and borders. You can give it a fixed width, and you can center it with margin-left: auto; margin-right: auto.

Plus, content, like a copyright notice for example, can go on the outside of the container div, but it can't go on the outside of the body, allowing for content on the outside of a border.

Jonathan Tran
A: 

The most common reasons for me are so that:

  1. The layout can have a fixed width (yes, I know, I do a lot of work for designers who love fixed widths), and
  2. So the layout can be centered by applying text-align: center to the body and then margin: auto to the left and right of the container div.
da5id
ain't nothin wrong with fixed widths, IMO.
nickf
just preempting the haters...
da5id
Container is need for fixed "percentage" widths too. (essentially a fluid width)
Atømix
2 isn't really relevant these days, you only need it for IE 5.5 and earlier and most people don't support IE that old.
David Dorward
A: 

This is one of the biggest bad habits perpetrated by front end coders.

All the answers above me are wrong. The body does take a width, margins, borders, etc and should act as your initial container. The html element should act as your background "canvas" as it was intended. In dozens of sites I've done I've only had to use a container div once.

I'd be willing to be that these same coders using container divs are also littering their markup with divs inside of divs--everywhere else.

Dont' do it. Use divs sparingly and aim for lean markup.

Troy Dalmasso
Can we see one of these sites?
Joe Holloway
I was about to point to his website, but that uses a container and nested divs.
chrisbtoo
Yeah, that site has `body > div#BgContainer > div#Container` which seems to contradict, but maybe that's the one time out of dozens.P.S. I didn't downvote, I'm honestly curious to see one of these purely semantic sites I keep hearing about.
Joe Holloway
A: 

Certain browsers (<cough> Internet Explorer) don't support certain properties on the body, notably width and max-width.

David Dorward