Course contentsShow
← Web Development
Lesson 340 of 2,9826. CSS GridFree lesson

Named Grid Lines for Semantic Templates

Assign custom names to grid lines within track definitions for more readable placement code.

Named Grid Lines for Semantic Templates

You'll learn how to give grid lines meaningful names that make your layout code easier to read and maintain.

What Are Named Grid Lines?

So far, you've positioned grid items using line numbers (1, 2, 3, etc.). CSS Grid lets you assign custom names to these lines right inside your grid-template-columns and grid-template-rows definitions. Think of it like naming streets in a city instead of just numbering them—"Main Street" is much clearer than "Street 5."

How to Name Grid Lines

Wrap your custom name in square brackets [name] before or after a track size:

.container {
  display: grid;
  grid-template-columns: [sidebar-start] 200px [sidebar-end content-start] 1fr [content-end];
  grid-template-rows: [header-start] 80px [header-end main-start] 1fr [main-end];
}

Now you can position items using these semantic names:

.header {
  grid-column: sidebar-start / content-end;
  grid-row: header-start / header-end;
}

.sidebar {
  grid-column: sidebar-start / sidebar-end;
  grid-row: main-start / main-end;
}

Why Use Named Lines?

Instead of cryptic grid-column: 1 / 3, you write grid-column: content-start / content-end. When you return to your code months later (or share it with teammates), the intent is immediately clear. Named lines make your grid templates self-documenting.

You can even give a single line multiple names, and lines with -start and -end suffixes create implicit grid areas for even simpler placement.

Key Takeaway: Named grid lines replace numeric references with meaningful labels, making your grid layout code semantic, readable, and maintainable—especially important for complex templates.