简体   繁体   English

子元素不能拉伸父容器

[英]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;
}

example

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). 在这方面,它们与绝对定位的元素有很大的不同,绝对定位的元素已从文档流中删除并且不与其他元素交互……但是即使如此,如果它们具有定位的祖先,那么它们将受到该信封的限制。并将其用作计算大小和尺寸的基础(尽管仍然可以使它们扩展或存在于所定位的祖先之外)。

Source of Quote 报价来源

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. 由于该元素是浮动的,并且设置在普通文档流之外 ,因此它现在可以扩展到父级的真实宽度,而不是最初定义的固定宽度。

Widths and the CSS Visual Formatting Model 宽度和CSS视觉格式模型

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.windowposition: 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 margins 10.3计算宽度和边距
10.3.5 Floating, non-replaced elements 10.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.

相关问题 子元素不能以固定高度拉伸父容器 - Children element not stretch parent container with fixed height 如何使元素( <a>)拉伸到其父元素(容器)的整个宽度?</a> - How to make an element (<a>) stretch to the full width of its parent element (container)? 如何拉伸垂直div元素以填充父容器? - How to stretch vertical div element to fill parent container? 如何使子元素位于父元素之后,但不在主容器之上 - How to make a children element be behind parent element, but not over the main container 绝对元素继承父宽度,但我希望它根据其子元素进行拉伸 - Absolute element inherits parent width but I want it to stretch according its children 让 flex 孩子不要伸展(但在绝对父级内部)? - Get flex children not to stretch (but inside an absolute parent)? 如何拉伸父div以适合子div? - How to stretch parent div to fit children div? 仅使用 CSS 让元素“拉伸”过去父容器的填充一直到边缘? - Getting an element to "Stretch" past padding of the parent container all the way to the edges using CSS only? 如何在父容器中自动拉伸并约束HTML子元素? - How to automatically stretch and yet constraint HTML child element within parent container? 如何防止子元素的字体大小更改以在 CSS 中拉伸父容器 - How to prevent font-size change of child element to stretch parent container in CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM