Thuta Learning
CSS
AdvancedWeb DevelopmentBeginner

Grid Intro

CSSLesson 2025 min

What you'll walk away with

  • Describe a grid with its vocabulary: container, item, track, line, cell and area
  • Predict where auto-placed items land and when the browser adds implicit rows
  • Make an item span several tracks with line numbers (including 1 / -1) and span
  • Choose between Grid and Flexbox by asking which way the content should line up

Explain it simply

Explain it simply

Picture a car park whose bays are painted on the ground before a single car arrives. Cars drive in and take the next free bay, starting from the front row. CSS Grid works the same way: you paint the lines on a container with display: grid plus a list of columns, and the children settle into the bays in order.

Because the lines exist first, a column has the same width in every row. Flexbox is more like a bookshelf: each shelf is filled on its own, so books on the top shelf never promise to line up with books below. That difference is why layouts that must align in both rows and columns reach for Grid.

Problem → Why → Solution

The problem: a flex-wrap card grid with a stretched last row

Lay out five product cards in a 700px container with flex-wrap, giving each card flex: 1 1 200px. Three cards fit on the first line at about 223px each. On the second line, the two leftover cards share all the remaining space and grow to 342px each, so the cards no longer form columns.

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  width: 700px;
}

.card {
  flex: 1 1 200px; /* line 1: three cards ~223px, line 2: two cards 342px */
}

Why don't the columns line up?

Flexbox has no columns, only lines. Each wrapped line distributes space among its own items, and it has no idea how wide the line above was. You can patch it with calc() widths and margin hacks, but every gap change means recalculating them. Grid defines the column tracks once, on the container, and every row shares those same tracks.

Core concept: draw the tracks first, place the items second

Writing display: grid on an element turns it into a grid container and turns its direct children into grid items. Only direct children are placed: the heading and image inside a card are not grid items. The container itself still behaves like a block box from the outside, taking the full available width. On its own, display: grid changes little you can see: without a column list you get a single column, and each item stacks on a row of its own.

A few words make grids readable. A track is one row or one column. A line is the boundary between tracks, numbered from 1, so three columns have four vertical lines (1 to 4). Lines can also be counted from the end: -1 is the last line. A cell is where one row meets one column, and an area is any rectangle of cells. The gap only separates tracks; it never adds space around the outside edge of the container.

When you do not position items yourself, auto-placement fills cells in source order: left to right, then on to the next row. If you only define columns, the browser creates a new row every time the cells run out. The tracks you wrote are the explicit grid; the rows the browser adds are implicit tracks, and their height is auto by default, so they grow to fit their content.

text
grid-template-columns: repeat(3, 1fr) with five items
-----------------------------------------------------
line: 1           2           3           4
      +-----------+-----------+-----------+
      |  item 1   |  item 2   |  item 3   |  row 1 (implicit, auto)
      +-----------+-----------+-----------+
      |  item 4   |  item 5   |   empty   |  row 2 (implicit, auto)
      +-----------+-----------+-----------+
      <-- every row reuses the same three column tracks -->

Here is the key difference to remember. Flexbox is content-out: the items' sizes shape each line. Grid is layout-in: the container decides the track sizes first and the items sit inside those tracks. So if the items should size themselves, choose Flexbox; if things must align across both rows and columns, choose Grid.

Mental model: paint the bays, then park
  1. Container draws the column tracks
  2. Lines are numbered from 1; -1 is the end
  3. Items take free cells in source order
  4. Out of cells: the browser adds a new row

Grid decides the tracks first and then fits the items in; in Flexbox the item sizes decide each line.

General pattern

css
.container {
  display: grid;
  grid-template-columns: size size size; /* one value per column */
  gap: row-gap column-gap;
}

.item {
  grid-column: start-line / end-line;    /* or: span count */
  grid-row: start-line / end-line;
}

The container gets display: grid and a list of column tracks, one value per column. Items are normally positioned by auto-placement; only when you need to, you name a start line and an end line with grid-column and grid-row.

Example: a three-column gallery

A gallery should line its photos up in three even columns no matter how many photos there are. All the grid code goes on the container, and no photo needs a hand-calculated width. Try it with this HTML: with five photos, the second row ends with one empty cell.

gallery.html
<div class="gallery">
  <figure>Photo 1</figure>
  <figure>Photo 2</figure>
  <figure>Photo 3</figure>
  <figure>Photo 4</figure>
  <figure>Photo 5</figure>
</div>
.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

