[英]Why doesn't a nested flexbox grow to full width?
Consider the following HTML/CSS:考虑以下 HTML/CSS:
<div style="display: flex; background-color: yellow;"> <div style="display: flex;background-color: lightblue;">i am a NESTED flexbox</div> </div>
Note how the nested flexbox (lightblue background color) does not fill the complete width, while the outer flexbox (yellow background) does.请注意嵌套的 flexbox(浅蓝色背景色)不会填充整个宽度,而外部 flexbox(黄色背景)会填充。
Why doesn't a nested flexbox container grow to the full width of the parent?为什么嵌套的 flexbox 容器没有增长到父容器的全宽?
Hint : I know I can fix it with flex: 1
, but the question is about understanding why it behaves like this, not how to fix it提示:我知道我可以用
flex: 1
修复它,但问题是关于理解它为什么会这样,而不是如何修复它
The lightblue div is actually a flex item .浅蓝色 div 实际上是一个弹性项目。 The default properties of flex items are:
弹性项目的默认属性是:
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
So the item won't grow to fill the remaining space, it would shrink, and look at my width or height property for basis.因此,该项目不会增长以填充剩余空间,它会缩小,并查看我的宽度或高度属性作为基础。
Your nested div-block (lightblue background color) only takes up the space (width) it needs.您嵌套的 div 块(浅蓝色背景色)只占用它需要的空间(宽度)。 You can see this by doubling the text of the nested block.
您可以通过将嵌套块的文本加倍来看到这一点。
It's a matter of default behavior and context formatting:这是默认行为和上下文格式的问题:
If you want it to take the full width then you have the choice to add width: 100%;
如果你想让它占据整个宽度,那么你可以选择添加
width: 100%;
or flex:1
in the CSS of the nested div.或
flex:1
在嵌套 div 的 CSS 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.