Css Grid Areas Tracks And Gap Complete Guide

 Last Update:2025-06-22T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    9 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of CSS Grid Areas, Tracks, and Gap


CSS Grid: Areas, Tracks, and Gap

CSS Grid is a powerful layout system in CSS that allows for two-dimensional layout design with rows and columns, making it easier to create responsive designs without relying on floats or positioning. It’s particularly useful for complex web pages requiring precise placement of elements. The key concepts within CSS Grid include Grid Areas, Grid Tracks, and Gap (or Gutters). By understanding these components, you can build highly efficient and visually appealing web page layouts.

Grid Tracks

Grid Tracks constitute the rows and columns in your grid layout. They are defined through grid-template-columns and grid-template-rows properties, respectively.

Grid Columns

  • Definition: Columns are vertical tracks in your grid. They divide your layout into vertical sections.
  • Usage: Use the grid-template-columns property to specify the size of each column.
  • Example:
    .container {
      display: grid;
      grid-template-columns: 1fr 2fr 3fr; /* Creates three columns, where the width of each is a fraction of the available space */
    }
    
  • Keyword Representation: You can use keywords like auto, max-content, min-content, and fit-content() to define column sizes dynamically based on content.

Grid Rows

  • Definition: Rows are horizontal tracks in your grid, dividing your layout into horizontal sections.
  • Usage: The grid-template-rows property specifies the size of each row.
  • Example:
    .container {
      display: grid;
      grid-template-rows: 50px auto 100px; /* Defines the top row as 50 pixels fixed height, the middle row adjusts automatically based on its content, and the bottom row as 100 pixels fixed */
    }
    

Grid Areas

Grid Areas refer to the named sections of your grid that can be composed of multiple cells (one or more tracks combined).

  • Definition: Each area occupies one or more grid tracks and can have any shape.
  • Naming: You name these areas using grid-template-areas on the parent container and then reference them in the child elements.
  • Usage: Assign names to grid cells to create complex layouts easily.
  • Example:
    .container {
      display: grid;
      grid-template-columns: 100px 100px 100px;
      grid-template-rows: 100px 100px 100px;
      grid-template-areas: 
        "header header header"
        "sidebar main main"
        "footer footer footer";
    }
    
    .header { grid-area: header; }
    .sidebar { grid-area: sidebar; }
    .main { grid-area: main; }
    .footer { grid-area: footer; }
    
  • Importance: Using named grid areas helps in creating complex grid layouts in a readable and manageable way. It also simplifies reorganizing the layout with different screen sizes.

Grid Gap

Grid Gap is used to control the spacing between grid items. It is the space between tracks in the grid layout.

  • Definition: A gap is the space you want to leave between grid items.

  • Types: There are two types of gaps:

    • grid-gap: This is a shorthand property for both grid-row-gap and grid-column-gap.
    • grid-row-gap: This controls the vertical gap between rows.
    • grid-column-gap: This controls the horizontal gap between columns.
  • Renaming Note: As of CSS Grid Level 2, the shorthand grid-gap has been renamed to gap. The individual properties have also been updated to row-gap and column-gap.

  • Usage: Specify the gap size in any CSS unit (e.g., px, em, %).

  • Example:

    .container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, 100px);
      gap: 10px; /* Equivalent to setting both row-gap and column-gap to 10 pixels */
      row-gap: 15px; /* Sets the vertical gap between rows to 15 pixels */
      column-gap: 5px; /* Sets the horizontal gap between columns to 5 pixels */
    }
    
  • Responsive Design: Gaps can significantly influence the aesthetics of grid layouts and are crucial when ensuring that layouts remain visually appealing across different devices and screens.

Combining Grid Areas, Tracks, and Gap

When designing a complex layout, combining Grid Tracks, Grid Areas, and Gap ensures precision and flexibility. Here’s an example that illustrates all three:

.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 
    "header header header"
    "sidebar main main"
    "footer footer footer";
  gap: 10px; /* Adds a 10 pixel gap around all grid items */
}

.header {
  grid-area: header; /* Assigns this element to the header area */
  background-color: #4CAF50; /* Just for visual distinction */
}

.sidebar {
  grid-area: sidebar; /* Assigns this element to the sidebar area */
  background-color: #f44336;
}

.main {
  grid-area: main; /* Assigns this element to the main area */
  background-color: #ffeb3b;
}

.footer {
  grid-area: footer; /* Assigns this element to the footer area */
  background-color: #2196F3;
}

In this example:

  • A three-column layout (1fr 2fr 1fr) creates a sidebar area occupying a smaller fraction of the container compared to the main area.
  • Two explicit row heights (auto 1fr auto) are set with one dynamic row in between, allowing the main content to expand based on the content.
  • Named grid areas (header, sidebar, main, footer) clearly define the structure and make it easy to adjust individual items’ placement.
  • A gap of 10px adds uniform spacing around all grid items.

Important Information Summarized

  • Grid Layout Creation: Define your grid structure with display: grid;.
  • Tracks:
    • Columns: Create vertical sections using grid-template-columns.
    • Rows: Create horizontal sections using grid-template-rows.
  • Areas: Use grid-template-areas to name sections of your grid and grid-area on individual items for precise placement.
  • Gap/Gutters: Control spacing between grid items using gap, row-gap, and column-gap.
  • Responsive Design: Leverage relative units and named areas for flexibility.
  • New Terminology: Since CSS Grid Level 2, grid-gap is now gap, and its counterparts are renamed row-gap and column-gap.

By mastering these basics, developers can craft sophisticated and adaptive grid-based layouts, enhancing user experience and design quality on the web.


Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement CSS Grid Areas, Tracks, and Gap

Prerequisites

  • Basic HTML knowledge.
  • Basic CSS knowledge.

Understanding CSS Grid

Before we dive into Grid Areas, Tracks, and Gaps, let's briefly look at CSS Grid. CSS Grid Layout is a two-dimensional grid-based layout system that allows you to create grid-based designs more efficiently.

CSS Grid Terminology

  1. Grid Container: The parent element on which you apply grid layout using display: grid;.
  2. Grid Item: The child elements inside the grid container.
  3. Grid Lines: The vertical and horizontal lines that define the grid structure.
  4. Grid Cells: The spaces between two grid lines.
  5. Grid Areas: The rectangle area defined by four grid lines (two vertical and two horizontal).

CSS Grid Properties

  • display: grid; or display: inline-grid;: defines a grid container.
  • grid-template-columns: defines the number (and width) of columns in a grid.
  • grid-template-rows: defines the number (and height) of rows in the grid.
  • grid-gap or gap: controls the gap between tracks (both row and column gaps).

Example 1: Basic Grid with Columns and Rows

Let's start with a simple grid example.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="grid-container">
        <div class="item1">Item 1</div>
        <div class="item2">Item 2</div>
        <div class="item3">Item 3</div>
        <div class="item4">Item 4</div>
        <div class="item5">Item 5</div>
        <div class="item6">Item 6</div>
    </div>
</body>
</html>

CSS (styles.css)

body {
    font-family: Arial, sans-serif;
}

.grid-container {
    display: grid;
    grid-template-columns: 150px 150px 150px; /* 3 columns of 150px width */
    grid-template-rows: 100px 100px; /* 2 rows of 100px height */
    gap: 10px; /* gap between grid items */
    border: 2px solid #333;
    padding: 10px;
}

.item1, .item2, .item3, .item4, .item5, .item6 {
    background-color: #aaa;
    text-align: center;
    padding: 20px;
    border: 1px solid #333;
}

Explanation

  • .grid-container is the grid container (display: grid).
  • grid-template-columns: 150px 150px 150px; creates 3 columns, each 150px wide.
  • grid-template-rows: 100px 100px; creates 2 rows, each 100px tall.
  • gap: 10px; adds a 10px gap between items.
  • Each .itemX is a grid item inside the grid container.

![Basic Grid Layout]

Adding Grid Gaps

You can add gaps between rows and columns independently.

grid-column-gap: 20px; /* gap only between columns */
grid-row-gap: 15px; /* gap only between rows */

Or use the shorthand gap property:

gap: 15px 20px; /* first value is row gap, second is column gap */

Example 2: Grid Areas

Grid Areas allow you to name grid items and place them in specific grid locations using these names.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid Areas Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="grid-container">
        <header class="header">Header</header>
        <nav class="nav">Navigation</nav>
        <aside class="aside">Sidebar</aside>
        <main class="main">Main Content</main>
        <footer class="footer">Footer</footer>
    </div>
</body>
</html>

CSS (styles.css)

body {
    font-family: Arial, sans-serif;
}

.grid-container {
    display: grid;
    grid-template-areas: 
        "header header header"
        "nav nav nav"
        "aside main main"
        "footer footer footer";
    grid-template-columns: 200px 1fr 1fr; /* 3 columns */
    grid-template-rows: 50px 50px 150px 50px; /* 4 rows */
    gap: 10px;
    padding: 10px;
}

.header { grid-area: header; }
.nav { grid-area: nav; }
.aside { grid-area: aside; }
.main { grid-area: main; }
.footer { grid-area: footer; }

.header, .nav, .aside, .main, .footer {
    background-color: #aaa;
    padding: 20px;
    border: 1px solid #333;
}

Explanation

  • grid-template-areas defines the layout using named grid areas.
  • Each string within grid-template-areas represents a row.
  • The number of strings determines the number of rows.
  • Each word within a string represents a column, using the defined area names.
  • .header and other elements are assigned to specific grid areas using grid-area.

![Grid Areas Example]


Example 3: Grid with Automatic Tracks

CSS Grid can automatically place items into tracks based on available space.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid Auto Tracks Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="grid-container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
        <div class="item">Item 4</div>
        <div class="item">Item 5</div>
        <div class="item">Item 6</div>
        <div class="item">Item 7</div>
        <div class="item">Item 8</div>
        <div class="item">Item 9</div>
        <div class="item">Item 10</div>
    </div>
</body>
</html>

CSS (styles.css)

body {
    font-family: Arial, sans-serif;
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); /* automatically create columns based on available space */
    gap: 10px;
    padding: 10px;
}

.item {
    background-color: #aaa;
    padding: 20px;
    border: 1px solid #333;
    text-align: center;
}

Explanation

  • grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); creates columns that fill the container automatically.
  • auto-fill automatically creates as many columns as will fit in the container.
  • minmax(150px, 1fr) sets a minimum width of 150px and a flexible width that will take up 1 fraction of the available space.
  • gap: 10px; adds a gap of 10px between the grid items.

![Grid Auto Tracks Example]


Example 4: Grid with Row and Column Gaps

Let's see how to independently control the gaps between rows and columns.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid Row and Column Gaps Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="grid-container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
        <div class="item">Item 4</div>
    </div>
</body>
</html>

CSS (styles.css)

body {
    font-family: Arial, sans-serif;
}

.grid-container {
    display: grid;
    grid-template-columns: 150px 150px; /* 2 columns */
    grid-template-rows: 100px 100px; /* 2 rows */
    grid-row-gap: 20px; /* gap between rows */
    grid-column-gap: 30px; /* gap between columns */
    padding: 10px;
    border: 2px solid #333;
}

.item {
    background-color: #aaa;
    padding: 20px;
    border: 1px solid #333;
    text-align: center;
}

Explanation

  • grid-row-gap: 20px; sets a 20px gap between the rows.
  • grid-column-gap: 30px; sets a 30px gap between the columns.

Alternatively, you can use the gap shorthand:

gap: 20px 30px; /* first value is row gap, second is column gap */

![Grid Row and Column Gaps Example]


Conclusion

CSS Grid Layout provides powerful tools for creating complex and flexible layouts. By understanding and applying Grid Areas, Tracks, and Gaps, you can design responsive and versatile web pages.

Top 10 Interview Questions & Answers on CSS Grid Areas, Tracks, and Gap

1. What is CSS Grid Layout?

CSS Grid Layout is a two-dimensional layout system that allows you to align elements in both rows and columns. Unlike flexbox, which is primarily one-dimensional, CSS Grid handles two dimensions simultaneously making it easier for developers to create complex layouts.

2. How do CSS Grid Tracks work?

CSS Grid Tracks are the rows and columns that divide your grid into space. They are defined with the grid-template-columns and grid-template-rows properties. For example, setting grid-template-columns: 100px 1fr 150px; creates a grid with three columns: the first is 100px wide, the second fills the remaining space, and the third is 150px wide.

3. What are CSS Grid Areas?

CSS Grid Areas are named regions within the grid defined using the grid-template-areas property. Each area is defined by assigning a name to a specific intersection of rows and columns. Elements can be placed into these areas using the grid-area property. For example:

grid-template-areas: 
  "header header"
  "aside content";
header { grid-area: header; }
aside  { grid-area: aside; }
content { grid-area: content; }

4. How does the grid-template-areas property work?

The grid-template-areas property creates named grid areas, enabling a more intuitive layout definition. Within this property, you use strings to describe the areas, with each string representing a row. To define areas that span multiple rows or columns, you repeat the name across these rows or columns.

Example:

.container {
  display: grid;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
}

.header { grid-area: header; }
.nav    { grid-area: nav; }
.main   { grid-area: main; }
.footer { grid-area: footer; }

5. What is the gap property in CSS Grid?

The gap property in CSS Grid (also known as grid-gap) controls the space between rows and columns without affecting the grid items themselves. You can set the row gap by row-gap and the column gap by column-gap, or use the shorthand gap which applies to both:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

6. How do you create a responsive grid using CSS Grid?

Creating a responsive grid using CSS Grid involves leveraging flexible units like fr (fractional units), minmax(), and media queries. fr units divide the available space into equal parts, while minmax() ensures that tracks have minimum and maximum sizes, which are crucial for responsiveness.

Example:

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

7. Can grid areas overlap?

No, CSS Grid areas cannot directly overlap by default. If you define overlapping grid areas, the later areas will overwrite the previous ones. However, you can simulate overlapping with carefully arranged gap properties and z-index for layering.

8. How do you align items within a grid cell?

You can align items within a grid cell using properties like justify-items and align-items (for all grid items) or justify-self and align-self (for individual grid items). These properties control the distribution of space along the main and cross axes.

Example:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  justify-items: center; /* Aligns items horizontally within each cell */
  align-items: center;   /* Aligns items vertically within each cell */
}

.item {
  justify-self: end;   /* Aligns this item horizontally within its own cell */
  align-self: start;     /* Aligns this item vertically within its own cell */
}

9. Can CSS Grid be used to create complex page layouts?

Absolutely! CSS Grid excels at creating complex page layouts due to its powerful layout capabilities. You can define nested grids, use named grid areas, and precisely control spacing and alignment, making it ideal for intricate and adaptive designs.

10. What are some common use cases for CSS Grid?

CSS Grid is commonly used for:

  • Creating complex page layouts: When you need a highly structured grid with multiple rows and columns.
  • Responsive design: Leveraging flexible units and functions to create layouts that adapt to different screen sizes.
  • Packing items efficiently: Organizing data grids, galleries, and cards where items need to be packed efficiently.
  • Multi-column layouts: Creating newspaper-style multi-column layouts easily.

You May Like This Related .NET Topic

Login to post a comment.