I want to create a responsive grid of squares with Bootstrap 4, to do so, I'm doing something like this (One row):
<div class="container">
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
</div>
</div>
And I'm setting the colclass to have the following rule:
.col {
padding-top: 100%;
}
But that only creates a row with each column of the height of the viewport.
This solution used to work before, but I think it breaks the Bootstrap 4 flexbox.
Any ideas?
...applies a padding value equal to the parent's
width. The parent width is.rows width. If you have 5 elements, you need to applypadding-bottom: 20%. If they're 4, you needpadding-bottom: 25%, etc...And, remember, if you want your columns to wrap responsively, you'll need to adjust you
padding-bottomvalues for each case.A much easier way of doing it is to place your square items inside the
cols. This way they will always be squares relative to current column width:Now you safely add responsiveness classes to your columns, the squares will remain squares, relative to current column width.
no-guttersclass torowto remove thecolleft/right padding values.containertocontainer-fluidif you want the container to have full page width.Bonus: how to place content into a pre-sized component:
The above solution leaves you with the next problem: the square is sized solely on the padding value. If you add any flow content to it, it will become bigger. The solution is to place the contents outside of document flow, like this:
Note you should only have one immediate
divchild (content wrapper) in each.squarefor this to work.