cssgrid-ama icon indicating copy to clipboard operation
cssgrid-ama copied to clipboard

css grid layout for the repeating pattern

Open shmdhussain opened this issue 7 years ago • 12 comments

Trying to achieve the repeating patterns like the one in the image below using css grid test1 Tried using the CSS grid , see the code pen https://codepen.io/shmdhussain/pen/MomwBq

I am not able to achieve the pattern except the first pattern,

It is working only when I give the hardcoded value for grid-row-start and grid-row-end property.

Note: Pleaser don't change the DOM structure. Thanks in advance for any help

shmdhussain avatar Jun 20 '17 13:06 shmdhussain

@shmdhussain I worked out a solution to your problem, https://codepen.io/webcraftsman/pen/awWNba. I made comments in the code to explain what I did. Basically you want to take out the hard coded values for grid-columns and grid-rows for your larger elements. I also used grid-auto-flow: row dense to get Box 5 to correctly align in the second row of your pattern.

Tested in Chrome 58, Firefox 54 and Safari 10.1.1

webcraftsman avatar Jun 20 '17 15:06 webcraftsman

Using @webcraftsman pen as a starting point I reduced the amount of css and made it easier to understand (in my opinion) but that required increasing the html slightly by adding some wrapping divs, so his is best for your request but I think mine is easiest to understand: https://codepen.io/thinsoldier/pen/NgZMMQ?editors=0100

thinsoldier avatar Jul 19 '17 16:07 thinsoldier

This looks similar to what I am trying to achieve. Any thoughts on how I would implement the following repeatable pattern? I too was only able to do it by using hard coded values. Is it just a matter of understanding the correct nth selectors to determine which item is the "big one" or am I missing something?

desired pattern

ryanwiemer avatar Oct 25 '17 07:10 ryanwiemer

@ryanwiemer I'd define 3 1fr columns and set a height on grid-auto-rows. Then put a "big left" or "big right" class on the img tags that you want to be bigger.

.big { grid-row-end: span 2; grid-column-end: span 2;} 
.left{ grid-column-start:1; } 
.right{ grid-column-start:2;}

There proably is a way to do this with nth-child also.

thinsoldier avatar Oct 25 '17 18:10 thinsoldier

@thinsoldier, Thanks for responding back! I put together a quick Codepen. Ideally I don't want to have to add a class for this. The end goal would be to repeat this pattern regardless if there are 12 items or 120 and no changes to HTML structure.

I think the nth-selectors to get a big item on 1,5,7,11,13,17....etc is just tripping me up. Math class coming back to haunt me I suppose. 😉

Would the property grid-auto-flow: dense help me or not really?

https://codepen.io/ryanwiemer/pen/ZXgeeV

Thanks, Ryan

ryanwiemer avatar Oct 25 '17 19:10 ryanwiemer

@ryanwiemer You can simplify it to two declarations: https://codepen.io/webcraftsman/pen/NaQjYQ

webcraftsman avatar Oct 25 '17 19:10 webcraftsman

Awesome! Yes, that does the trick and keeps the code simple and repeatable.

Thank you both @webcraftsman and @thinsoldier for your help with this.

ryanwiemer avatar Oct 25 '17 19:10 ryanwiemer

Hello guys, any tips on how to produce the following grid pattern image

Thanks :)

gelsot avatar May 17 '18 10:05 gelsot

@gelsot How about this: https://codepen.io/webcraftsman/pen/NMEBde? The pattern should work even if you needed to tweak the dimensions. I was just eyeballing it and guessing at the proportions and relationships of the different cells.

webcraftsman avatar May 18 '18 02:05 webcraftsman

Awesome. Thanks a billion @webcraftsman :)

gelsot avatar May 18 '18 07:05 gelsot

Screenshot 2019-05-24 at 11 13 00

Hello everyone,

I would be super grateful if anyone could help me out with this. I'm trying to make this grid pattern and make it repeatable so that I could keep adding more items.

Thank you in advance! :)

amadeakimmins avatar May 24 '19 10:05 amadeakimmins

@amadeakimmins Can you show the underlying grid? It is hard to tell from your graphic about sizing and such. And what is the grid row gap?

webcraftsman avatar May 24 '19 18:05 webcraftsman