Flexbox showing dynamic number of items with 5 items per row using flex basis to consume extra space
Clash Royale CLAN TAG#URR8PPP
Flexbox showing dynamic number of items with 5 items per row using flex basis to consume extra space
I want to show 5 items per row using flexbox.
.parent-wrapper
height: 100%;
width: 181px;
border: 1px solid black;
.parent
display: flex;
font-size: 0;
flex-wrap: wrap;
margin: -10px 0 0 -10px;
.child
display: inline-block;
background: blue;
margin: 10px 0 0 10px;
flex: 1 1 calc(100% * (1/5) - 10px - 1px);
height: 100px;
<body>
<div class="parent-wrapper">
<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 class="child"></div>
<div class="child"></div>
</div>
</div>
</body>
I want to use the flex grow, so that resize will not affect the data displayed. This code above will stretch the data if the row less than 5 items. If i didnt use the flex grow, it works fine for the view, but when resizing there will show some blank space after the data displayed.
If without flex grow, how can i consume extra space by just using the flex basis
calc(100% * (1/5) - 10px - 1px)
Related - stackoverflow.com/questions/32802202/…
– Paulie_D
Aug 6 at 10:25
2 Answers
2
I create example for you, can try
.container
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: center;
width: 600px;
height: 300px;
border: solid 1px;
align-items: center;
.item
width: 100px;
height: 70px;
margin-right: 5px;
display: inline-block;
border: solid 1px;
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div
And if you want that items move down if you resize window you must set width and height in percentage for container block
– a.yushko
Aug 6 at 8:26
possible to use flex-grow = 1 and maintain the next row data same width as others?
– Crazy
Aug 6 at 8:44
If I understand correctly, it seems that you are looking for the justify-content
property. You want to remove space following the items while also preventing them from stretching. By setting justify-content: space-between
you can achieve the desired effect. It will create an even space between the items but not after or before. I also set flex-grow: 0 0
as I assume you don't want your items to grow or shrink.
justify-content
justify-content: space-between
flex-grow: 0 0
Here is a link to w3schools listing all possible values for justify-content
if space-between
is not the layout you need. For example, you also have the option to center
your items in order to consume the extra space.
justify-content
space-between
center
.parent-wrapper
height: 100%;
width: 181px;
border: 1px solid black;
.parent
display: flex;
font-size: 0;
flex-wrap: wrap;
margin: -10px 0 0 -10px;
justify-content: space-between; /* Spaces items evenly between each other */
.child
display: inline-block;
background: blue;
margin: 10px 0 0 10px;
flex: 0 0 calc(100% * (1/5) - 10px - 1px); /* Setting 0 (none) for flex-grow and 0 for flex shrink */
height: 100px;
<div class="parent-wrapper">
<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 class="child"></div>
<div class="child"></div>
</div>
</div>
Not really, i want the next row 4 items aligned like the first row 5 items. The last 5th's item will left blank. Without the flex grow = 1, when i resizing my page, it will shows extra space behind. Not fully used of the space.
– Crazy
Aug 6 at 8:24
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible of duplicate: stackoverflow.com/questions/29546550/flexbox-4-items-per-row
– jaydeep patel
Aug 6 at 7:51