Complete CSS Grid Layout Tutorial

CSS Grid Layout is one of the most powerful layout systems available in modern web development. It allows developers to create complex and responsive layouts easily using rows and columns. Before CSS Grid, developers relied heavily on floats, positioning, or frameworks to build layouts. These methods were often complicated and difficult to maintain. CSS Grid simplifies the entire layout process.

The CSS Grid system works by defining a container element as a grid. Inside that grid container, the child elements automatically become grid items. Developers can then control how these items are arranged in rows and columns using grid properties.

Grid layout is extremely useful for building page structures such as dashboards, galleries, landing pages, and responsive websites. Because of its flexibility and control, many modern websites use CSS Grid along with Flexbox to design user interfaces.

In this tutorial, you will learn how CSS Grid works, how to create grid containers, how to define rows and columns, and how to position items within the grid.

What is a Grid Container?

A grid container is the parent element where the CSS Grid layout is applied. When you set the display property of an element to grid, that element becomes a grid container. All the direct children inside this container become grid items.

Creating a grid container is very simple. You only need to apply display grid to the parent element.

CSS
Grid Container Example
.container {
  display: grid;
}

Once this property is applied, the browser will treat the container as a grid system. The next step is to define rows and columns for this grid.

Defining Grid Columns

Columns define the vertical structure of the grid layout. You can specify how many columns you want and how wide they should be using the grid-template-columns property.

For example, if you want three equal columns, you can define them using the following CSS code.

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

The fr unit stands for fraction. It divides the available space equally between columns. This means the three columns will share the space evenly.

You can also define columns using fixed values such as pixels or percentages.

Defining Grid Rows

Rows define the horizontal structure of the grid. Similar to columns, rows can be defined using the grid-template-rows property.

Rows allow you to control the vertical spacing between elements.

CSS
Grid Rows Example
.container {
  display: grid;
  grid-template-rows: 200px 200px;
}

This example creates two rows with a height of 200 pixels each. Grid items will automatically be placed inside these rows.

Grid Gap (Spacing Between Items)

The gap property adds spacing between grid rows and columns. It helps create cleaner layouts without manually adding margins.

Using grid gaps makes layouts easier to manage and more consistent.

CSS
Grid Gap Example
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 20px;
}

This example creates a 20-pixel space between every row and column.

Placing Items in the Grid

One of the most powerful features of CSS Grid is the ability to place items exactly where you want them in the grid layout.

You can control where items start and end using properties such as grid-column and grid-row.

CSS
Grid Item Placement
.item1 {
  grid-column: 1 / 3;
}

This code makes the item span across two columns.

Creating Responsive Grid Layouts

CSS Grid is extremely useful for responsive design. You can automatically adjust column sizes depending on screen size.

One common technique is using the repeat function along with auto-fit and minmax.

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

This layout automatically adjusts the number of columns based on available screen space.

Real World Grid Example

Many websites use grid layouts to display cards, product listings, or image galleries. For example, an online project gallery can display multiple project cards in a grid format.

Each card represents a project with an image, title, and description. The grid system automatically organizes them into rows and columns.

CSS
Project Card Grid Example
.projects {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 25px;
}

.project-card {
  padding: 20px;
  border: 1px solid #ddd;
}

Best Practices When Using CSS Grid

  • Use CSS Grid for overall page layouts.
  • Combine Grid with Flexbox for component layouts.
  • Use gap instead of margins for spacing.
  • Use fractional units for flexible layouts.
  • Test layouts on different screen sizes.

Conclusion

CSS Grid Layout is a powerful tool that allows developers to build complex and responsive layouts with ease. By understanding grid containers, rows, columns, gaps, and item placement, you can design modern websites efficiently.

As web design continues to evolve, mastering CSS Grid will help you create flexible and scalable layouts for real-world applications.