简体   繁体   中英

Is the body wrapping div really necessary?

A popular technique to build a set fixed centered layout is wrap everything in the body in a div and set some CSS rules on it, for example:

<body>
    <div id="wrap">
        all content here
    </div>
</body>

#wrap { width: 850px; margin: 0 auto; }

I was wondering, if there is a reason to add this extra wrapping div, since setting the same CSS rules on the body works. Is it for compatibility with some browsers or just a tradition?

No. You do not need a "wrapper" div that so many use because they saw others do so. Use the body for that as you should. Setting CSS properties on the body was a problem in the past but not any longer.

It might work in some browsers, I don't think it'll work in all of them. Also, it's quite common to set a background on the body, then a separate one on the wrap.

If you need a specific background for the whole page and a centered container, you can't do otherwise. It also provides a way to use "layers" like lightboxes that need to blur the whole #wrap content.

要添加额外的包装,可以使body和div独立更改。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM