带包装的行弹性框内的嵌套列弹性框 [英] Nested column flexbox inside row flexbox with wrapping
问题描述
我在此处有一个嵌套了弹性框的页面: http://jsfiddle.net/fr0/6dqLh30d/
I have a page with nested flex boxes here: http://jsfiddle.net/fr0/6dqLh30d/
<div class="flex-group">
<ul class="flex-container red">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
</ul>
<ul class="flex-container gold">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
<ul class="flex-container blue">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
<li class="flex-item">7</li>
<li class="flex-item">8</li>
</ul>
<div>
以及相关的CSS:
.flex-group {
display: flex;
flex-direction: row;
height: 500px;
}
.flex-container {
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
display: flex;
flex-direction: column;
flex-wrap: wrap;
flex: 0 0 auto;
}
.flex-item {
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
外部flexbox(.flex-group
)用于从左到右布局.内部flexbox(.flex-container
)用于从上到下进行布局,如果没有足够的空间(我的jsfiddle中没有),则应该进行包装.我想发生的是,发生换行时.flex-container
会在X方向上增长.但这没有发生.相反,容器正在溢出.
The outer flexbox (.flex-group
) is meant to lay out from left-to-right. The inner flexboxes (.flex-container
) are meant to layout from top-to-bottom, and should wrap if there isn't enough space (which there isn't in my jsfiddle). What I want to happen, is that the .flex-container
s will grow in the X direction when the wrapping happens. But that's not occurring. Instead, the containers are overflowing.
我希望它的外观(在Chrome中): https://dl.dropboxusercontent.com/u/57880242/flex-good.png
What I want it to look like (in Chrome): https://dl.dropboxusercontent.com/u/57880242/flex-good.png
是否有一种方法可以使.flex-container
在X方向上适当调整大小?我不想对它们的宽度进行硬编码,因为这些列表中显示的项目将是动态的.
Is there a way to get the .flex-container
s to size appropriately in the X direction? I don't want to hard-code their widths, since the items that appear in these lists will be dynamic.
推荐答案
我已经玩了几分钟,我想我已经找到了您想要的东西.
I've been playing with this for a few minutes now, and I think I've got what you're looking for.
这是CSS
.flex-group {
margin: auto;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.flex-container {
flex: 0 1 auto;
display: flex;
flex-flow: row wrap;
align-items: space-around;
align-content: flex-start;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
.red li {
background: red;
}
.gold li {
background: gold;
}
.blue li {
background: deepskyblue;
}
.flex-item {
flex: 0 1 auto;
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
这是一个更新了小提琴,以了解它的作用.
And here's an updated fiddle to see it in action.
这篇关于带包装的行弹性框内的嵌套列弹性框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!