CSS Grid Complete Guide

Learn how to master CSS Grid and effortlessly build complex responsive layouts. Know the key properties, the difference between grid and flexbox, and how to build two-dimensional layouts with simple, powerful code structures that are perfect for modern full-stack development.

authorImageStudy Abroad30 May, 2026
Backend Foundations: Examples, Guide, Concepts, Roadmap

Building a website layout used to be about messy floats or rigid tables that would easily break on mobile. It's very common for developers to fight to align items in rows and columns with perfection, without writing hundreds of lines of code. 

The answer is CSS Grid, a revolutionary layout system based on the parent-child relationship that lets you build complex structures. With a grid set up in a container, you have full control over where each element sits, which makes your responsive design process much quicker and much more sensible for any web project. 

What is CSS Grid?

Modern web design is all about flexibility. This system takes care of both the horizontal and vertical axes at the same time; older methods looked at one dimension at a time. It is a native feature of CSS, so you don't need any external libraries to build professional interfaces.

  • Two-Dimensional Control: Older tools can’t do this, but you can work on rows and columns simultaneously.

  • Code Efficiency: Less need for nested `div` tags, so your HTML is cleaner.

  • Source Order Independence: You can rearrange elements on the page without changing your HTML.

  • Alignment Tools: It has inbuilt properties to perfectly align or stretch content.

Understanding the CSS Grid Layout Basics

To start using a CSS grid layout, you must first designate an HTML element as a container. This is done by applying a specific display property to the parent. Once the parent becomes a grid, its immediate children automatically become items that you can position.

Defining the Container

You activate the system by using display: grid; or display: inline-grid;. This creates the context for all subsequent layout rules.

Columns and Rows

You define the size and number of tracks using specific properties. For example, grid-template-columns and grid-template-rows allow you to set fixed sizes in pixels or flexible sizes using percentages and fractions.

Property

Purpose

display

Turns an element into a grid container

grid-template-columns

Sets the width of each column

grid-template-rows

Sets the height of each row

gap

Controls the spacing between items

Properties of CSS Grid

Mastering a CSS grid tutorial involves learning how to manipulate the lines and areas within your layout. There are properties specifically for the container and others for the individual items inside it.

The "fr" unit is a key part of this system. It represents a fraction of the free space in the grid container. Using this unit ensures that your layout remains fluid and adapts to different screen sizes without overlapping.

Container Properties

  • grid-template-areas: This allows you to name sections of your layout (like 'header' or 'footer') and place items visually.

  • justify-items: Aligns items along the row axis (horizontally).

  • align-items: Aligns items along the column axis (vertically).

 

Item Properties

  • grid-column: Specifies where an item starts and ends across the vertical lines.

  • grid-row: Specifies the starting and ending points across horizontal lines.

  • justify-self: Overrides the container's alignment for a single specific item.

How to Build a Responsive Design using CSS Grid?

Creating a CSS grid responsive design is much simpler than using media queries for every single change. The system includes functions like repeat() and keywords like auto-fit or auto-fill.

These functions allow the browser to decide how many columns can fit on the screen based on the available width. If the screen gets smaller, the items automatically wrap to the next line without any extra CSS.

Using the Repeat Function

Instead of writing 1fr 1fr 1fr, you can write repeat(3, 1fr). This makes your code easier to read and maintain.

The Minmax Function

This tool ensures a column never gets too small or too large. You can set a minimum width of 200px and a maximum of 1fr, so the item grows to fill space but never shrinks to the point of breaking the design.

CSS Grid Examples

Looking at CSS grid examples helps bridge the gap between theory and practice. One common pattern is the "Holy Grail" layout, which features a header, footer, and three columns in the middle.

Another popular example is the masonry-style image gallery. By using grid-auto-flow: dense;, you can tell the browser to fill in gaps automatically if items are different sizes, creating a tight and professional look.

Example 1: Basic 3-Column Layout

 

CSS



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

Example 2: Named Areas Layout

 

CSS



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

CSS Grid vs Flexbox

Choosing between CSS grid vs flexbox often confuses beginners. While they can sometimes do similar things, they are designed for different purposes. Flexbox is best for one-dimensional layouts—either a single row or a single column.

Grid is designed for two-dimensional layouts where you need to align items in both directions. Use Flexbox when you want to distribute space among items in a simple navigation bar. Use Grid when you are building a full-page layout with headers, sidebars, and main content areas.

  • Flexbox: Content-based. The layout is determined by the size of the items.

  • Grid: Layout-based. You define the structure first, then place items into it.

  • Collaboration: Most modern sites use both. A grid for the main page structure and a flexbox for small components inside the grid cells.

Common Mistakes to Avoid in CSS Grid

Even with a detailed CSS grid guide, it is easy to run into issues. One common mistake is forgetting that grid properties only apply to the immediate children of the container. Grandchildren will not follow the grid rules unless you turn the child into a grid as well.

Another error is over-complicating the layout. Often, beginners try to name every single line when using simple numbers or fractional units would be much faster. Keep your code dry and only use advanced features like named areas when the layout is truly complex.

  • Nesting Issues: Remember that children of a grid item are not automatically part of the grid.

  • Unit Confusion: Don't mix too many fixed pixel values with flexible fr units unless necessary.

  • Gap vs Margin: Use the gap property for internal spacing instead of applying margins to every child element.

 

Tips for CSS Grid

Follow this CSS grid guide to build your first container. First, create a div with the class "wrapper" and five child divs inside it. In your CSS, set the wrapper to display as a grid.

Next, define your columns. Use grid-template-columns: 1fr 2fr 1fr; to make the middle section twice as wide as the sides. Finally, add a gap of 15px to ensure the content does not touch the edges of the cells.

  1. Initialise: Set the container to display: grid.

  2. Structure: Define your tracks using fr units for flexibility.

  3. Place: Use grid-column and grid-row to span items across multiple cells.

  4. Align: Use place-items: center to perfectly center all content within its boxes.

 

FAQs

Can grid items overlap each other?

Yes, by assigning multiple items to the same line numbers or areas, you can easily layer content without using absolute positioning.

What is the main difference between grid and flexbox?

Grid is for two-dimensional layouts (rows and columns), while Flexbox is better for one-dimensional layouts (either a row or a column).

Can I use CSS Grid in all modern browsers?

Yes, it is supported in all major modern browsers, including Chrome, Firefox, Safari, and Edge.

How does the fr unit work in a layout?

The fr unit stands for "fraction", and it distributes the available free space in a container proportionally among the tracks.

Do I still need media queries with this system?

While grid handles many responsive tasks, you still use media queries to change the number of columns on very small mobile screens.
Popup Close ImagePopup Open Image
Talk to a counsellorHave doubts? Our support team will be happy to assist you!
Popup Image
Join 15 Million students on the app today!
Point IconLive & recorded classes available at ease
Point IconDashboard for progress tracking
Point IconLakhs of practice questions
Download ButtonDownload Button
Banner Image
Banner Image