CSS Grid

CSS Grid Layout is a web layout system that supports two-dimensions. Flexbox is a one-dimensional layout system, so it only controls either rows or columns in this dimension. Grid lets you control rows and columns simultaneously which enables much more flexibility and control when building complex web layouts that are responsive.

Why Use CSS Grid?

There are many reasons why CSS Grid is so useful:

Basic Structure of CSS Grid

To create a CSS Grid layout, you need to define a grid container using display: grid; on the parent element. Once you have set your parent up as a grid you will set up your rows and columns and control them using grid properties.

Basic Example


          .container {
               display: grid;
               grid-template-columns: 1fr 1fr 1fr;
               grid-gap: 10px;
          }
          
          .item {
               background-color: #4CAF50;
               color: white;
               padding: 20px;
          }

In this example, the grid container will have three equal-width columns and a gap of 10px between the grid items. The .item elements will automatically be arranged within these columns.

CSS Grid Properties

Grid Container Properties

The following properties are applied to the grid container:

Grid Item Properties

Grid items can also be modified using additional properties:

Media Queries with Grid

CSS Grid is also compatible with media queries, allowing you to build layouts that adapt to different screen sizes. You can change the grid’s structure based on the viewport size, creating a fully responsive design.


          @media (max-width: 600px) {
                 .container  {
                     grid-template-columns: 1fr;
                }
          }

This media query will change the grid layout to a single column on screens with a width of 600px or less.

Example: Basic Grid Layout

Here’s an example of how you can create a basic grid layout for a website using CSS Grid:


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

          .header  {
               grid-column: 1 / 3;
               background-color: #4CAF50;
               color: white;
               padding: 20px;
          }

          .sidebar  {
               background-color: #f1f1f1;
               padding: 20px;
          }

          .main  {
               background-color: #f9f9f9;
               padding: 20px;
          }

          .footer  {
               grid-column: 1 / 3;
               background-color: #333;
               color: white;
               padding: 10px;
          }

     
Header
Main Content

Best Practices for Using CSS Grid

Conclusion

CSS Grid is an extraordinary tool for all your layout needs. Once you understand the power of CSS Grid, you will have the ability to craft complex & responsive web pages with ease (it’s also a very flexible layout tool). You will be able to create exact & adaptable layouts across a multitude of screen sizes & devices by mastering CSS Grid. It should be an important tool for any modern web developer's toolbox.

Try it Yourself