I want to create an automatic grid without specifying exactly how many rows and cols.
Then each grid cell would be wide enough to fit the content (automatically). Then if the grid is not wide enough to fit 3 cols, then if would have 2 cols, if not enough for 2 cols, then it would have 1 col, etc, etc. Then if not wide enough for 1 col, that 1 col would be limited to 100% width (no horizontal scrolling).
I think you can do this with flex box, where flex-item would be limited to 100% max-width
, and flex-wrap
would be enabled, so it fits everything in 1 line if wide enough, but would wrap to next line if not wide enough, until there is only 1 flex-item, and that item would be limited to 100% width. But then what's the point of css grid layout?
I'm trying with grid-template-columns: repeat(auto-fit, minmax(18em, 1fr));
but you end up with horizontal scroll if width is less than 18em. If you lower it to say, 1cm, then you always get 1cm tiny cols, and content overflow the grid cell.
I'm just learning grid layout, so I hope someone can help me.
update: code:
.wpr{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(25em, 1fr));
grid-gap: 1em;
}
.wpr > div{
background-color: red;
}
img{
width: 100%;
display: inline-block;
padding-bottom: 100%;
background-color: blue;
}
.wpr > div:nth-child(1){
grid-row: auto / span 5;
}
<div class="wpr">
<div>
A<br/>
Author<br/>
<img/>
</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div>E</div>
<div>F</div>
<div>G</div>
<div>H</div>
<div>I</div>
<div>J</div>
<div>K</div>
<div>L</div>
<div>M</div>
</div>