Placing Items with Named Lines
You'll learn how to position grid items using custom line names instead of counting line numbers, making your grid code more readable and maintainable.
Using Named Lines for Positioning
In the previous lesson, you learned how to create named grid lines using brackets in your grid-template-columns and grid-template-rows. Now you'll use those names to place items—no more counting numbers!
Instead of writing grid-column: 2 / 4, you can reference your custom names directly. If you defined a line called [sidebar-start] and another called [sidebar-end], you simply write:
.sidebar {
grid-column: sidebar-start / sidebar-end;
}
Why This Matters
Think of it like labeling moving boxes. Would you rather tell movers "put box 3 in room 7" or "put the 'kitchen' box in the 'kitchen' room"? Named lines turn grid positioning from a counting exercise into semantic, self-documenting code.
Example Pattern
.container {
display: grid;
grid-template-columns: [edge-start] 1fr [content-start] 3fr [content-end] 1fr [edge-end];
}
.main-content {
grid-column: content-start / content-end;
}
.full-width {
grid-column: edge-start / edge-end;
}
Here, .main-content stays within the central area, while .full-width spans the entire grid—all without counting lines or doing math.
The Benefit
Named lines make your grid layouts self-explanatory. Six months from now, you'll instantly understand what grid-column: main-start / main-end means, but grid-column: 2 / 5 requires mental calculation and reference to the template.
Key Takeaway: Named grid lines let you position items using meaningful labels instead of numbers, making your grid code more intuitive and easier to maintain.