Named Lines
In a previous example, we learned how to place an item on the grid by providing the grid-column
and grid-row
properties with specific grid lines. We can also name some or all of those grid lines when defining a grid. This allows us to use those names instead of grid lines. To name a grid line, simply add the name in square brackets:
To name a grid line, we simply provide the name in square brackets:
.container {
display: grid;
width: 100%;
height: 600px;
grid-gap: 1rem;
grid-template-columns:
[main-start sidebar-start] 200px
[sidebar-end content-start] 1fr
[column3-start] 1fr
[content-end main-end];
grid-template-rows:
[row1-start] 80px
[row2-start] 1fr
[row3-start] 1fr
[row4-start] 100px
[row4-end];
}
Now that we have line names, we can use those names when placing items. Let's recreate our basic layout using named lines, instead of line numbers:
.header {
grid-column: main-start / main-end;
grid-row: row1-start / row2-start;
}
.sidebar {
grid-column: sidebar-start / sidebar-end;
grid-row: row2-start / row4-start;
}
.content-1 {
grid-column: content-start / content-end;
grid-row: row2-start / row3-start;
}
.content-2 {
grid-column: content-start / column3-start;
grid-row: row3-start / row4-start;
}
.content-3 {
grid-column: column3-start / content-end;
grid-row: row3-start / row4-start;
}
.footer {
grid-column: main-start / main-end;
grid-row: row4-start / row4-end;
}
Here is our HTML:
<div class="container">
<div class="item header">header</div>
<div class="item sidebar">sidebar</div>
<div class="item content-1">Content-1</div>
<div class="item content-2">Content-2</div>
<div class="item content-3">Content-3</div>
<div class="item footer">footer</div>
</div>
Here is the result: