How can I centre the following element:
<div class="outer">
<div class="inner">
<div class="cards card1">Card 1</div>
<div class="cards card2">Card 2</div>
<div class="cards card3">Card 3</div>
<div class="cards card4">Card 4</div>
</div>
</div>
The number of cards is dynamically set. So is the width of outer div. Depending on the width of outer div, I would like to display them like this:
| |
| |Card 1 | |Card 2 | |Card 3| |
| | | | | | | |
| |
| |Card 4 | |
| | | |
| |
| |Card 1 | |Card 2 | |
| | | | | |
| |
| |Card 3 | |Card 4 | |
| | | | | |
| |
| |Card 1 | |Card 2 | |Card 3| |Card 4 | |
| | | | | | | | | |
There are other questions like this (Centering a div block without the width), however they don't deal with multiple rows of cards. I have tried using the following:
.outer {
text-align: center;
}
However this puts Card 4 is in the middle not on the left.
Or
.outer {
width: 100%;
}
.inner {
display: inline-block;
transform: translateX(-50%);
left: 50%;
}
This works, but only when there is a single row of cards. When there are more cards than fit in one row, it aligns everything left.