Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use a container div in HTML?

Tags:

html

css

I 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?

like image 306
user44810 Avatar asked Dec 10 '08 00:12

user44810


2 Answers

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.

like image 115
Jonathan Tran Avatar answered Oct 27 '22 01:10

Jonathan Tran


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.

like image 44
Mitchel Sellers Avatar answered Oct 27 '22 01:10

Mitchel Sellers