Pum*_*jie 7 html css overflow flexbox
我的 Flex 容器奇怪地(在我看来)忽略了max-width
和wrap
。
其目的是在左侧放置一个(始终)大的项目,并在该项目旁边或下方放置零个、一个、两个或三个较小的项目。如果只有一个,它应该显示在右侧,如果有两个或三个,它们应该全部转到下一行(等距)。
0) -------------------------------
| large item |
-------------------------------
1) -------------------------------
| large item | small |
-------------------------------
2) -------------------------------
| large item |
-------------------------------
| small | small |
-------------------------------
3) -------------------------------
| large item |
-------------------------------
| small | small | small |
-------------------------------
Run Code Online (Sandbox Code Playgroud)
我认为 Flexbox“必须这么做”。但现在它不会按预期换行。右侧的项目(应该位于第二行)刚刚溢出弹性盒。
谁能看到我做错了什么吗?非常感谢!
一个例子在这里:Codepen
0) -------------------------------
| large item |
-------------------------------
1) -------------------------------
| large item | small |
-------------------------------
2) -------------------------------
| large item |
-------------------------------
| small | small |
-------------------------------
3) -------------------------------
| large item |
-------------------------------
| small | small | small |
-------------------------------
Run Code Online (Sandbox Code Playgroud)
.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;
}
}
Run Code Online (Sandbox Code Playgroud)
您需要更好地了解柔性网格。这是一篇很好的Flex 文档。
您遇到的主要问题是 id flex-grow: 1
.container {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1em;
}
.item_large {
flex: 1;
background-color: green;
}
.item_small {
flex: 0 0 190px;
background-color: steelblue;
}
[class^="item_"] {
height: 2em;
border-radius: 5px;
margin: 0.5em;
display: flex;
align-items: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="item_large"></div>
</div>
<div class="container">
<div class="item_large"></div>
<div class="item_small">190px</div>
</div>
<div class="container">
<div class="item_large"></div>
</div>
<div class="container">
<div class="item_large"></div>
<div class="item_large"></div>
</div>
<div class="container">
<div class="item_large"></div>
</div>
<div class="container">
<div class="item_large"></div>
<div class="item_large"></div>
<div class="item_large"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
您可能想更多地了解 Flex 网格,以便将来可以制作任何您想要的网格。您可以在 .item_small 上使用相同的想法来制作网格 item_1 --> 10% | item_2 --> 20% | 等等...