Skip to content
Tutorials Center
My learning

Topics

Lesson 73 of 139 ยท Grid

Grid Basics

Learn how CSS Grid works: grid containers and items, columns and rows, the gap property, and how to build your first two-dimensional layout step by step.

Flexbox lays things out along one line at a time. CSS Grid works in two dimensions at once: you describe columns and rows, and the browser lines every item up in both directions. That makes Grid the natural tool for page layouts, card galleries, dashboards, forms and anything else that looks like a table without actually being tabular data.

In this lesson you will turn an element into a grid, define columns and rows, add gaps, and learn the vocabulary (lines, tracks, cells and areas) that the rest of this chapter builds on.

Making a grid container

Grid starts with one declaration on a parent element:

CSS
.gallery {
  display: grid;
}

The element with display: grid becomes a grid container. Its direct children become grid items. Grandchildren are not affected: they keep their normal layout inside their own parent, unless you make that parent a grid too (or use subgrid, covered later in this chapter).

On its own, display: grid does very little that you can see. With no columns defined, the browser creates a single column and puts each item in its own row. The magic starts when you describe the columns.

Your first grid

Three columns of 120 pixels each, and seven items. The browser fills the first row from left to right, then starts a new row, and keeps going until it runs out of items. Item 7 sits alone on the third row. Try changing the value to 120px 120px or 80px 80px 80px 80px and watch the items reflow.

grid-template-columns and grid-template-rows

These two properties define the explicit grid: the tracks you ask for by name.

  • grid-template-columns lists the width of each column, separated by spaces.
  • grid-template-rows lists the height of each row.
CSS
.layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 60px 1fr 40px;
}

That creates two columns (200px, then whatever is left) and three rows. The fr unit means "a fraction of the free space", and it is so useful it gets its own lesson: Grid Tracks: fr, repeat() and minmax().

You can mix any length units: px, rem, %, fr, auto, and the intrinsic keywords min-content and max-content. If you skip grid-template-rows entirely, rows are created automatically and sized to fit their content. That is what you want most of the time.

Columns and rows together

The first column is always 100px wide; the second takes everything that remains. Widen or narrow the preview and only the second column changes. The row heights are fixed, so the middle row stays 140px tall no matter how much text it holds. Add a long paragraph to "Main content" and you will see it overflow. In real layouts you would use auto or minmax() for rows that hold text.

Adding space with gap

gap puts space between tracks, never around the outside edge. It replaces the old trick of adding margins to every item and then cancelling the extra margin on the edges.

CSS
.grid {
  gap: 16px;          /* rows and columns */
  gap: 24px 12px;     /* row gap, then column gap */
  row-gap: 24px;      /* just rows */
  column-gap: 12px;   /* just columns */
}

Gaps behave like empty tracks: items never land in them, and backgrounds on items do not fill them. If you want a colored line between cells, give the container a background color and let it show through the gap.

Old name, same feature. You may see grid-gap, grid-row-gap and grid-column-gap in older code. They are aliases kept for compatibility. Write gap, which works for Grid, Flexbox and multi-column layouts alike.

Grid vocabulary

Grid comes with a handful of terms. Learn them once and every grid property becomes easier to read.

  • Grid lines are the dividing lines between columns and rows. A grid with 3 columns has 4 vertical lines, numbered 1 to 4 from the start edge. You place items using these numbers in Placing Grid Items.
  • Grid tracks are the spaces between two neighboring lines: a single column or a single row.
  • Grid cells are the intersection of one row and one column, the smallest unit you can place an item into.
  • Grid areas are rectangles made of one or more cells. You can give them names, which you will do in Grid Template Areas.

Browser DevTools can draw all of this for you. In Chrome, Edge and Firefox, open the Elements panel, find the grid container and click the small grid badge next to it. The overlay shows line numbers, track sizes and gaps right on the page. It is the fastest way to understand why a grid looks the way it does (see Inspecting CSS with DevTools).

How items flow into the grid

When you do not tell items where to go, the auto-placement algorithm puts them into the next free cell, one after another in source order. By default it fills row by row. The direction is controlled by grid-auto-flow:

CSS
.grid {
  grid-auto-flow: row;     /* default: fill each row, then start a new one */
  grid-auto-flow: column;  /* fill each column, then start a new one */
}

Any rows (or columns) that the browser creates beyond the ones you declared form the implicit grid. Their size comes from grid-auto-rows and grid-auto-columns, which default to auto. Setting grid-auto-rows is a quick way to give every row a consistent height:

Implicit rows with grid-auto-rows

Every row is exactly 70px, however many items you add. Try grid-auto-rows: minmax(70px, auto) instead: rows are at least 70px but grow when content needs more room. Notice also the trick inside each tile: a one-cell grid with place-items: center centers its content in both directions. You will see that pattern again in Centering Anything and Grid Alignment.

Grid or Flexbox?

Both are modern, well supported and often used together. A quick way to choose:

  • Reach for Flexbox when content should decide the layout: a row of buttons, a navbar, tags that wrap. Items size themselves and the line adapts. See Flexbox Basics.
  • Reach for Grid when the layout should decide where content goes: columns that line up across rows, a page skeleton, a gallery with equal cells.

If you find yourself fighting Flexbox to make items in different rows line up with each other, that is Grid's job. And a grid item can itself be a flex container, so a card in a grid can use Flexbox for its own buttons.

Grid is not a table

Grid lets you build layouts that look like tables, but it adds no meaning. Screen readers do not announce rows and columns for a CSS grid. If your content really is tabular data (prices, schedules, results), use an HTML <table>; see HTML Tables. Use Grid for visual layout.

Also keep the visual order close to the source order. Grid lets you move items anywhere, but keyboard users still tab through elements in source order, so a big mismatch is confusing. More on this in Placing Grid Items.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which elements become grid items when you set display: grid on a container?

    Only direct children become grid items. Deeper descendants keep their own layout unless their parent is also a grid (or uses subgrid).
  2. 2. How many vertical grid lines does a grid with four columns have?

    Lines sit on both edges and between every pair of columns, so four columns have five vertical lines, numbered 1 to 5.
  3. 3. What does gap: 20px do?

    gap only creates space between tracks. Use padding on the container if you also want space around the edges.
  4. 4. You declared three columns but have nine items, and you never set grid-template-rows. What controls the height of the rows?

    The extra rows belong to the implicit grid. Their size comes from grid-auto-rows, which is auto (fit the content) unless you change it.

Summary

  • display: grid turns an element into a grid container and its direct children into grid items.
  • grid-template-columns and grid-template-rows define the explicit grid; each value is the size of one track.
  • gap (or row-gap and column-gap) adds space between tracks only.
  • Lines, tracks, cells and areas are the core vocabulary; DevTools grid overlays make them visible.
  • Items without a position are auto-placed in source order; extra tracks form the implicit grid, sized by grid-auto-rows and grid-auto-columns.
  • Use Grid for two-dimensional layout, Flexbox for one-dimensional, content-driven layout, and tables for tabular data.

Next: Grid Tracks: fr, repeat() and minmax(), where you learn the sizing tools that make grids flexible.