I am wanting to create a grid layout with responsive squares.
I feel like I should be able to do this with CSS Grid layout but having trouble setting the height of each square to be equal to the width.
Also having trouble setting a gutter between each square.
Would I be better off using flexbox?
Currently my HTML looks like this but will be dynamic so more squares may be added. And of course it needs to be responsive so will ideally use a media query to collapse it to one column.
<div class="square-container"> <div class="square"> <div class="content"> </div> </div> <div class="square"> <div class="content spread"> </div> </div> <div class="square"> <div class="content column"> </div> </div> </div>
Using css grid, this is as far as I got
.square-container{ display: grid; grid-template-columns: 30% 30% 30%; .square { } }
I was able to get a bit further with flexbox and able to use space-between to align squares with a nice gutter but was still struggling to get the height to match the width of each square.
I wasn't able to find any examples of this being done with either flexbox or grid but any examples would be appreciated as well.
Thanks
Building a Responsive Grid-View First ensure that all HTML elements have the box-sizing property set to border-box . This makes sure that the padding and border are included in the total width and height of the elements. Read more about the box-sizing property in our CSS Box Sizing chapter.
A responsive grid allows a layout to change dynamically based on the size of the screen. This also guarantees consistent layouts across Adobe's products.
Each square is 1 square inch. To draw this grid, put your ruler at the top of the paper, and make a small mark at every inch. Place the ruler at the bottom of the paper and do the same thing. Then use the ruler to make a straight line connecting each dot at the bottom with its partner at the top.
The padding-bottom
trick is the most used to accomplish that.
You can combine it with both Flexbox and CSS Grid, and since using percent for margin/padding gives inconsistent result for flex/grid items (on older browser versions, see edit note below), one can add an extra wrapper, or like here, using a pseudo, so the element with percent is not the flex/grid item.
Edit: Note, there's an update made to the specs., that now should give consistent result when used on flex/grid items. Be aware though, the issue still occurs on older versions.
Note, if you will add content to the content
element, it need to be position absolute to keep the square's aspect ratio.
Fiddle demo - Flexbox
Edit 2: In a comment I were asked how to have a centered text, so I added that in below snippet.
.square-container { display: flex; flex-wrap: wrap; } .square { position: relative; flex-basis: calc(33.333% - 10px); margin: 5px; border: 1px solid; box-sizing: border-box; } .square::before { content: ''; display: block; padding-top: 100%; } .square .content { position: absolute; top: 0; left: 0; height: 100%; width: 100%; display: flex; /* added for centered text */ justify-content: center; /* added for centered text */ align-items: center; /* added for centered text */ }
<div class="square-container"> <div class="square"> <div class="content"> <span>Some centered text</span> </div> </div> <div class="square"> <div class="content spread"> </div> </div> <div class="square"> <div class="content column"> </div> </div> <div class="square"> <div class="content spread"> </div> </div> <div class="square"> <div class="content column"> </div> </div> </div>
CSS Grid version
.square-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(30%, 1fr)); grid-gap: 10px; } .square { position: relative; border: 1px solid; box-sizing: border-box; } .square::before { content: ''; display: block; padding-top: 100%; } .square .content { position: absolute; top: 0; left: 0; height: 100%; width: 100%; }
<div class="square-container"> <div class="square"> <div class="content"> </div> </div> <div class="square"> <div class="content spread"> </div> </div> <div class="square"> <div class="content column"> </div> </div> <div class="square"> <div class="content spread"> </div> </div> <div class="square"> <div class="content column"> </div> </div> </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With