Code breakdown

  1. .gallery {.gallery {

    The grid is declared on the parent, .gallery, not on each figure. Layout decisions live on the container.

  2. display: grid;display: grid;

    Makes .gallery a grid container and its five figures grid items. The text inside each figure is not an item.

  3. repeat(3, 1fr)grid-template-columns: repeat(3, 1fr);

    Same as writing 1fr 1fr 1fr: after the gaps are removed, three columns share the remaining width equally. That gives four vertical lines, and since no rows are defined, every row is implicit. The next lesson covers fr in detail.

  4. gap: 20px;gap: 20px;

    Puts 20px between rows and between columns. Items need no margins, and no extra space appears at the container's outer edges.

You should see
Photos 1-3 fill the first row as three equal columns; Photos 4-5 sit in the first two columns of the second row, exactly as wide as the cards above them.

Spanning items with line numbers and span

Auto-placement gives each item a single cell. To show a featured photo bigger, write grid-column: span 2 and it takes two columns. For an item that must cross the whole container, such as a caption bar, use grid-column: 1 / -1, meaning from the first line to the last line of the explicit grid, so it keeps working if the column count changes. The remaining items keep filling whatever cells are still free.

featured.css
.photos {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.photos .featured {
  grid-column: span 2; /* two columns wide */
  grid-row: span 2;    /* two rows tall */
}

.photos .caption {
  grid-column: 1 / -1; /* first line to last line */
}
Layout needReach for
Navbar links, an icon next to a labelFlexbox (one direction)
Cards that must line up in columnsGrid (rows + columns)
Page shell: header, sidebar, main, footerGrid
A row of tags of unknown count and widthFlexbox + flex-wrap

In real projects

Where this shows up in real projects

Real projects do not pick Grid or Flexbox once; they nest them. The page shell (header, sidebar, content, footer) and the product listing are typically Grid, while inside each card Flexbox stacks the image, title, price and button, and Flexbox also lines up the links in the navbar. A grid item can itself be a flex container; the two never conflict.

Dashboards, calendars, pricing tables and photo galleries, anything that must align in both rows and columns, are Grid territory. In Chrome, Firefox and Edge DevTools, clicking the "grid" badge next to a grid container draws an overlay with line numbers, track sizes and gaps right on the page. When an item lands somewhere unexpected, turn that overlay on first and count how many implicit rows were created.

Common mistakes

Common mistakes

1. Expecting grandchildren to be grid items

In <ul class="grid">, each <li> is a grid item but the <a> inside it is not, so grid-column on the <a> does nothing. Wrapping the children in an extra <div> has the same effect: that single wrapper becomes the only item and the whole layout collapses into one cell. Grid only places direct children.

2. Hard-coding the last line number

Writing grid-column: 1 / 5 for a caption while the grid has four columns breaks the day someone switches to three: line 5 no longer exists, so the browser adds an implicit column and the layout grows sideways. When you mean "to the end", write 1 / -1.

3. Using Grid where one direction is enough

Laying out tag chips of different lengths with grid-template-columns: repeat(6, 1fr) wastes space around short chips and squeezes long ones. When each item's own content size matters and there is only one direction, Flexbox is the better tool.

Best practices

Best practices

  • Before building a layout, ask whether it must align in both rows and columns, then pick Grid or Flexbox.
  • Space items with gap on the container instead of margins on the items.
  • Span the full width with 1 / -1 rather than a hard-coded last line number.

Mini challenge

Mini challenge: a gallery with a featured photo

Build a four-column photo grid where the first photo is featured and shown larger, and a caption bar at the bottom spans the whole grid.

  • Make .photos a 430px-wide grid with four equal columns and a 10px gap
  • The .featured photo covers two columns and two rows
  • The .caption spans to the end even if the column count changes
  • Place the other six photos with auto-placement only (no line numbers)
featured-gallery.html
<style>
  /* TODO: .photos - a 430px-wide grid, four equal columns, 10px gap */

  /* TODO: give every photo (.photos > div) a light background */

  /* TODO: .featured - cover two columns and two rows */

  /* TODO: .caption - span from the first line to the last line */
</style>
<div class="photos">
  <div class="featured">Featured</div>
  <div>B</div><div>C</div><div>D</div><div>E</div><div>F</div><div>G</div>
  <div class="caption">Six photos from the trip</div>
</div>

Expected: Featured sits top-left, 210px wide and two rows tall. B and C fill the right half of row 1, D and E the right half of row 2, F and G the first two columns of row 3, and the caption runs the full 430px along the bottom.

Show hint

span 2 takes two tracks without naming any line, and -1 is the last line of the explicit grid. The featured photo leaves the two right-hand columns free for two rows, so B, C, D and E fill those spots.

Show solution
featured-gallery.html
<style>
  .photos {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    width: 430px;
  }
  .photos > div { background: #cde; padding: 4px; }
  .featured {
    grid-column: span 2;
    grid-row: span 2;
  }
  .caption {
    grid-column: 1 / -1;
  }
</style>
<div class="photos">
  <div class="featured">Featured</div>
  <div>B</div><div>C</div><div>D</div><div>E</div><div>F</div><div>G</div>
  <div class="caption">Six photos from the trip</div>
</div>

Quick quiz 1 / 3

A container has grid-template-columns: repeat(3, 1fr), seven items and no row definitions. What happens?

Quick quiz 2 / 3

In a four-column grid, how many columns does an item with grid-column: 1 / -1 cover?

Quick quiz 3 / 3

Which layout suits Flexbox better than Grid?

What to remember

  • display: grid makes a container a grid and places only its direct children as items.
  • Read a grid as tracks (rows/columns), lines numbered from 1, cells and areas; -1 is the last line.
  • Auto-placement fills cells in source order and adds implicit rows when the cells run out.
  • Choose Grid (layout-in) when things must align in two directions, Flexbox (content-out) when item content should set the size.

Basic concepts of Grid layout — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Grid Intro | Thuta Learning