在 flexbox 中,当我使用justify-content:space-between
第一个和最后一个项目时,它们与 flex 容器的边界完全对齐,它们之间的空间是分开的。如何将第一个项目向右对齐,而其他项目也向右对齐(而不是整个宽度)?
下面的示例代码不好,因为当我使用时flex-end
,我必须为项目设置边距,所以每行中的第一个不会粘在右边。
#flexcontainer{
display: -webkit-flex;
display: flex;
flex-wrap:wrap;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-justify-content: flex-end;
justify-content: flex-end;
background:#ff8800;
}
.flexitem{
display: -webkit-flex;
display: flex;
margin:20px;
width:24%;
background:#666666;
box-sizing:border-box;
height:50px;
}
<div id="flexcontainer">
<div class="flexitem"></div>
<div class="flexitem"></div>
<div class="flexitem"></div>
<div class="flexitem"></div>
<div class="flexitem"></div>
<div class="flexitem"></div>
</div>
这也不好,因为项目没有向右对齐(但整个宽度):
#flexcontainer{
display: -webkit-flex;
display: flex;
flex-wrap:wrap;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-justify-content: space-between;
justify-content: space-between;
background:#ff8800;
}
.flexitem{
display: -webkit-flex;
display: flex;
margin:0;
width:24%;
background:#888888;
box-sizing:border-box;
height:50px;
}
<div id="flexcontainer">
<div class="flexitem"></div>
<div class="flexitem"></div>
<div class="flexitem"></div>
</div>
我想要的图像输出(如果存在 4 个元素,第一个贴在右边,没有边距,第四个贴在左边,如果超过 4 个,则其他对齐到右边):