Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Grid-template-columns, grid-template-rows, grid-area in CSS


CSS Grid Layout is a powerful tool for creating complex, responsive web layouts. Three important properties that help in defining the structure of a grid are grid-template-columns, grid-template-rows, and grid-area. These properties allow you to control the size and placement of grid items in the container. Let's explore each property in detail with examples.

1. grid-template-columns

The grid-template-columns property defines the size of the columns in a grid container. You can specify column sizes using fixed units (like pixels), percentages, or flexible units like fr.

Example 1: Simple Column Layout

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 100px 200px 100px;
            }
          </style>
          <div class="grid-container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example:

Example 2: Using Flexible Columns

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 1fr 2fr 1fr;
            }
          </style>
          <div class="grid-container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example:

2. grid-template-rows

The grid-template-rows property defines the size of the rows in a grid container. Similar to grid-template-columns, it accepts fixed sizes, percentages, or flexible units like fr.

Example 1: Simple Row Layout

          <style>
            .grid-container {
              display: grid;
              grid-template-rows: 150px 200px 100px;
            }
          </style>
          <div class="grid-container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example:

Example 2: Using Flexible Rows

          <style>
            .grid-container {
              display: grid;
              grid-template-rows: 1fr 2fr 1fr;
            }
          </style>
          <div class="grid-container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example:

3. grid-area

The grid-area property allows you to place a grid item into a specific grid area by specifying the grid lines it should span across. It can take four values: row-start, column-start, row-end, and column-end.

Example 1: Placing Grid Items

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 1fr 1fr 1fr;
              grid-template-rows: 100px 200px;
            }
            .item1 {
              grid-area: 1 / 1 / 2 / 2;
            }
            .item2 {
              grid-area: 2 / 2 / 3 / 4;
            }
          </style>
          <div class="grid-container">
            <div class="item1">Item 1</div>
            <div class="item2">Item 2</div>
          </div>
        

In this example:

Example 2: Using grid-area for Complex Layout

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 1fr 2fr 1fr;
              grid-template-rows: 100px 200px;
            }
            .header {
              grid-area: 1 / 1 / 2 / 4;
            }
            .main-content {
              grid-area: 2 / 1 / 3 / 3;
            }
            .sidebar {
              grid-area: 2 / 3 / 3 / 4;
            }
          </style>
          <div class="grid-container">
            <div class="header">Header</div>
            <div class="main-content">Main Content</div>
            <div class="sidebar">Sidebar</div>
          </div>
        

In this example:

Conclusion

The properties grid-template-columns, grid-template-rows, and grid-area are key to mastering CSS Grid. By using these properties, you can define the structure and placement of grid items, giving you complete control over your layout. Experiment with different combinations to create flexible, responsive web designs.










Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML 5

Python

java

C++

C

JavaScript

Campus Learning

C

C#

java