如何使 div 假定 100% 包含它的 div?
我试图让一个 div 假设 100% 是它的父 div。父 div 充当页面包装器,因此它已经假定了 100% 的页面宽度。我尝试添加 width: 100%
,但这似乎不起作用。我有点困惑,因为这似乎是一件相对简单的事情。
I'm trying to make a div assume 100% of it's parent div. The parent div acts as a page wrapper, so it's already assuming 100% of the page width. I've tried adding width: 100%
, but this did not seem to work. I'm a little baffled, because this seems like a relatively simply thing to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根本不指定宽度。对于
div
元素(或任何与此相关的块级元素),这将使其假定 100% 宽度,无论它设置了什么填充/边距设置。根据盒子模型,显式设置 100% 宽度实际上会使元素太宽,因为填充是计算在内的。
如果这不起作用,则可能存在其他一些 CSS 设置干扰,并且您需要显示更多布局和 HTML 代码。
Don't specify a width at all. For a
div
element (or any block level element for that matter), this will make it assume 100% width regardless what padding/margin settings it has set.Depending on the box model, explicitly setting 100% width can actually make the element too wide because paddings are calculated into it.
If this doesn't work, there is some other CSS setting interfering and you need to show more of your layout and HTML code.
应该为你工作。
Should work for you.
您需要像平常一样显示更多现有的 css 代码,div 默认情况下占用整个可用空间,前提是它有一些内容。
除此之外,请确保将父级 div 的边距和填充设置为 0。
You need to show more of your existing css code as normally, a div takes by default the whole space available to it, provided it has some content.
Other than that, make sure you set margin and padding of the parent div to 0.