What is CSS Grid

CSS grid is a two-dimensional layout that helps web designers build complex and flexible web pages. As we know now, CSS offers a two-dimensional grid layout model which allows us exact control over the placement and the size of elements within the rows and columns. Thanks to CSS Grid's new properties and values, web designers can now create dynamic, responsive, and grid-based layouts without relying heavily on positioning workarounds or floats. It makes the process of organizing the content on a web page easier, more logical, and more effective.

Key Points on CSS Grid:

Features of Grid Layout in CSS:

What is CSS Grid Layout?

In CSS, we have a two-dimensional layout system that is called CSS grid layout, also known as CSS grid, or with the help of CSS grid, web designers can build flexible and adaptable grid-based layouts for web pages. With precise control over the positioning and sizing of elements within the grid, it offers a flexible way to arrange content in rows and columns.

The grid container and grid items make up the CSS Grid Layout model:

Why We Use Grid Layout in CSS?

Syntax for CSS Grid:

Syntax Example

/* Basic Grid Syntax */
        .grid-container {
            display: grid;
            grid-template-rows: auto auto;
            grid-template-columns: 1fr 2fr;
            gap: 10px;
                        }
        
        .grid-item {
            background-color: lightgray;
            border: 1px solid black;
                   }

Examples of CSS Grid:

The following examples demonstrate the usage of CSS Grid:

Code Example: Basic Grid Layout


            .grid-container {
            display: grid;
            grid-template-rows: auto auto;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 10px;
            padding: 10px;
                            }
        
            .grid-item {
            background-color: lightblue;
            text-align: center;
            padding: 20px;
            font-size: 18px;
                       }

Output

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Code Example: Named Grid Areas


           .grid-container {
            display: grid;
            grid-template-areas:
                "header header header"
                "sidebar content content"
                "footer footer footer";
            gap: 10px;
            padding: 10px;
                             }
        
           .header {
            grid-area: header;
            background-color: lightcoral;
                   }
        
            .side {
            grid-area: sidebar;
            background-color: lightgreen;
                  }
        
            .content {
            grid-area: content;
            background-color: lightblue;
                     }
        
            .footer {
            grid-area: footer;
            background-color: lightgray;
                   }

Output

Header
Sidebar
Content

Common Grid Properties:

CSS Grid provides a robust system for web layouts, allowing precise control over element positioning and responsiveness. Utilize its flexibility to design complex, modern web interfaces efficiently.