Flexbox & GridCore· 45 min read

CSS Grid Basics

The most powerful layout system in CSS — build true two-dimensional grids with rows AND columns.

What you will learn

  • Create a grid container with columns and rows
  • Use gap and the fr unit
  • Build a responsive grid with repeat() and minmax()

Define columns with grid-template-columns

Set display: grid and describe the columns. The fr unit means “a fraction of the free space”, so 1fr 1fr 1fr makes three equal columns.

A 3-column grid with gap
<style>
  .grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
  .grid > div { background:#4338ca; color:#fff; padding:20px; border-radius:10px; text-align:center; }
</style>
<div class="grid">
  <div>1</div><div>2</div><div>3</div>
  <div>4</div><div>5</div><div>6</div>
</div>
Live preview

display: grid turns the container into a grid, and grid-template-columns: 1fr 1fr 1fr defines three columns. The fr unit means “a fraction of the leftover space”, so three equal 1fr columns split the width evenly into thirds. The six child <div>s simply flow into the grid — three per row, then wrapping to the next row automatically — with gap: 12px spacing every cell. Unlike flexbox, this lines items up in both rows and columns at once.

Responsive grids with repeat() + minmax()

This one line makes a grid that fits as many columns as possible and wraps automatically — no media queries needed.

Auto-responsive grid — resize to watch it reflow
<style>
  .auto { display: grid; gap: 12px;
          grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
  .auto > div { background:#06b6d4; color:#fff; padding:24px; border-radius:10px; text-align:center; }
</style>
<div class="auto">
  <div>A</div><div>B</div><div>C</div><div>D</div><div>E</div>
</div>
Live preview

This single line makes a fully responsive grid with no media queries. Read repeat(auto-fit, minmax(120px, 1fr)) as: “fit as many columns as you can; each column should be at least 120px but may stretch to share leftover space (1fr)”. On a wide screen you get many columns; as you narrow the preview, columns drop off and the items reflow to fill the space. minmax sets the min/max size of each column, and auto-fit decides how many fit.

Tip: Memorise this magic responsive line: grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));. It builds a responsive card grid with zero media queries.

Note: Flexbox vs Grid: use Flexbox for one-dimensional rows/columns (navbars, button groups). Use Grid for two-dimensional layouts (galleries, page layouts). They work beautifully together.

Q. What does the fr unit represent in CSS Grid?

Answer: fr is a fraction of the free space in the grid container. 1fr 1fr makes two equal columns.

✍️ Practice

  1. Build a 3-column grid of six boxes with a gap.
  2. Make a responsive image gallery using repeat(auto-fit, minmax(150px, 1fr)).

🏠 Homework

  1. Rebuild your landing page gallery as a responsive CSS Grid.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →