I've been asked by a client to add a "key figures" section on a website, where there can only be a maximum of 6 key figures per row (there can be more because the website is hooked to a CMS)
So what I used for that is a CSS Grid:
* {
margin: 0;
}
.container {
display: grid;
grid-template-columns: repeat(6, max-content);
justify-content: center;
gap: 3rem;
margin: 3rem auto;
}
.item {
height: 5rem;
width: 5rem;
}
<div class="container">
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
<div class="item">
<h2>400+</h2>
<p>Lorem ipsum</p>
</div>
</div>
But the client also asked that if there are less or more than 6 key figures, the items should be centered and not to the left, like:
If 3 key figures are provided:
X X X
If 8 key figures are provided:
X X X X X X
X X
If 11 key figures are provided:
X X X X X X
X X X X X
And so on
How can I achieve that ? How can I have a 6 columns row with items centered if less or more than 6 items are in the grid?