我重构非响应代码,我试图如使用Flexbox的一个2×2 网格以显示下面的代码来呈现4个图像。我曾尝试使用display: flex
,flex-flow: row-wrap
,但我的ul
中的图像充当块元素,不会并排坐在一起。使用Flexbox的在UL作为一个2x2栅格
这里是原代码:
<div class="gallery">
<h2>Newest Products:</h2>
<ul>
<li><img src="http://placehold.it/360x240" alt="First gallery image" /></li>
<li><img src="http://placehold.it/360x240" alt="Second gallery image" /></li>
<li><img src="http://placehold.it/360x240" alt="Third gallery image" /></li>
<li><img src="http://placehold.it/360x240" alt="Fourth gallery image" /></li>
</ul>
</div>
.gallery {
clear: both;
}
.gallery ul {
list-style: none;
margin: 32px 0;
}
.gallery li {
float: left;
margin: 0 10px;
}
.gallery img {
width: 280px;
height: auto;
}
这张截图是发生了什么事,当我试图使用此代码:
.gallery {
clear: both;
display: flex;
flex-flow: row wrap;
}
.gallery ul {
list-style: none;
margin: 32px 0;
}
.gallery li {
margin: 0 10px;
flex-basis: 50%;
}
.gallery img {
width: 50%;
height: auto;
}
同样,所有我想要做的就是地方这些图像以响应2x2方向。谢谢!
谢谢!它完美的工作,并感谢你解释flex项目是如何工作的(并且不起作用!)@Michael – femmebug
@Michael_B当没有容器宽度时,究竟是什么定义它包装2x2?如果你想要3顶3底,你会怎么做?我很欣赏任何输入。 –
@SteveHartley flex-basis:calc(50% - 20px); - 改变你需要的东西的50% – vals