Is it a good idea to style the body tag?
In my fixed-width website, I have a layout similar to this:
<div id="wrapper">
<header>...</header>
<div id="content">...</div>
<footer>...</footer>
</div>
And my CSS is similar to this:
#wrapper {
width: 700px;
m开发者_JAVA百科argin-left: auto;
margin-right: auto;
}
Styling the <body>
tag the same way also works, and it makes my code more readable, as I have less nested <div>
tags.
Is applying margins and a fixed with to the <body>
a safe idea?
It is perfectly safe, body
is an HTML element like all the others.
For your layout, you might still want to use #wrapper
(seems like a fixed width layout), because you might want to set a background for body
(or do something with the visible "empty" space).
I'm testing the width/margin/padding on these elements in IETester now with XHTML Strict doctype.
So far I have the following for <body>
:
IE 5.5 - FAIL (won't set width, or background expands beyond dimensions), padding/margin OK
IE 6 - PASS
IE 7 - PASS
IE 8 - Honors width but didn't handle padding/margin perfectly
Page I'm using here
Testing with width and margin applied to html element here:
IE 5.5 - SAME, except margin/padding won't work
IE 6 - Will not honor width
IE 7 - Honors width but Will not center with display:block
and margin:0 auto
with width set in px.
IE 8 - Honors width but didn't handle padding/margin perfectly
When width was set to the html element in all cases, it actually rendered as if it was padding, by constraining the body element.
This was fun but I'm outta gas with this now, time to get back to actual work ;)
Styling the body tag is fine but it does mean that should you ever need to make a change on a website you have no other 'spare' tags to modify. The body tag is often a safe tag to leave to facilitate future changes.
To jcomeau_ictx: There are a million reasons to use fixed width websites. I think you'll find more fixed width websites than fluid websites out there.
精彩评论