【发布时间】:2018-09-24 17:03:58
【问题描述】:
我的弹性容器奇怪地(在我看来)忽略了max-width 和wrap。
目的是在左边有一个(始终如一)大的项目,在那个项目的旁边或下方有零个、一个、两个或三个较小的项目。如果只有一个,它应该显示在右边,如果有两个或三个,它们应该都到下一行(等距)。
0) -------------------------------
| large item |
-------------------------------
1) -------------------------------
| large item | small |
-------------------------------
2) -------------------------------
| large item |
-------------------------------
| small | small |
-------------------------------
3) -------------------------------
| large item |
-------------------------------
| small | small | small |
-------------------------------
我认为 flexbox 将“只需要这样做”。但现在它不会按预期包装。右边的项目(应该在第二行)刚好溢出弹性框。
谁能看到我做错了什么?非常感谢!
这里有一个例子:Codepen
.container {
display: flex;
flex-wrap: wrap; /* ? */
background-color: #ccc;
padding: 10px 0;
max-width: 1024px;
margin: 0 auto;
}
.item_large {
flex: 1 0 450px;
background-color: green;
height: 40px;
border-radius: 2px;
}
.container_2 {
display: flex;
flex-wrap: nowrap;
flex: 1 0 190px;
margin: 0 -12px;
}
.item_small {
flex: 1 0 190px;
background-color: blue;
height: 40px;
border-radius: 2px;
margin: 0 12px;
}
@media screen and (max-width: 719px) {
.container_2 {
flex-wrap: wrap;
}
.item_small {
flex: 1 0 320px;
}
}
@media screen and (min-width: 720px) {
.item_small:first-child:nth-last-child(1) {
margin-left: 36px;
}
}
<div class="container">
<div class="item_large"></div>
</div>
</div><br /><br />
<div class="container">
<div class="item_large"></div>
<div class="container_2">
<div class="item_small"></div>
</div>
</div><br /><br />
<div class="container">
<div class="item_large"></div>
<div class="container_2">
<div class="item_small"></div><div class="item_small"></div>
</div>
</div><br /><br />
<div class="container">
<div class="item_large"></div>
<div class="container_2">
<div class="item_small"></div><div class="item_small"></div><div class="item_small"></div>
</div>
</div>
【问题讨论】:
-
它们并没有溢出主容器,但是你有嵌套容器和内部容器
nowrap -
是的,内部容器不应该换行(它的内容),因为如果里面只有一个元素,它应该保留在第一行,如果有多个,它们会溢出主容器,因此在下面的行上对齐(展开)。你是什么意思(@temani-afif):它们没有溢出?我确实认为他们是(?)
-
检查控制台,你会看到元素溢出内部容器......并且主容器没有溢出,所以问题出在内部容器
-
对不起,我不明白:/“主要”容器(有两个孩子)为什么不会被太大的第二个孩子溢出?
-
更新:通过一点“作弊”,我们可能(经过更多工作以使利润表现良好)到达那里:updated example from above。非常感谢@Temani Afif 将我推向正确的方向!