Currently, I am dynamically fetching "boxes" and adding them to the HTML document using ngFor
. The number of boxes is unknown in advance. Here is the code I am currently using:
<div *ngIf="items && items.length" class="row">
<div *ngFor="let item of items"
class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">
<app-single-item [item]="item"></app-single-item>
</div>
</div>
The current layout looks like this:
https://i.sstatic.net/inTFn.png
However, I would like to increase the spacing between the elements to achieve a layout similar to this:
https://i.sstatic.net/5kfIi.png
To accomplish this, I set width: 21%
for col-xl-3
and added display: flex
and justify-content: between
to the row div.
The issue now is that when there are fewer than 4 boxes in a row, the spacing becomes uneven as shown below:
https://i.sstatic.net/XMzuI.png
My question is how can I maintain consistent spacing between the boxes regardless of their quantity without fixing the width of each item div
? It's important that these items always align with the top of the outer div. Thank you.