Course topics

By WebNest Studio Engineering Team

CSS Tutorial

CSS Grid Layout

CSS Grid is a two-dimensional layout system that lets you control rows and columns simultaneously, which makes it the natural choice for page-level layouts, card grids, and any design where content needs to align both horizontally and vertically at once. Where Flexbox excels at distributing items along a single axis, Grid excels at defining an explicit structure that content is placed into.

Like Flexbox, Grid works through a container/item relationship: display: grid on a parent turns it into a grid container, and you define the shape of that grid with grid-template-columns and grid-template-rows, then place children into specific cells or spans.

Why it matters

Grid is the first CSS layout system designed for two dimensions: rows and columns together. Page layouts, image galleries and dashboards that needed frameworks or nested wrappers are a few lines of grid. Knowing when to use grid and when flexbox is one of the questions interviewers ask most.

Defining the Grid

grid-template-columns and grid-template-rows define the size of each column and row as a space-separated list of values, e.g. grid-template-columns: 200px 1fr 1fr; creates a fixed 200px first column followed by two equally sized flexible columns. The fr unit represents a fraction of the remaining available space, making it ideal for flexible grid tracks. gap (shorthand for row-gap and column-gap) adds consistent spacing between grid tracks without needing margins on individual items.

Placing Items with grid-area

Items can be positioned explicitly using grid-column and grid-row (e.g. grid-column: 1 / 3; spans from line 1 to line 3), or more readably by naming areas with grid-template-areas on the container and assigning each child a matching grid-area name. Named areas make complex layouts, like a classic header/sidebar/content/footer page, much easier to read at a glance than raw line numbers.

Grid vs. Flexbox

A practical rule: reach for Grid when you need to control both rows and columns together as a cohesive layout (e.g. a full page or a photo gallery), and reach for Flexbox when you're arranging items along one direction (e.g. a toolbar or a list of tags). The two are not mutually exclusive — a Grid layout commonly contains Flexbox-arranged content inside individual grid cells.

Examples

A responsive card grid using fr units and auto-fit

CSS
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
}
Output
Cards arrange themselves into as many 200px-minimum columns as fit the container width, each column stretching equally to fill any remaining space, and automatically reflowing as the viewport is resized.

A classic page layout using named grid-template-areas

CSS
.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  gap: 1rem;
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }
Output
The header and footer stretch across both columns, the sidebar occupies a fixed 200px column on the left, and the main content fills the remaining space between them, all within a full-height page layout.

Common Mistakes

  • Using Flexbox and fighting it to build a two-dimensional layout that Grid would handle far more directly.
  • Forgetting that grid line numbers start at 1, not 0, when using grid-column or grid-row with explicit line numbers.
  • Misaligning the quoted strings in grid-template-areas (uneven column counts per row), which causes the layout to silently fail.
  • Using fixed pixel widths for every column instead of the fr unit, producing a grid that doesn't adapt to different screen sizes.

Exercise

Make main a grid with two columns, the second twice as wide as the first, and a 16px gap. The heading should stretch across both columns, with the paragraph and the button side by side beneath it.

Expected result: a full-width heading, and under it the paragraph in a narrow column and the button in a wide one.

CSS · starter code
/* The page contains: <main> with an <h1>, a <p> and a <button> */

main {
  /* TODO: a two-column grid, 1 part and 2 parts, with a 16px gap */
}

h1 {
  /* TODO: span every column */
}
Show hints
  • The fr unit shares out the free space: grid-template-columns: 1fr 2fr.
  • grid-column: 1 / -1 stretches an item from the first grid line to the last.
Show solution
CSS · solution
/* The page contains: <main> with an <h1>, a <p> and a <button> */

main {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 16px;
}

h1 {
  grid-column: 1 / -1;
}

Quick quiz

1. What does the fr unit represent?

  • A. A fixed number of pixels
  • B. A fraction of the free space in the grid container
  • C. The font size of the root
  • D. A frame of animation
Show answer

B. Columns of 1fr and 2fr share the free space in the ratio one to two.

2. What does grid-template-columns: repeat(3, 1fr) create?

  • A. One column repeated in three grids
  • B. Three rows
  • C. Three equal columns
  • D. Three columns of 1px
Show answer

C. repeat avoids writing 1fr 1fr 1fr.

3. What is repeat(auto-fit, minmax(200px, 1fr)) used for?

  • A. A fixed layout of 200 columns
  • B. Animating a grid
  • C. Hiding empty cells
  • D. A responsive grid that fits as many columns of at least 200px as the width allows
Show answer

D. The number of columns changes with the available width, with no media query.

Interview questions

When would you use grid and when flexbox?

Show answer

Flexbox lays items out along one axis, a row or a column, and lets their content decide their size; it suits navigation bars, toolbars and the alignment of items inside a component. Grid controls rows and columns together from the container, so it suits page layouts and anything where items must line up in both directions. They are often combined: grid for the overall page, flexbox inside the pieces.

What is the difference between auto-fit and auto-fill?

Show answer

Both create as many columns as fit in the container. They differ when there are fewer items than columns. auto-fill keeps the empty columns, so the items stay at their minimum width with empty space beside them. auto-fit collapses the empty columns to zero, so the existing items stretch to fill the row.

Key Points to Remember

  • display: grid plus grid-template-columns/rows defines a two-dimensional track structure for a container.
  • The fr unit distributes remaining space proportionally between flexible tracks.
  • grid-template-areas paired with grid-area on children creates highly readable, named layout regions.
  • Grid suits full two-axis layouts; Flexbox suits one-axis arrangements — the two are often combined.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering