Creating Flexible Grid Structures Using grid-template-areas


What is grid-template-areas?

How it works

    • You use strings to represent rows in your grid.
    • Each character in the string represents a cell in that row.
    • You assign a name to each area by using the same name consistently across multiple cells.
  1. Create Named Areas

    • Multiple cells with the same name create a single named grid area.
    • These areas can span multiple rows and columns.
  2. Place Grid Items

    • Use the grid-area property on grid items to assign them to specific named areas.

Example

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-template-areas:
    "header header header"
    "sidebar content footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.content {
  grid-area: content;
}

.footer {
  grid-area: footer;
}

In this example:

  • The footer spans all three columns in the second row.
  • The content spans the middle and right columns of the second row.
  • The sidebar takes the left column of the second row.
  • The header spans all three columns in the first row.
  • The named areas are header, sidebar, content, and footer.
  • The grid container has three columns and two rows.

Key points

  • You can mix named and unnamed areas in the same grid.
  • The number of strings determines the number of rows.
  • The number of characters in each string determines the number of columns.
  • Each cell can only have one name.

Visual Representation

  • For more advanced use cases, consider using CSS Grid's implicit and explicit grids.
  • grid-template-areas can be combined with other grid properties like grid-template-columns and grid-template-rows for more complex layouts.

By understanding grid-template-areas, you can create more readable and maintainable grid layouts. It's a powerful tool for achieving complex designs with CSS Grid.



Example 1: Responsive Layout with Named Areas

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  grid-template-areas:
    "header header header"
    "sidebar content content"
    "footer footer footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.content {
  grid-area: content;
}

.footer {
  grid-area: footer;
}

Example 2: Complex Layout with Overlapping Areas

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-template-areas:
    "header header header"
    "sidebar content content"
    "sidebar footer footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.content {
  grid-area: content;
}

.footer {
  grid-area: footer;
}

This example demonstrates overlapping areas. The sidebar area overlaps with the footer area, creating a visually interesting layout.

Example 3: Combining Named and Unnamed Areas

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-template-areas:
    "header header header"
    "sidebar content ."
    "footer footer footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.content {
  grid-area: content;
}

.footer {
  grid-area: footer;
}

This example shows how to combine named and unnamed areas. The middle cell in the second row is an unnamed area, which can be used for flexibility in placing elements.

.item1 {
  grid-area: 1 / 2 / 3 / 4; /* Starts at row 1, column 2, ends at row 3, column 4 */
}

While not directly using grid-template-areas, this example demonstrates how to use grid-area to place an item at a specific position within the grid.

  • You can combine grid-template-areas with other grid properties for more complex layouts.
  • The number of strings determines the number of rows.
  • The number of characters in each string of grid-template-areas determines the number of columns.
  • You can use any valid CSS selectors for the named areas.


Explicit Grid Lines and grid-column and grid-row Properties

  • Position items
    Employ grid-column and grid-row properties on individual grid items to place them within the grid.
  • Directly specify grid lines
    Use grid-template-columns and grid-template-rows to define the grid structure.

Example

.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto 1fr auto;
}

.item1 {
  grid-column: 2 / 3;
  grid-row: 1 / 2;
}

.item2 {
  grid-column: 1 / span 3;
  grid-row: 2 / 3;
}

Implicit Grid and grid-auto-flow

  • Control item placement
    Use grid-auto-flow to determine the order in which items are placed.
  • Create a flexible grid
    Let the browser automatically generate grid tracks as needed.

Example

.container {
  display: grid;
  grid-auto-flow: row dense;
}

CSS Grid Templates with Line Numbers

  • Combine both approaches
    Use line numbers to explicitly define grid tracks and grid areas, providing more control over layout.

Example

.container {
  display: grid;
  grid-template-columns: [col1] 1fr [col2] 2fr [col3] 1fr;
  grid-template-rows: [row1] auto [row2] 1fr [row3] auto;
}

.item1 {
  grid-column: col2 / col3;
  grid-row: row1 / row2;
}
  • CSS Grid Templates with Line Numbers
    Offers a balance of flexibility and control.
  • Implicit grid and grid-auto-flow
    Suitable for responsive designs where the number of grid items is dynamic.
  • Explicit grid lines and grid-column/grid-row
    Ideal for precise control over item placement and complex layouts.
  • grid-template-areas
    Best for simple, visually intuitive layouts.

Key Considerations

  • The need for responsiveness or adaptability.
  • The level of control required over item placement.
  • The complexity of your layout.

By understanding these alternatives, you can choose the approach that best suits your specific design requirements.