How to display columns and rows using named CSS grid items

To display columns and rows using named CSS grid items, use the grid-area property in combination with the grid-template-areas property. This approach allows you to create intuitive, readable layouts by assigning names to grid areas.

Syntax

.container {
    display: grid;
    grid-template-areas: 
        "area1 area2 area3"
        "area4 area5 area6";
}

.item {
    grid-area: area-name;
}

Example

The following example demonstrates how to create a grid layout using named areas −

<!DOCTYPE html>
<html>
<head>
<style>
    .container {
        display: grid;
        background-color: #f0f0f0;
        grid-template-areas: 
            'header header header'
            'sidebar content content'
            'footer footer footer';
        grid-gap: 10px;
        padding: 20px;
        width: 600px;
    }
    
    .container > div {
        background-color: #4CAF50;
        color: white;
        text-align: center;
        padding: 20px;
        font-size: 18px;
        border-radius: 5px;
    }
    
    .header {
        grid-area: header;
        background-color: #2196F3;
    }
    
    .sidebar {
        grid-area: sidebar;
        background-color: #FF9800;
    }
    
    .content {
        grid-area: content;
        background-color: #4CAF50;
    }
    
    .footer {
        grid-area: footer;
        background-color: #9C27B0;
    }
</style>
</head>
<body>
    <div class="container">
        <div class="header">Header</div>
        <div class="sidebar">Sidebar</div>
        <div class="content">Main Content</div>
        <div class="footer">Footer</div>
    </div>
</body>
</html>
A grid layout appears with a blue header spanning the full width at the top, an orange sidebar on the left, green main content area taking up two-thirds of the middle row, and a purple footer spanning the full width at the bottom.

Key Points

  • Use quotes around each row definition in grid-template-areas
  • Periods (.) represent empty grid cells
  • Area names must be repeated to span multiple cells
  • Each row must have the same number of columns

Conclusion

Named CSS grid areas provide an intuitive way to create complex layouts. By combining grid-template-areas and grid-area, you can build readable, maintainable grid structures that clearly represent your design intent.

Updated on: 2026-03-15T13:17:49+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements