开发者

CSS - Div padding "all except z pixels"

开发者 https://www.devze.com 2023-04-09 17:21 出处:网络
I have a div that needs to stretch the whole way across the page, but the actual content should only be z pixels wide, and centered. That is, the text is not centered, but the area where the content i

I have a div that needs to stretch the whole way across the page, but the actual content should only be z pixels wide, and centered. That is, the text is not centered, but the area where the content is - the interior of the div, if you will - is centered.

The logical approach is to do something like this:

<div>
<!--I stretch across the entire page!-->
 <div>
 <!--I am z pixels wide, and my margins are auto. Content goes here.-->
 </div>
</div>

The only problem with this is that it seems really div-itis-y. This is something that should be ab开发者_如何学编程le to be achieved using the box model.

Is there anyway to do this without adding divs?


body already stretches the width of the page, so just use that one interior div

something like

<body>
    <div></div>
</body>

and

div{
    width:980px;
    margin:0 auto;
}

This is what divs are meant to do.

Now, depending on the content and whether or not you are using HTML5, you may want to wrap it all in another element tag, such as header, nav, section etc.

But there is nothing inherently wrong with using divs, even nested ones.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号