我试图制作多行正方形(每行3个),它们具有相同的高度。
我为此编写了一些HTML和CSS,但这些方块都在同一行。
这是我到目前为止所拥有的:
#list-wrapper {
display: flex;
width: 100%;
}
#list-wrapper div {
width: 33.33%;
}
#list-wrapper div img {
flex: 1;
}

<div id="list-wrapper">
<div>
<img src="images/1.png" alt="image one" />
</div>
<div>
<img src="images/2.png" alt="image two" />
</div>
<div>
<img src="images/1.png" alt="image one" />
</div>
<div>
<img src="images/2.png" alt="image two" />
</div>
<div>
<img src="images/1.png" alt="image one" />
</div>
<div>
<img src="images/2.png" alt="image two" />
</div>
</div>
&#13;
当我加载页面时,显示所有框,但它们都在一行上超过父div的100%宽度。
非常感谢任何帮助。
答案 0 :(得分:40)
弹性容器的初始设置为flex-wrap: nowrap
。
这意味着当您创建Flex容器时(通过将display: flex
或display: inline-flex
应用于元素),所有子元素(&#34; flex项目&#34;)都被限制在一行中
要启用弹性项目以包裹使用flex-wrap: wrap
。
以下是一些Flex属性如何工作的示例:
包含图像的各种弹性项目的简单弹性容器:
#list-wrapper {
display: flex;
border: 1px solid black;
}
#list-wrapper div {}
#list-wrapper div img {
height: 150px;
width: 150px;
}
&#13;
<div id="list-wrapper">
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
</div>
&#13;
请注意,只声明了一个flex属性:display: flex
。这将建立Flex容器。默认情况下,以下行为为:
flex-wrap: nowrap
flex-direction: row
justify-content: flex-start
要允许项目换行,请添加flex-wrap: wrap
:
#list-wrapper {
display: flex;
flex-wrap: wrap; /* NEW */
border: 1px solid black;
}
#list-wrapper div { }
#list-wrapper div img {
height: 150px;
width: 150px;
}
&#13;
<div id="list-wrapper">
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
</div>
&#13;
要允许每行只有三个项目,请使用flex
属性:
#list-wrapper {
display: flex;
flex-wrap: wrap;
border: 1px solid black;
}
#list-wrapper div {
margin: 10px;
flex: 1 1 calc(33.33% - 20px); /* flex-grow, flex-shrink,
flex-basis: (width - margin) */
}
#list-wrapper div img {
height: 150px;
width: 150px;
}
&#13;
<div id="list-wrapper">
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
</div>
&#13;
此外,默认情况下,所有弹性项目的高度相同(align-items: stretch
)。
#list-wrapper {
display: flex;
flex-wrap: wrap;
border: 1px solid black;
}
#list-wrapper div {
margin: 10px;
flex: 1 1 calc(33.33% - 30px);
border: 1px solid #ccc;
background-color: lightgreen;
text-align: center;
}
#list-wrapper div img {
height: 150px;
width: 150px;
}
&#13;
<div id="list-wrapper">
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
</div>
&#13;
如果恢复flex-wrap: nowrap
并给容器增加高度,则拉伸更明显:
#list-wrapper {
display: flex;
/* flex-wrap: wrap; */
border: 1px solid black;
height: 600px;
}
#list-wrapper div {
margin: 10px;
flex: 1 1 calc(33.33% - 30px);
border: 1px solid #ccc;
background-color: lightgreen;
text-align: center;
}
#list-wrapper div img {
height: 150px;
width: 150px;
}
&#13;
<div id="list-wrapper">
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
<div><img src="http://i.imgur.com/60PVLis.png" alt=""></div>
</div>
&#13;
答案 1 :(得分:6)
如果您使用的是flexbox并希望内容换行,则必须指定flex-wrap: wrap
。默认情况下,flex项目不会换行。
此处的代码无效,因为#list-wrapper div img
的父级不是display: flex
。您需要将其移至#list-wrapper div
:
#list-wrapper div img {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
}
要使图像为三对,您应指定flex-basis: 33.33333%
。
将您的CSS更改为此,它将起作用:
#list-wrapper {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
width: 100%;
flex-wrap: wrap;
}
#list-wrapper div {
flex: 0 0 33.333%;
}