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

Defining Grid Columns and Rows in CSS


CSS Grid is a powerful layout system that allows for precise control over the placement of items in both rows and columns. To make use of this layout, we can define the grid columns and rows in a container using the grid-template-columns and grid-template-rows properties. This article will explain how to define grid columns and rows in CSS and provide examples to illustrate the concepts.

1. What Are Grid Columns and Rows?

Grid columns and rows form the foundation of the CSS Grid layout. The grid-template-columns property defines the width of the columns, while the grid-template-rows property defines the height of the rows. Both properties accept values in different units, including pixels (px), percentages (%), and flexible units like fr (fractional units).

2. Defining Columns with grid-template-columns

To define the number of columns and their width, you use the grid-template-columns property. This property specifies how many columns your grid will have and what size each column will be. You can use any valid length unit, but fractional units (fr) are commonly used to create flexible layouts.

Example 1: Defining Columns

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

In this example:

Example 2: Defining Columns with Different Sizes

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

In this example:

3. Defining Rows with grid-template-rows

Just like columns, you can define the number of rows and their height using the grid-template-rows property. This property allows you to control how much space each row occupies. You can use fixed units like pixels, flexible units like fr, or even percentages for row heights.

Example 1: Defining Rows

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

In this example:

Example 2: Using fr for 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:

4. Combining Rows and Columns

You can combine both grid-template-columns and grid-template-rows to create a full grid layout. This allows you to define both the columns and rows simultaneously and control how your grid items are arranged.

Example: Full Grid Layout

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

In this example:

5. Conclusion

Defining grid columns and rows in CSS is a fundamental part of working with CSS Grid. By using grid-template-columns and grid-template-rows, you can create flexible, responsive layouts that adapt to different screen sizes. Whether you're building a simple layout or a complex grid structure, understanding how to define and manipulate rows and columns is essential for creating well-organized, dynamic web pages.










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