According to MDN, if grid-auto-flow
is not specified or only specified to be row or column and dense
is omitted.. it should be set to sparse
and the DOM should be respected and gaps earlier than the first element that could be filled will not be filled.
My code appears to default to dense. How could I force it to act sparse?
I also tried setting the auto flow to row or column instead of not setting it at all but that didn’t work either.
.container {
height: 400px;
width: 400px;
margin: 0 auto;
background-color: black;
display: grid;
grid-template: repeat(4, 1fr) / repeat(4, 1fr);
grid-auto-flow: row;
}
.first {
grid-column: 3;
grid-row: 1/2;
}
<div class="container">
<div class="box first" style="background-color: #E53935">FIRST</div>
<div class="box second" style="background-color: #D81B60">SECOND</div>
<div class="box" style="background-color: #8E24AA">THIRD</div>
<div class="box" style="background-color: #5E35B1">FOURTH</div>
<!-- <div class="box" style="background-color: #3949AB">FIFTH</div>
<div class="box" style="background-color: #1E88E5">SIXTH</div>
<div class="box" style="background-color: #00ACC1">SEVENTH</div>
<div class="box" style="background-color: #00897B">EIGHTH</div> -->
</div>
Below you can find the desired result of the code above. I have hardcoded the individual box placements inside the grid, how could this be achieved by only setting the first box like in the snippet above.
While hardcoding i have discovered that the sparse property only gets ignored on the first box IF grid-row
is set. If grid-row
is not set at all the grid behaves sparse
, otherwise dense
. Also if you place the second box after the first one in grid-column: 4/5;
and dont set grid-row
, the following boxes do not act dense
. Further proving this. Please feel free to try for yourself
.container {
height: 400px;
width: 400px;
margin: 0 auto;
background-color: black;
display: grid;
grid-template: repeat(4, 1fr) / repeat(4, 1fr);
grid-auto-flow: row;
}
.first {
grid-column: 3;
}
<div class="container">
<div class="box first" style="background-color: #E53935">FIRST</div>
<div class="box second" style="background-color: #D81B60">SECOND</div>
<div class="box third" style="background-color: #8E24AA">THIRD</div>
<div class="box" style="background-color: #5E35B1">FOURTH</div>
<!-- <div class="box" style="background-color: #3949AB">FIFTH</div>
<div class="box" style="background-color: #1E88E5">SIXTH</div>
<div class="box" style="background-color: #00ACC1">SEVENTH</div>
<div class="box" style="background-color: #00897B">EIGHTH</div> -->
</div>
2
Answers
While hardcoding i have discovered that the sparse property only gets ignored on the first box IF grid-row is set. If grid-row is not set at all the grid behaves sparse, otherwise dense. Also if you place the second box after the first one in grid-column: 4/5; and dont set grid-row, the following boxes do not act dense. Further proving this. Please feel free to try for yourself
The grid is behaving as intended, so let’s break down what’s happening
This declaration actually does nothing, as
row
is the initial value forgrid-auto-flow
out of the box:So we’re placing items on our grid in rows, with a "sparse" algorithm:
So you’re thinking that the sparse algorithm should give each box a fresh row, which makes sense – it’s called "sparse" and you’ve made four rows.
How it works is that sparse doesn’t backtrack to fill previous free spaces, it just goes forwards. This is entirely different to looking forward to see if there’s lots of free space.
What your grid here will do is place the boxes along in a row because they all fit, and dense/sparse will not matter at all because we’ve already placed all of our boxes on the first row.
If you want the boxes to get a row each then the
column
grid flow might work for you: