I am using CSS flexbox for grid based navigation which has 3 columns in row and 'n' no of rows in the grid. The width of the grid-child calculated based on the parent container width (i.e. 100%).
I set margin-right:10px;
and margin-bottom:10px;
for each 3rd of the grid-child i had set 0px
for margin-right
to force the grid-child at the right edge of the parent container.
Everything works fine if a the column equally divided by 3. (i.e. 3 columns per row).
Here the problem is, if last row has only 2 columns then we have 10px
space between grid and the grid-child.
Is there any possible solution to get rid of the remaining space.
Please find the below codepen link for better understanding: https://codepen.io/yesvin/pen/xXwBqa
HTML code block:
<div class="menu-wrapper">
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
<div class="menu">
<div class="menu-icon">
<img src="http://via.placeholder.com/80x80"/>
</div>
<div class="menu-name">
<p>Txt</p>
</div>
</div>
</div>
CSS code block:
.menu-wrapper {
width:100%;
height:100%;
display: flex;
flex-wrap:wrap;
margin:0px;
.menu {
display: inline-block;
border:solid 1px #ccc;
margin:0 10px 10px 0;
flex-grow: 1;
width: calc(33% - 10px);
}
.menu:nth-child(3n) {
margin-right:0;
}
.menu-icon {
text-align:center;
}
.menu-name {
width:80%;
margin:0 auto;
text-align:center;
}
}
Problem:
Required result
Thanks in advance.
NOTE: It is possible to achieve the similar effect by split the each row in a separate flexbox styling. But, I don't want that, because, the 'n' no. of menus are dynamically appending inside the menu-wrapper.