Trying to group items in two separate line by CSS Grid using different class names. It works fine until in \"red\" group have no more elements then predefined rows (this cas
You are specifying all the red
elements into the fifth row by using grid-row-start: 5
. Yes, the red elements are placed to the fifth row, and that is not immediately visible as you haven't specified an explicit row definition (say using grid-template-rows
).
You can define the implicit row definition using something like grid-auto-rows: 50px
and see that the red
element are actually in the fifth row:
ul {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-rows: 50px; /* specify row height */
list-style: none; /* remove bullets */
padding: 0; /* remove default ul padding */
}
.blue {
background-color: blue;
}
.red {
background-color: red;
grid-row-start: 5;
}
li {
border: 1px solid #bbb; /* border for illustration */
}
<ul>
<li class="blue">
<h2>1</h2>
</li>
<li class="red">
<h2>2</h2>
</li>
<li class="blue">
<h2>3</h2>
</li>
<li class="blue">
<h2>4</h2>
</li>
<li class="red">
<h2>5</h2>
</li>
<li class="red">
<h2>6</h2>
</li>
<!-- If you delete this (or any other "red") "li" element then it's working fine -->
<li class="red">
<h2>7</h2>
</li>
<li class="blue">
<h2>8</h2>
</li>
</ul>
Now your question is why the fourth red item is in the same row - because you have placed all of them in the same fifth row. An implicit grid column is created with auto width - you can control this width using grid-auto-columns
:
ul {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-rows: 50px; /* specify row height */
grid-auto-columns: 100px; /* specify column width */
list-style: none; /* remove bullets */
padding: 0; /* remove default ul padding */
}
.blue {
background-color: blue;
}
.red {
background-color: red;
grid-row-start: 5;
}
li {
border: 1px solid #bbb; /* border for illustration */
}
<ul>
<li class="blue">
<h2>1</h2>
</li>
<li class="red">
<h2>2</h2>
</li>
<li class="blue">
<h2>3</h2>
</li>
<li class="blue">
<h2>4</h2>
</li>
<li class="red">
<h2>5</h2>
</li>
<li class="red">
<h2>6</h2>
</li>
<!-- If you delete this (or any other "red") "li" element then it's working fine -->
<li class="red">
<h2>7</h2>
</li>
<li class="blue">
<h2>8</h2>
</li>
</ul>
You can do this to group them:
set order: 1
to the red items so that they always come after the blue ones,
set the first red
element to the first column using grid-column: 1
and the rest will be auto-placed using grid-column: auto
.
See demo below:
ul {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
list-style: none; /* remove bullets */
padding: 0; /* remove default ul padding */
}
.blue {
background-color: blue;
}
.red {
background-color: red;
order: 1; /* red items have higher order */
}
.red { /* first red element into first column */
grid-column: 1;
}
.red ~ .red { /* subsequent red elements auto-placed */
grid-column: auto;
}
li {
border: 1px solid #bbb; /* border for illustration */
}
<ul>
<li class="blue">
<h2>1</h2>
</li>
<li class="red">
<h2>2</h2>
</li>
<li class="blue">
<h2>3</h2>
</li>
<li class="blue">
<h2>4</h2>
</li>
<li class="red">
<h2>5</h2>
</li>
<li class="red">
<h2>6</h2>
</li>
<!-- If you delete this (or any other "red") "li" element then it's working fine -->
<li class="red">
<h2>7</h2>
</li>
<li class="blue">
<h2>8</h2>
</li>
</ul>
Explicit grid is the grid that you define using grid-template-columns
, grid-template-rows
, grid-template-areas
and related shorthand properties - see excerpts below from W3C:
Explicit Grid (W3C)
The three properties grid-template-rows, grid-template-columns, and grid-template-areas together define the explicit grid of a grid container. The final grid may end up larger due to grid items placed outside the explicit grid; in this case implicit tracks will be created, these implicit tracks will be sized by the grid-auto-rows and grid-auto-columns properties.
See an example below of and explicit 2x2 grid which we will come back to shortly:
.wrapper {
display: grid;
grid-template-columns: 100px 100px;
grid-template-rows: 50px 50px;
grid-gap: 5px;
}
/* styles */
.wrapper > div { border: 1px solid cadetblue; background: lightblue; display: flex; justify-content: center; align-items: center;}
<div class="wrapper">
<div>1</div><div>2</div><div>3</div><div>4</div>
</div>
If you have more content or elements inside the grid container but outside the explicit grid definition, then that is part of what is called as the implicit grid.
You control the size of your implicit grid using grid-auto-rows
and grid-auto-columns
properties.
Implicit Grid (W3C)
The
grid-template-rows
,grid-template-columns
, andgrid-template-areas
properties define a fixed number of tracks that form the explicit grid. When grid items are positioned outside of these bounds, the grid container generates implicit grid tracks by adding implicit grid lines to the grid. These lines together with the explicit grid form the implicit grid. Thegrid-auto-rows
andgrid-auto-columns
properties size these implicit grid tracks.
You can see implicit rows created if you have more grid items in the example above - these rows can be sized using grid-auto-rows
property:
.wrapper {
display: grid;
grid-template-columns: 100px 100px;
grid-template-rows: 50px 50px;
grid-gap: 5px;
grid-auto-rows: 25px; /* size of implicit rows */
}
/* styles */
.wrapper > div { border: 1px solid cadetblue; background: lightblue; display: flex; justify-content: center; align-items: center;}
<div class="wrapper">
<div>1</div><div>2</div><div>3</div><div>4</div>
<div>5</div><div>6</div><div>7</div><div>8</div>
</div>
To have a look at implicit columns created you can try placing some grid items beyond the second column - these rows can be sized using grid-auto-columns
property. See how a new column is created and how the grid behaves:
.wrapper {
display: grid;
grid-template-columns: 100px 100px;
grid-template-rows: 50px 50px;
grid-gap: 5px;
grid-auto-rows: 25px; /* size of implicit rows */
grid-auto-columns: 25px; /* size of implicit columns */
}
div:nth-child(5), div:nth-child(6) {
grid-column: 3; /* place in thrid column */
}
/* styles */
.wrapper > div { border: 1px solid cadetblue; background: lightblue; display: flex; justify-content: center; align-items: center;}
<div class="wrapper">
<div>1</div><div>2</div><div>3</div><div>4</div>
<div>5</div><div>6</div><div>7</div><div>8</div>
</div>