在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>
答案 0 :(得分:2)
AFAIK使用flexbox
实现所需输出的最佳效果可以通过以下方式操纵flex-basis
和margin
:
flexitem
margin: 20px;
display: flex;
flex: 0 1 calc(25% - 40px);
样式中的flex-basis
calc(25% - 40px)
将宽度分为四个,也会调整边距。
注意我已将flex
设置为已禁用。
flex-grow
完成了它。
让我知道您的反馈意见。谢谢!
flex-end
&#13;
#flexcontainer {
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: flex-end;
background: #ff8800;
}
.flexitem {
margin: 20px;
display: flex;
flex: 0 1 calc(25% - 40px);
background: #666666;
box-sizing: border-box;
height: 50px;
}
&#13;
修改强>:
所以我对保证金计算做了一些调整:
通过减少<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>
来更改flex-basis
以调整以便在行的左右两端删除每个20px:
10px
最右边/最左边的框位于右/左边缘:
flex: 0 1 calc(25% - 30px);
给我你的反馈意见。谢谢!
.flexitem:nth-child(4n) {
margin-right: 0;
}
.flexitem:nth-child(4n + 1) {
margin-left: 0;
}
.flexitem:last-child {
margin-right: 0;
}
&#13;
#flexcontainer {
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: flex-end;
background: #ff8800;
}
.flexitem {
margin: 20px;
display: flex;
flex: 0 1 calc(25% - 30px);
background: #666666;
box-sizing: border-box;
height: 50px;
}
.flexitem:nth-child(4n) {
margin-right: 0;
}
.flexitem:nth-child(4n + 1) {
margin-left: 0;
}
.flexitem:last-child {
margin-right: 0;
}
&#13;
答案 1 :(得分:1)
我想你可以在第一个例子中删除右边距,这是你想要的样子吗?
#flexcontainer {
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: flex-end;
background: #ff8800;
}
.flexitem {
margin: 20px;
margin-right: 0;
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>