问题
Is there a method to align child items in that way: [i][i]______[i][i][i], regardless how many items are in each of the groups?
回答1:
In a Flexbox Container
- Use
justify-content
for the alignment of items horizontally. - Use
align-items
to align items vertically.
Have a look at the example snippet below:
.parent {
display: flex;
width: 100%;
height: 100px;
align-items: center; /* Align Items Vertically */
justify-content: space-between; /* Align Items Horizontally (with equal space in between each of them */
background: #eee;
}
.children {
display: flex;
}
.child {
margin: 0 5px;
padding: 0 5px;
font-size: 30px;
background: #ccc;
color: #000;
}
<div class="parent">
<div class="children left-children">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
</div>
<div class="children right-children">
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
</div>
</div>
Hope this helps!
回答2:
put left and right child in one div and give left and right in this way
<div class="parent">
<div class="left">
<div class="child">1</div>
<div class="child">2</div>
</div>
<div class="right">
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
</div>
</div>
and give this css
.parent {
display: flex;
width: 100%;
height: 100px;
justify-content: space-between; /* Align Items Horizontally */
background: #eee;
}
.child {
margin: 0 5px;
padding: 0 5px;
font-size: 30px;
background: #ccc;
color: #000;
}
来源:https://stackoverflow.com/questions/40572361/flexbox-align-items-horizontally