Reputation: 41822
Please check this fiddle where when you resize the window and when the cards are going down, the bottom part of the cards looks odd with more space between them. I want those bottom parts of the cards be align with the above cards.
I know this can be done easily with display: inline-block
but I am trying to do this using flexbox i.e display: flex
.
.parent {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.child {
min-width: 200px;
width: 200px;
height: 200px;
box-shadow: 0px 0px 0px 1px black;
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
Upvotes: 3
Views: 87
Reputation: 370993
Aligning the last items in a flex container can be tricky. There are currently no built-in properties or methods in the flexbox spec for targeting the last item in a row / column.
But there are many hacks, including the use of invisible flex items and pseudo-elements to occupy space and, therefore, achieve the desired alignment.
This is all covered in the following post:
However, your layout can be easily achieved with Flex Layout's sister technology, Grid Layout:
.parent {
display: grid; /* 1 */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* 2 */
grid-auto-rows: 200px; /* 3 */
grid-gap: 10px; /* 4 */
}
.child {
box-shadow: 0px 0px 0px 1px black;
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
Notes:
auto-fill
). Each column can be a minimum width of 200px, and a maximum width of 1fr
(i.e., proportion of free space; similar to flex-grow: 1
). (spec reference)grid-column-gap
and grid-row-gap
. Sets a 10px "margin" around each grid item. (spec reference)Browser Support for CSS Grid
Here's the complete picture: http://caniuse.com/#search=grid
Upvotes: 2