CSS Grid

Create powerful two-dimensional page and component layouts.

Hand-drawn CSS Grid illustration showing differently sized items arranged across rows and columns

Creating complex webpage layouts used to require a lot of CSS code and workarounds. CSS Grid Layout was introduced to make building layouts easier, more organized, and more powerful. It allows developers to arrange elements into rows and columns, creating structured designs with minimal code.

Think of CSS Grid as a table-like system for layouts. Instead of manually positioning elements, you can define a grid and place items exactly where you want them.

Today, CSS Grid is widely used for building dashboards, galleries, landing pages, blog layouts, and many other modern website designs.

Why Use CSS Grid?

CSS Grid helps developers:

  • Create complex layouts easily
  • Organize content into rows and columns
  • Build responsive designs
  • Reduce the amount of CSS code needed
  • Control element placement precisely

For example, an online store may display products in a neat grid with multiple columns. CSS Grid makes this layout simple to create and maintain.

Understanding Grid Containers and Grid Items

Grid Layout works with two main parts:

  • Grid Container
  • Grid Items

The parent element becomes the grid container, while its child elements become grid items.

Example

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

By setting display: grid, the container becomes a grid layout system.

Creating Columns

The grid-template-columns property defines how many columns the grid should have.

Example

CSS
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

This creates three equal-width columns.

The fr unit stands for fraction and divides available space evenly.

Creating Rows

The grid-template-rows property defines row sizes.

Example

CSS
.container {
  display: grid;
  grid-template-rows: 100px 100px;
}

This creates two rows, each 100 pixels high.

Adding Space Between Items

The gap property creates spacing between grid items.

Example

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

This adds 20 pixels of space between rows and columns.

Using the Repeat Function

The repeat() function helps reduce repetitive code.

Example

CSS
.container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

This creates four equal-width columns.

Responsive Grid Layouts

Grid works well with responsive design.

Example

CSS
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

This automatically adjusts the number of columns based on screen size.

As the screen becomes smaller, items move to new rows automatically.

Grid Areas

CSS Grid also allows you to create named layout sections.

Example

CSS
.container {
  grid-template-areas:
    "header header"
    "sidebar content";
}

This helps organize large layouts such as blogs and dashboards.

Key Takeaways

  • Grid Layout is an important topic to understand.
  • Start with small examples and practice one step at a time.
  • Use the idea in simple projects so it becomes easier to remember.