Color scheme

Grid Areas and Named Lines

If you have used CSS Grid before, you likely placed items by specifying column and row numbers like grid-column: 1 / 3 or grid-row: 2 / 4. That works, but it requires you to mentally map numbers to positions and update multiple rules every time the layout changes. This reading assignment introduces a more descriptive approach: naming the regions of your grid and placing items by name instead of by coordinate. It also covers how grid line names give you precise, readable control when named areas are not enough.

The Grid Model: A Quick Grounding

A grid container creates a two-dimensional layout context. You define tracks using grid-template-columns and grid-template-rows, and the space between tracks is controlled with gap. Grid items are placed into the cells that form at the intersections of those tracks.

Every column and row track is separated by a grid line. A grid with three columns has four column lines: one before the first column, one between each pair of columns, and one after the last. By default these lines are numbered starting at 1 from the start edge. That numbering is what most people use when they write grid-column: 1 / 3. Named areas and named lines are simply a way to give those lines and regions meaningful labels so your CSS reads like a layout description instead of a coordinate map.

Explicit vs. Implicit Grid

Tracks you define with grid-template-columns and grid-template-rows form the explicit grid. If you place more items than the explicit grid has cells, the browser creates additional tracks automatically, forming the implicit grid. Named areas only apply to the explicit grid, so this reading focuses entirely on explicitly defined layouts.

Naming Grid Regions with grid-template-areas

The grid-template-areas property lets you describe your layout visually inside your CSS. You provide a series of quoted strings, one per row, where each word in a string names the cell in that position. The result is a layout template that is easy to read, easy to modify, and self-documenting.

The Basic Syntax

Consider a classic page layout: a full-width header, a sidebar and main content area side by side, and a full-width footer. With grid-template-areas, you define it like this:


        .page {
            display: grid;
            grid-template-columns: 250px 1fr;
            grid-template-rows: auto 1fr auto;
            grid-template-areas:
                "header  header"
                "sidebar main"
                "footer  footer";
            gap: 1rem;
        }
    

Each quoted string represents one row. Each word in the string names one cell in that row. The browser reads across and down: the first string defines row one, the second defines row two, and so on. The number of words in each string must match the number of columns.

To assign an item to a named area, use the grid-area property on the child element:


        .page-header  { grid-area: header; }
        .page-sidebar { grid-area: sidebar; }
        .page-main    { grid-area: main; }
        .page-footer  { grid-area: footer; }
    

That is the entire placement system. No column numbers, no row numbers. When you want to change the layout, say moving the sidebar to the right, you edit the template strings and nothing else changes.

The Rectangle Rule

Every named area must form a rectangle. You cannot create an L-shaped area, a T-shaped area, or any non-rectangular region. The browser will silently ignore an area definition that violates this rule and treat the affected items as auto-placed. A rectangle can span multiple columns, multiple rows, or both as long as it is a complete, unbroken rectangle.

Empty Cells with the Dot Placeholder

If you want a cell to remain empty, use a period (.) in that position. A single period tells the browser to leave that cell unoccupied without giving it a name:


        grid-template-areas:
            "header header header"
            ".      main   aside"
            "footer footer footer";
    

Responsive Layouts Without Media Query Clutter

One of the most practical advantages of named areas is how cleanly they combine with media queries. To restack the layout for small screens, you rewrite the template strings and the placement rules on the child elements do not change at all:


        .page {
            grid-template-columns: 1fr;
            grid-template-areas:
                "header"
                "main"
                "sidebar"
                "footer";
        }

        @media (min-width: 48rem) {
            .page {
                grid-template-columns: 250px 1fr;
                grid-template-areas:
                    "header  header"
                    "sidebar main"
                    "footer  footer";
            }
        }
    

The child elements keep their grid-area declarations unchanged. Only the container's template changes between breakpoints, making responsive grid work significantly easier to read and maintain than equivalent solutions using numeric line placement.

Named Areas in Action

