CSS - Div 填充“除 z 像素外的所有像素”
我有一个 div 需要在整个页面上拉伸,但实际内容应该只有 z 像素宽并居中。也就是说,文本不居中,但内容所在的区域(如果您愿意的话,是 div 的内部)居中。
合乎逻辑的方法是做这样的事情:
<div>
<!--I stretch across the entire page!-->
<div>
<!--I am z pixels wide, and my margins are auto. Content goes here.-->
</div>
</div>
这样做的唯一问题是它看起来真的很神圣。这是使用盒模型应该能够实现的。
有没有办法在不添加 div 的情况下做到这一点?
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 able to be achieved using the box model.
Is there anyway to do this without adding divs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
body
已经拉伸了页面的宽度,因此只需使用一个内部div
即可
,
这就是
div
的用途。现在,根据内容以及您是否使用 HTML5,您可能希望将其全部包装在另一个元素标记中,例如
header
、nav
、的。
但是使用
div
并没有本质上的错误,即使是嵌套body
already stretches the width of the page, so just use that one interiordiv
something like
and
This is what
div
s 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
div
s, even nested ones.