CSS · Layout

CSS Grid

CSS Grid lays out content in rows and columns at the same time. It is ideal for page layouts and card galleries.

Beginner 5 min Lesson 12 of 26 Runnable examples
Before you start:Flexbox
0/26 in CSS
On this page · 5 min · Beginner

Learning objectives#

  • Define columns and rows with grid-template-columns
  • Use the fr unit, repeat() and minmax()
  • Build a responsive card grid without media queries
  • Name areas with grid-template-areas

Explanation#

display: grid creates a grid container. grid-template-columns defines the columns; rows are created automatically as needed.

The fr unit means “one fraction of the free space”. 1fr 2fr gives the second column twice the width of the first.

repeat(auto-fill, minmax(220px, 1fr)) creates as many columns as fit, each at least 220px wide — a fully responsive grid in one line.

Syntax#

CSS Illustrative
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 24px;
}

Code example: Holy-grail layout with named areas#

HTMLHoly-grail layout with named areas Runnable
<style>
  body { margin:0; font-family:sans-serif; }
  .page {
    display:grid; min-height:100vh; gap:8px;
    grid-template-columns: 180px 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas: "header header" "sidebar main" "footer footer";
  }
  .page > * { padding:16px; background:#eef2ff; }
  header { grid-area: header; } aside { grid-area: sidebar; }
  main { grid-area: main; } footer { grid-area: footer; }
  @media (max-width: 600px) {
    .page { grid-template-columns: 1fr; grid-template-areas: "header" "main" "sidebar" "footer"; }
  }
</style>
<div class="page">
  <header>Header</header><aside>Sidebar</aside><main>Main content</main><footer>Footer</footer>
</div>
Expected output
A header across the top, a sidebar and main area side by side, and a footer. Below 600px the sidebar moves under the main content.
Opens this example in the full playground

Step-by-step explanation#

  1. grid-template-columns: 180px 1fr creates a fixed sidebar column and a flexible main column.
  2. grid-template-rows: auto 1fr auto lets the middle row stretch to fill the height.
  3. grid-template-areas draws the layout with names; each child is placed with grid-area.
  4. The media query redefines the areas as a single column for narrow screens.

Real-world use case#

Dashboards, documentation layouts (like this page), photo galleries and product listings are all natural Grid layouts.

Common mistakes#

Expecting grandchildren to be grid items

Only direct children of the grid container are placed on the grid.

Mismatched area strings

Each string in grid-template-areas must have the same number of cells, and named areas must be rectangles.

Best practices#

  • Use repeat(auto-fill, minmax()) for responsive card grids
  • Use named areas for page-level layouts
  • Combine Grid for the page with Flexbox inside components
  • Keep the source order logical — visual reordering does not change keyboard order

Remember this#

Grid = rows and columns together.

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

Flexbox for components, Grid for page layout.

Practice exercise#

Your task: Create a gallery where each card is at least 160px wide, with 12px gaps, that adapts to any screen width.

Show solution
CSS Illustrative
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }

Mini quiz#

CSS Grid quizMultiple choice Question 1 of 2

What does 1fr represent?

Score so far: 0/0

Want more? Practise all CSS MCQs · CSS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.