The demo below shows a four-area grid: header, sidebar, main, and footer. The area name is always visible in each zone. Drag any colored box from the tray into a named area and it will fill that entire cell completely, with the CSS readout below updating in real time. Drop two boxes into the same area to see overlap. Drag a box back to the tray to remove it.

Drag into a zone:
Box A
Box B
Box C
Box D
grid-area: header
grid-area: sidebar
grid-area: main
grid-area: footer
/* Drag a box into an area to see the CSS */

Overlapping Grid Items

CSS Grid allows multiple items to occupy the same cell or the same named area. Unlike floats or most flexbox patterns, grid does not automatically push items apart. Two items assigned to the same area will simply stack on top of each other, which is intentional and useful for overlay compositions like hero sections, caption-over-image patterns, and layered card designs.

How Overlap Works

When two grid items are placed in the same region, the default stacking order is document order: the item that appears later in the HTML sits on top. You can override this with z-index. Unlike in normal flow, grid items participate in stacking context without needing position: relative, so z-index works directly on grid items.

A common use of grid overlap is a hero section where a background fills the area and a text block sits on top of it, pinned to the bottom with align-self: end. Both elements are assigned to the same named area:


        .hero {
            display: grid;
            grid-template-areas: "hero";
            grid-template-rows: 400px;
        }

        .hero-bg {
            grid-area: hero;
        }

        .hero-text {
            grid-area: hero;
            z-index: 1;
            align-self: end;
            padding: 2rem;
        }
    

The text block sits on top because it appears later in the markup, and z-index: 1 makes that order explicit. The align-self: end rule positions the text at the bottom of the shared area without affecting the background at all.

Overlap in Action

The demo below is a real hero section built with grid overlap. The landscape background and the text block both carry grid-area: hero. Toggle the text layer off to confirm both elements occupy the exact same area simultaneously.

grid-area: hero · z-index: 2 · align-self: end

Into the Open Country

This text block and the landscape behind it share grid-area: hero. The text sits on top via z-index: 2 and pins to the bottom with align-self: end.

Named Grid Lines

When you define grid-template-areas, the browser automatically creates named lines for every area boundary. An area named sidebar generates four implicit line names: sidebar-start and sidebar-end for both the column and row axes. You can use these names in grid-column and grid-row to reference area edges without writing a full template.

You can also define your own line names explicitly inside grid-template-columns and grid-template-rows by placing bracket-enclosed names between track sizes:


        .layout {
            display: grid;
            grid-template-columns:
                [full-start] 1fr
                [content-start] 2fr
                [content-end] 1fr
                [full-end];
        }
    

A single line can carry multiple names by listing them space-separated inside the brackets. This is useful when a line serves as both the end of one region and the start of another:


        grid-template-rows:
            [header-start] auto
            [header-end main-start] 1fr
            [main-end footer-start] auto
            [footer-end];
    

Placing Items with Named Lines

Once lines are named, you reference them in placement rules exactly as you would reference a number. The syntax is identical, and the intent becomes self-documenting:


        /* Numeric lines */
        .pull-quote { grid-column: 2 / 3; }

        /* Named lines — same result, clearer intent */
        .pull-quote { grid-column: content-start / content-end; }
    

A Practical Pattern: Full-Bleed Layouts

One of the most practical uses of named lines is the full-bleed layout pattern, where most content is constrained to a readable center column but certain elements are allowed to span the full width:


        .page {
            display: grid;
            grid-template-columns:
                [full-start]    1fr
                [wide-start]    minmax(0, 200px)
                [content-start] minmax(0, 680px)
                [content-end]   minmax(0, 200px)
                [wide-end]      1fr
                [full-end];
        }

        .page > * {
            grid-column: content-start / content-end;
        }

        .page > .full-bleed {
            grid-column: full-start / full-end;
        }

        .page > .wide {
            grid-column: wide-start / wide-end;
        }
    

Named Lines in Action

