W

Understanding CSS Grid: A Comprehensive Guide

PinoyFreeCoder
Fri Mar 17 2023

CSS Grid is a powerful layout system that makes it easy to create complex and responsive designs. In this article, we will explore the basics of CSS Grid, including its properties and how to use it effectively.

Introduction to CSS Grid

CSS Grid is a two-dimensional layout system that allows you to control both rows and columns in your layout. It simplifies the process of creating responsive designs and helps to ensure consistent styling across different screen sizes.

Basic Setup

To start using CSS Grid, you need to create a container element and apply the display: grid property to it. Then, you can define the number of columns and rows you want to use. Here's an example:

  
  //HTML 
  <div class="grid-container">
    <div class="grid-item">1</div>
    <div class="grid-item">2</div>
    <div class="grid-item">3</div>
  </div>

  //CSS
  .grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: auto;
    grid-gap: 10px;
  }

Grid Template Columns and Rows

You can use the grid-template-columns and grid-template-rows properties to define the size and number of columns and rows in the grid. You can use various units, such as px, %, fr, or auto, to set the size. For example:

  
  //CSS
  .grid-container {
    grid-template-columns: 100px 200px 1fr;
    grid-template-rows: auto 150px;
  }
  

Grid Gap

The grid-gap property allows you to set the space between grid items. You can set the gap for both rows and columns, or specify them separately using grid-row-gap and grid-column-gap. For example:


  //CSS
  .grid-container {
    grid-gap: 10px;
  }
  

Placing Items on the Grid

You can place grid items using the grid-column-start, grid-column-end, grid-row-start, and grid-row-end properties. You can also use the shorthand properties grid-column and grid-row. For example:

  //CSS
  .grid-item-1 {
    grid-column: 1 / 3;
    grid-row: 1 / 2;
  }
  

Grid Areas

Grid areas allow you to give names to specific areas of your grid, making it easier to position grid items. To define grid areas, use the grid-template-areas property on the grid container and assign the area names to the grid items using the grid-area property. For example:


  //CSS
  .grid-container {
    grid-template-areas:
      "header header header"
      "sidebar main main"
      "footer footer footer";
  }

  .grid-header {
    grid-area: header;
  }

  .grid-sidebar {
    grid-area: sidebar;
  }

  .grid-main {
    grid-area: main;
  }

  .grid-footer {
    grid-area: footer;
  }
  

Responsive Design with CSS Grid

CSS Grid makes it easy to create responsive designs by allowing you to adjust the grid properties based on media queries. For example, you can change the number of columns and rows, or modify the grid gap based on screen size:


  //CSS
  @media (max-width: 768px) {
    .grid-container {
      grid-template-columns: repeat(2, 1fr);
      grid-gap: 5px;
    }
  }

  @media (min-width: 769px) {
    .grid-container {
      grid-template-columns: repeat(4, 1fr);
      grid-gap: 10px;
    }
  }
  

Aligning and Justifying Items

CSS Grid provides properties to align and justify items within the grid. The align-items and justify-items properties control the alignment of items along the row and column axes, respectively. The possible values are start, end, center, and stretch. For example:


  //CSS
  .grid-container {
    align-items: center;
    justify-items: start;
  }
  

Similarly, you can use align-content and justify-content to align and justify the grid itself within its container along the row and column axes, respectively. For example:


  //CSS
  .grid-container {
    align-content: center;
    justify-content: space-between;
  }
  

Conclusion

In this article, we covered the basics of CSS Grid, including its properties and how to use it effectively. By leveraging the power of CSS Grid, you can create complex and responsive layouts with ease. This versatile layout system simplifies the process of designing modern web pages and ensures consistent styling across different screen sizes.

Start Your Online Store with Shopify

Build your e-commerce business with the world's leading platform. Get started today and join millions of successful online stores.

🎉 3 MONTHS FREE for New Users! 🎉
Get Started
shopify