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

Understanding the Grid Container and Items in CSS


CSS Grid is a powerful layout system that allows you to create complex web layouts with ease. The grid system divides a container into rows and columns, making it simple to position items in a structured and responsive way. In this article, we will dive into the concept of grid containers and grid items and explore how to use them in CSS.

1. What is a Grid Container?

A grid container is an element that acts as the parent of grid items. To create a grid container, you must apply display: grid; to an element. This enables the grid layout system, and all direct children of this container automatically become grid items.

Basic Example of a Grid Container

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

In the example above, the .grid-container is the parent element with display: grid;, and the three <div> elements are automatically treated as grid items.

2. Defining Rows and Columns in a Grid

You can define the number of rows and columns in the grid container using the grid-template-rows and grid-template-columns properties. These properties allow you to specify the size of each row and column, either using fixed values (like pixels) or flexible values (like percentages or fractions).

Example: Defining Grid Rows and Columns

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 1fr 1fr 1fr;
              grid-template-rows: 100px 100px;
            }
          </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:

3. Grid Items

Grid items are the direct children of a grid container. By default, grid items will be placed automatically into the grid container's rows and columns. However, you can control their placement by using grid-related properties like grid-column and grid-row.

Example: Placing Grid Items

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 1fr 1fr 1fr;
              grid-template-rows: 100px 100px;
            }

            .item1 {
              grid-column: 1 / 3; /* Spans columns 1 to 3 */
              grid-row: 1; /* Stays in the first row */
            }

            .item2 {
              grid-column: 3; /* Placed in the third column */
              grid-row: 2; /* Stays in the second row */
            }
          </style>
          <div class="grid-container">
            <div class="item1">Item 1</div>
            <div>Item 2</div>
            <div class="item2">Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
            <div>Item 6</div>
          </div>
        

In this example:

4. Auto-Placement of Grid Items

CSS Grid allows items to be placed automatically without explicitly specifying their position. This is useful when you have a dynamic number of grid items that you want to arrange without manually setting each one.

Example: Auto-Placement of Grid Items

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: repeat(3, 1fr);
              grid-gap: 10px;
            }
          </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. Responsive Grid Layout

CSS Grid is an excellent choice for creating responsive layouts. By using media queries, you can change the grid's structure based on the screen size.

Example: Responsive Grid Layout

          <style>
            .grid-container {
              display: grid;
              grid-template-columns: 1fr;
              grid-gap: 10px;
            }

            @media (min-width: 600px) {
              .grid-container {
                grid-template-columns: repeat(2, 1fr);
              }
            }

            @media (min-width: 900px) {
              .grid-container {
                grid-template-columns: repeat(3, 1fr);
              }
            }
          </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:

Conclusion

CSS Grid is a powerful layout tool that provides precise control over both rows and columns in a grid. By using properties such as grid-template-columns, grid-template-rows, grid-column, and grid-row, you can create highly customizable and responsive layouts. With its ability to auto-place items and handle complex layouts, CSS Grid is a must-have tool for modern web design.










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