The demo below shows a five-column grid with six named lines, all labeled at the top of the grid at all times. Select a start line and the end dropdown automatically updates to show only valid choices. Watch the blue block stretch in real time, and see the grid-column declaration update below.

Named Lines In-Depth

This section is optional enrichment. It is not required for assignments or assessments in this course. If named lines clicked for you in the demos above and you want a longer walkthrough, the video below is worth your time.

The presenter is Kevin Powell, a CSS educator whose work has influenced how thousands of developers learn layout on the web. His YouTube channel includes more than 1,000 CSS-focused tutorials on modern and bleeding-edge techniques (Grid, container queries, nesting, and more). In this video he shows how named lines and related Grid features combine into sophisticated layouts that used to demand far more markup and trial and error.

Combining Areas and Named Lines

Named areas and named lines are not competing features. They complement each other. A common pattern is to use grid-template-areas for the primary structural regions and supplement it with explicit line names for elements that need to break out of those regions or sit precisely along specific boundaries.

Start with the areas that define your main layout zones. Add explicit line names only where an element needs to span or align in a way the area boundaries do not cleanly express. Over-naming creates the same readability problem as over-numbering, so name only the boundaries you will actually reference.

A useful rule of thumb: use grid-template-areas when you are describing what the layout regions are, and use named lines when you need to describe where a specific element aligns relative to a boundary.

Check Your Understanding

Work through the challenges below to practice what you just learned. Complete each challenge separately and use the copy icon above each code block to get individual feedback from your AI assistant.

Define a Named Area Layout


        /**
         * Define a grid layout with:
         *   - A full-width header spanning both columns
         *   - A narrow nav sidebar on the left (150px)
         *   - A main content area filling the remaining space
         *   - A full-width footer spanning both columns
         *
         * Then assign each child to its named area.
         */

        .page {
            display: grid;
            /* Define columns and grid-template-areas here */
        }

        .site-header { /* assign to header area */ }
        .site-nav    { /* assign to nav area    */ }
        .site-main   { /* assign to main area   */ }
        .site-footer { /* assign to footer area */ }
    

Overlap Two Elements


        /**
         * Build a card where:
         *   - .card-bg fills the entire card area
         *   - .card-caption sits on top, pinned to the bottom
         *   - z-index ensures the caption is always visible
         *
         * Both elements must share the same named grid area.
         */

        .card {
            display: grid;
            grid-template-rows: 200px;
            /* define a named area here */
        }

        .card-bg      { /* place in the named area */ }
        .card-caption { /* place in the same area, on top, at the bottom */ }
    

Span Between Named Lines


        /**
         * Define a layout with named outer and content lines:
         *   - full-start and full-end at the outer edges
         *   - content-start and content-end around the center column
         *
         * .regular-item should sit in the content column only.
         * .full-bleed-item should span the full width.
         */

        .layout {
            display: grid;
            grid-template-columns:
                /* add full-start here */ 1fr
                /* add content-start here */ 2fr
                /* add content-end here */ 1fr;
                /* add full-end here */
        }

        .regular-item    { /* content column */ }
        .full-bleed-item { /* full width     */ }
    

Key Concepts Summary

The grid-template-areas property lets you describe a layout using quoted strings, one per row, where every word names a cell. Items are placed by assigning a matching grid-area value. The rectangle rule requires every named region to form a complete, unbroken rectangle, and empty cells use a period as a placeholder. Responsive layouts benefit from this approach because only the container's template changes between breakpoints while child placement rules stay the same.

Grid overlap works by assigning multiple items to the same named area. Items stack in document order by default, and z-index controls the stacking order explicitly without requiring position: relative. Alignment properties like align-self and justify-self position overlapping items independently within their shared space.

Named lines, defined using bracket notation inside track definitions, give grid line boundaries readable labels. A single line can carry multiple names. Defining grid-template-areas automatically generates area-start and area-end line names for every defined area. Named areas and named lines work best together: areas for describing major layout zones, and named lines for precise alignment at specific boundaries.