[英]Children element not stretch parent container
Children element not stretch parent container. 子元素不能拉伸父容器。
My code: 我的代码:
html: 的HTML:
<div class='window'>
<div class='wrapper'>
<div class='short'>short</div>
<div class='long'>long</div>
</div>
</div>
css: CSS:
.window{
width: 500px;
height: 100px;
overflow: auto;
background: gray;
}
.wrapper{
background: pink;
border: 1px solid red;
}
.long{
width: 700px;
background: beige;
}
I want .long
stretch his parent .wrapper
. 我想
.long
舒展他的父母.wrapper
。 .wrapper
width must be the same as .long
(700px). .wrapper
宽度必须与.long
(700px)相同。
I can reach this by setting float: left
to .wrapper
. 我可以通过将
float: left
设置为.wrapper
来.wrapper
。
But what happens here i don't understand, why it helps? 但是我不明白这里会发生什么,为什么有帮助? What is the mechanism of such behavior?
这种行为的机理是什么? I need explanation, with links to w3c documentation.
我需要解释,并带有指向w3c文档的链接。
What else can i do to extend .wrapper
width? 我还能做些什么来扩展
.wrapper
宽度?
By default, the .wrapper
div is inheriting the fixed width you set on .window
. 默认情况下,
.wrapper
div继承您在.window
设置的固定宽度。 You can float the .wrapper
and set it's width to auto
so the width expands without restriction to the parent. 您可以浮动
.wrapper
并将其宽度设置为auto
以便宽度扩展而不受父级限制。
CSS: CSS:
.wrapper {
float: left;
width: auto;
}
Example: http://jsfiddle.net/WTGAc/3/ 示例: http : //jsfiddle.net/WTGAc/3/
Theory: 理论:
By default, the dimensions of wrapper
are constained to the dimensions placed on it's parent, .window
. 默认情况下,
wrapper
的尺寸.window
放置在其父级.window
上的尺寸。
Floated elements still live within the parameters defined by their parent element, ie the width available and horizontal position.
浮动元素仍然位于其父元素定义的参数之内,即可用宽度和水平位置。 They still interact with text and other elements inside that parent element (or other following elements).
它们仍与文本和该父元素(或其他后续元素)中的其他元素交互。 In that respect, they are quite different from absolutely positioned elements, which are removed from the document flow and don't interact with other elements ... but even then, if they have a positioned ancestor then they are restricted by the envelope of that ancestor and will use that as the basis for calculating size and dimension (although they can still be made to extend or exist outside that positioned ancestor).
在这方面,它们与绝对定位的元素有很大的不同,绝对定位的元素已从文档流中删除并且不与其他元素交互……但是即使如此,如果它们具有定位的祖先,那么它们将受到该信封的限制。并将其用作计算大小和尺寸的基础(尽管仍然可以使它们扩展或存在于所定位的祖先之外)。
Since the element is floated and set outside of the normal document flow , it can now expand to the true width of the parent, instead of the fixed width initially defined. 由于该元素是浮动的,并且设置在普通文档流之外 ,因此它现在可以扩展到父级的真实宽度,而不是最初定义的固定宽度。
In you example, you have the following: 在您的示例中,您具有以下内容:
<div class='window'>
<div class='wrapper'>
<div class='short'>short</div>
<div class='long'>long</div>
</div>
</div>
In the simplest case, .window
is the containing block with a fixed width (500px). 在最简单的情况下,
.window
是具有固定宽度(500px)的包含块。 The child element .wrapper
inherits the width from .window
. 子元素
.wrapper
继承的宽度.window
。 The .long
element has a width of 700px and it will trigger an overflow condition. .long
元素的宽度为700px,它将触发溢出条件。 Since .window
has overflow: auto
declared, the .window
element will generate a horizontal scroll bar. 由于
.window
发生overflow: auto
声明,因此.window
元素将生成水平滚动条。 Note that by using overflow: auto
, .window
establishes a new block formatting context, which is why the horizontal scroll bar appears on .window
instead of the viewport/root element. 请注意,通过使用
overflow: auto
, .window
建立了新的块格式化上下文,这就是为什么水平滚动条显示在.window
而不是视口/根元素的原因。
When you float .wrapper
to the left, the .wrapper
element defines an additional block formatting context. 当您将
.wrapper
浮动到左侧时, .wrapper
元素将定义其他块格式化上下文。 A new block formatting context will ignore the width inherited from its containing block and computes a new width sufficient to enclose the content (shrink-to-fit), which is why the pink background from .wrapper
now extends the entire 700px in width. 新的块格式化上下文将忽略从其包含的块继承的宽度,并计算足以包围内容的新宽度(缩小以适合),这就是为什么
.wrapper
的粉红色背景现在将整个宽度扩展到700 .wrapper
原因。
You can trigger the same effect by adding position: relative
to .window
and position: absolute
to .wrapper
. 您可以触发通过增加相同的效果
position: relative
以.window
和position: absolute
给.wrapper
。 However, this works because you set the height to .window
, otherwise, the .window
height would compute to zero since absolute elements are out of the flow and (unlike floats) will no longer affect how the .window
content is laid out (not contribute to the height in this case). 但是,这是
.window
,因为您将高度设置为.window
,否则,由于绝对元素不在流中,因此.window
高度将计算为零,并且(不同于float)将不再影响.window
内容的布局方式(不是在这种情况下有助于高度)。
As an aside, instead of using float: left
on .wrapper
, you can also try overflow: auto
which will also establish a new block formatting context but this time the scrolling bar appears on .wrapper
instead of .window
. .wrapper
,除了使用.wrapper
上的float: left
.wrapper
,您还可以尝试overflow: auto
,这也会建立一个新的块格式上下文,但是这次滚动条出现在.wrapper
而不是.window
。
The relevant documentation from w3.org is: 来自w3.org的相关文档是:
10 Visual formatting model details
10视觉格式化模型详细信息
10.3 Calculating widths and margins10.3计算宽度和边距
10.3.5 Floating, non-replaced elements10.3.5浮动的不可替换元素
Link: http://www.w3.org/TR/CSS2/visudet.html#float-width 链接: http : //www.w3.org/TR/CSS2/visudet.html#float-width
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.