CSS:宽度如何以百分比表示?
我遇到了一个我认为很奇怪的问题。我有一个 div,位于父 div 内,并且我给子 div 的宽度为 100%,但它没有增长到父 div 的大小。
父 div 没有有任何类型的设置宽度。所以我的问题是:百分比宽度仅在父元素具有设定宽度时才起作用,还是应该增长?
澄清:
有些人可能想知道如果父 div 本身没有设定宽度,它的宽度是如何增长到的。原因是我在父 div 内有子元素的其他同级元素,并且它们具有设定的宽度,因此父 div 已经增长以满足这些同级宽度。
代码示例:
<div id="parent-div">
<div id="child-element" style="width: 100%">Content</div>
<div id="sibling" style="width: 250px"></div>
</div>
子元素的增长无法满足父级 div 的要求。据我所知,100% 的宽度基本上没有做任何事情。这是在 IE7 中。
谢谢。
后续:感谢大家的回答。我正忙着测试。我最初认为父级 div 的宽度只会与子级一样宽,但事实证明我错了,考虑到上面的示例,我编写该示例只是为了演示我遇到的问题。就我而言,我的父 div 应用了 position:fixed
和 bottom:1px
和 right:1px
。根据我的测试,这似乎改变了父 div 的行为。它不再自动拉伸到页面的整个宽度,而是假设我认为的行为无论如何都是如此,即父 div 扩展仅足以容纳其最宽的子元素。这就是我现在看到的行为,但这只是因为我的父 div 有一个固定的位置。
I'm having what I think is a weird problem. I have one div, inside a parent div, and I'm giving the child div a width of 100%, but it's not growing to the size of the parent div.
The parent div does not have a set width of any kind. So my question: does width in percentage only work when a parent element has a set width, or should it grow anyway?
CLARIFICATION:
Some may be wondering how the parent div has a width to grow to at all, if it itself does not have a set width. The reason is I have other siblings of the child element inside the parent div, with a set width to them, so the parent div has grown to meet those sibling widths.
CODE SAMPLE:
<div id="parent-div">
<div id="child-element" style="width: 100%">Content</div>
<div id="sibling" style="width: 250px"></div>
</div>
Child element is not growing to meet the parent div. The width of 100% is essentially not doing anything at all that I can tell. This is in IE7.
Thanks.
FOLLOW-UP: Thanks everyone for the answers. I'm busy testing on my end. I originally thought that parent div's only grow as wide as their children, but it turns out I was wrong given my example above, which I coded only to demonstrate my issue I'm having. In my case, my parent div has a position: fixed
and bottom: 1px
and right: 1px
applied to it. From my tests, this appears to change the behavior of the parent div. No longer does it automatically stretch to the entire width of the page, but assumes the behavior I thought was the case anyway, which is the parent div expands only enough to accommodate its widest child. So that's the behavior I'm seeing now, but only because my parent div has a fixed position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在没有看到您的 CSS 和 HTML 的其余部分的情况下,我将假设您发布的内容就是您的 HTML 和 CSS 中的全部内容。
在这种情况下:
如果不是这种情况,则您还有其他 HTML 或 CSS干扰。
Without having seen the rest of your CSS and HTML, I'm going to assume that what you've posted is all there is in your HTML and CSS.
In that case:
If this is not the case, you've got other HTML or CSS interfering.
此屏幕截图解释了如何使用 CSS 测量指标。对不起,法国人,这是我电脑上的。
内边距、边距和边框 http://img524.imageshack.us/img524/5211/capturedcran20100204173 .png
其中“marge”表示
边距
,“bordure”表示边框
,“espacement”表示填充
。Margin
落在border
周围,而border
又落在padding
周围,而padding
又落在实际元素大小周围。在此示例中(通过检查堆栈溢出答案从嵌套元素中,您只能占用实际元素大小。父级上的
Padding
将导致嵌套项目保留边距。这意味着嵌套边界与其父级边界之间可能仍然存在空间。如果您希望嵌套的
占据其所能占用的所有空间,则必须将其
margin
属性设置为0px
,并且 <其父级的 code>padding 属性也设置为0px
。This screenshot explains how metrics are measured with CSS. Excuse the French, it's from my computer.
padding, margin and border http://img524.imageshack.us/img524/5211/capturedcran20100204173.png
Where "marge" means
margin
, "bordure" meansborder
and "espacement" meanspadding
.Margin
falls around theborder
, which in turn falls around thepadding
, which in turns falls around the actual element size. In this example (which was taken by inspecting the stack overflow answer<textarea>
tag) shows that there is no margin, the border is 1px wide, and there is a 3px padding between the border of the<textarea>
and its contents; and the size the content can use, excluding the padding, is 660x200. This size matches the CSSwidth
andheight
properties.From a nested element, you can only occupy the actual element size.
Padding
on the parent will cause your nested items to keep a margin. That means there may still be space between the border of your nested and the border of its parent.If you want your nested
<div>
to take all the room it can, you must set itsmargin
property to0px
, and thepadding
property of its parent to0px
as well.这实际上是一个CSS技巧。它是这样的:
如果将父 div 位置设置为相对,子 div 位置设置为绝对,则子 div 将保留在父 div 内,即使它们的位置是绝对的。
请参阅:相对定位内的绝对定位了解更多说明。
谢谢
This is actually a css trick. It goes like this:
If you set the parent div position to relative and the child divs position to absolute then child divs will remain inside the parent div even though their position is absolute.
See: Absolute Positioning Inside Relative Positioning for more explanation.
Thanks
正如 zneak 指出的,不要忘记重置 css 盒子模型;请参阅这篇关于理解盒模型的精彩文章。
只是要指出,不要忘记设置,
因为您可能已将其设置为内联,它不会扩展到父级宽度的 100%。
我无法复制你的问题,一切都很好。
As zneak pointed out, don't forget to reset the css box model; see this great article on understanding the box model.
Just to point out, don't forget to set
as you may have set it to inline an it won't expand to 100% of parent's width.
I couldn't replicate your problem, everything worked just fine.