tags:

views:

199

answers:

1

Can we give width and border to <body> and use in place of Container div? see this example

see source code of this file and code of file is also perfectly W3C valid. and looking same in IE 7 and firefox 3.5.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">
  <head>
    <title> Width in body</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <style type="text/css">
      html { background-color: #00f; }
     body{background: #cd5c5c;width:800px;height:400px;border:1px solid;color: #fff;margin:0 auto;}
     </style>
  </head>
   <body>
     <p>Hello world!</p>

   </body>
</html>
+4  A: 

Your example answers your question! Body is a block element like any other. It has width, height, padding, margin and border properties.

  • Note that it is essential that the page is rendered in strict, rather than quirks, mode to be able to treat the body element as a block-level element; otherwise it is treated as the documentElement and all bets are off. – NickFitz

Then can we use body in place of #container div? – Jitendra

  • Nice theory. Shame about Microsoft. – David Dorward
  • @Jitendra: you can, but be thorough with your cross-browser testing, particularly when it comes to scrolling :-) – NickFitz

What is the difference between viewport and body? – Jitendra

  • the viewport is the visible area of the browser window which displays the document. The body is an element in the document. In quirks mode, the body will also be treated as the documentElement: that is, the root node of the document, which will fill the window, and if necessary will be able to be scrolled. In strict mode, the html element will be treated as the documentElement, and the body will be a child of that. As an experiment, change your test page by adding the style rule html { background-color: #00f; } - you will see that the html element contains the `body' – NickFitz
deau
then can we use body in place of #container div?
metal-gear-solid
Nice theory. Shame about Microsoft.
David Dorward
Note that it is essential that the page is rendered in strict, rather than quirks, mode to be able to treat the `body` element as a block-level element; otherwise it is treated as the `documentElement` and all bets are off.
NickFitz
What is the difference between viewport and body?
metal-gear-solid
@NickFitz thx I didn't know that!
deau
@Jitendra: the viewport is the visible area of the browser window which displays the document. The `body` is an element in the document. In quirks mode, the `body` will also be treated as the `documentElement`: that is, the root node of the document, which will fill the window, and if necessary will be able to be scrolled. In strict mode, the `html` element will be treated as the `documentElement`, and the `body` will be a child of that. As an experiment, change your test page by adding the style rule `html { background-color: #00f; }` - you will see that the `html` element contains the `body'
NickFitz
@NickFitz thx for clarification. i updated my sample page. then in xhtml strict html is documentElement so can we use body as container in place of #container div
metal-gear-solid
@Jitendra: you can, but be thorough with your cross-browser testing, particularly when it comes to scrolling :-)
NickFitz
@NickFitz you mean in xhtml strict div and body are same in beh?aviour
metal-gear-solid
I found more knowledge from here http://www.sitepoint.com/blogs/2009/02/11/styling-the-html-and-body-elements/
metal-gear-solid