# CSS Grid: grid-template-columns, fr and repeat() Explained

> Learn CSS Grid with live examples: grid-template-columns, the fr unit, repeat(), minmax(), auto-fill vs auto-fit, grid-template-areas and responsive grids without media queries.

Author: [Marko Denic](https://markodenic.com/) | Published: 2026-10-09 | Updated: 2026-10-09 | Topics: CSS, Tutorial, Web Development | Canonical: https://markodenic.com/css-grid/

![CSS Grid: grid-template-columns, fr and repeat() Explained](https://markodenic.com/wp-content/uploads/2026/10/og-css-grid.png)

**CSS Grid** is a two-dimensional layout system: you define columns *and* rows on a parent, and its children snap into the cells. Most grids need just two lines:

`display: grid;` and `grid-template-columns: repeat(3, 1fr);`. That’s three equal columns.

This guide covers the properties you’ll use every day, with a demo you can copy: `grid-template-columns`, the `fr` unit, `repeat()`, `minmax()`, named areas and the one-line responsive grid.

### Demo:

```html
<div class="grid-demo">
  <header>Header</header>
  <aside>Sidebar</aside>
  <main>
    <div class="grid-demo__cards">
      <div>1</div><div>2</div><div>3</div>
      <div>4</div><div>5</div><div>6</div>
    </div>
  </main>
  <footer>Footer</footer>
</div>
```

```css
/* Page layout with named areas */
.grid-demo {
  display: grid;
  grid-template-columns: 120px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 12px;
  padding: 12px;
  border-radius: 16px;
  background: #f1f5f9;
  font: 600 14px/1.3 system-ui, sans-serif;
  color: #fff;
}

.grid-demo header { grid-area: header; background: #1e3a8a; }
.grid-demo aside  { grid-area: sidebar; background: #1d4ed8; }
.grid-demo main   { grid-area: main; }
.grid-demo footer { grid-area: footer; background: #1e3a8a; }

.grid-demo header,
.grid-demo aside,
.grid-demo footer {
  padding: 16px;
  border-radius: 10px;
}

/* Cards: as many 90px+ columns as fit, no media queries */
.grid-demo__cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 12px;
}

.grid-demo__cards div {
  padding: 24px 0;
  border-radius: 10px;
  background: #3b82f6;
  text-align: center;
}
```

[See the live demo](https://markodenic.com/css-grid/#demo)

The outer grid uses named areas for a classic page layout. The cards inside use the one-line responsive pattern: resize the window and the number of columns changes on its own.

## grid-template-columns

`grid-template-columns` defines how many columns the grid has and how wide each one is. Every value you list is one column.

### Fixed, percentage and flexible columns

You can mix any units. Fixed sizes stay fixed, and `fr` columns share whatever space is left.

```css
.grid {
  display: grid;

  /* 3 columns: 200px, 50% of the grid, and the rest */
  grid-template-columns: 200px 50% 1fr;

  /* Sidebar layout: fixed sidebar, flexible content */
  grid-template-columns: 250px 1fr;
}
```

### The fr unit

`fr` means “a fraction of the free space”. The browser first gives fixed columns their size, then splits what’s left between the `fr` columns in proportion. `1fr 2fr` makes the second column twice as wide as the first.

**fr vs %:** percentages are calculated from the whole grid width and ignore `gap`, so `25% 25% 25% 25%` with a gap overflows. `fr` is calculated after gaps and fixed columns, so `repeat(4, 1fr)` always fits. (If you really need percentage-based tracks, subtract the gaps with [calc()](https://markodenic.com/css-calc/).)

```css
.grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr; /* 25% / 50% / 25% of the free space */
  gap: 16px;                          /* no overflow: fr accounts for it */
}
```

### repeat()

`repeat(count, size)` saves you writing the same column over and over. It can also repeat a pattern of several columns.

```css
.grid {
  grid-template-columns: repeat(4, 1fr);         /* 1fr 1fr 1fr 1fr */
  grid-template-columns: repeat(2, 100px 1fr);   /* 100px 1fr 100px 1fr */
  grid-template-columns: 200px repeat(3, 1fr);   /* fixed + 3 equal */
}
```

### minmax()

`minmax(min, max)` lets a column flex between two sizes. A very common one is `minmax(0, 1fr)`: a plain `1fr` column won’t shrink below its longest word or widest image, so long URLs or code blocks can blow it up. `minmax(0, 1fr)` fixes that.

```css
.grid {
  /* At least 200px, at most an equal share */
  grid-template-columns: repeat(3, minmax(200px, 1fr));

  /* Equal columns that never overflow because of long content */
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
```

## Responsive grid without media queries

### repeat(auto-fill, minmax())

This one line gives you a fully responsive card grid: as many columns as fit, each at least 250px wide, all stretching to fill the row. On a phone you get one column, on a laptop three or four. Give the card images the same [`aspect-ratio`](https://markodenic.com/css-aspect-ratio/) so every row lines up.

```css
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 24px;
}

/* On screens narrower than 250px, avoid overflow */
.cards {
  grid-template-columns: repeat(auto-fill, minmax(min(250px, 100%), 1fr));
}
```

### auto-fill vs auto-fit

They only differ when there are fewer items than columns that fit. `auto-fill` keeps the empty columns, so a single card stays card-sized. `auto-fit` collapses the empty columns, so the cards stretch to fill the whole row. Use `auto-fill` for grids of cards, and `auto-fit` when a few items should spread out.

```css
/* 2 items in a wide container: */
.fill { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); } /* 2 small cards + empty space */
.fit  { grid-template-columns: repeat(auto-fit,  minmax(150px, 1fr)); } /* 2 cards, each half the row */
```

## Rows, gaps and placement

### grid-template-rows

Works exactly like `grid-template-columns`, for rows. You usually don’t need it: rows are created automatically and sized to their content. Use it for fixed-height layouts, or `grid-auto-rows` to size the automatic rows. For a sideways-scrolling row, use grid-auto-flow: column with overflow-x: auto, and [hide the scrollbar](https://markodenic.com/hide-scrollbar-css/) if it gets in the way.

```css
.app {
  display: grid;
  grid-template-rows: auto 1fr auto; /* header, content fills, footer */
  min-height: 100dvh;
}

.gallery {
  display: grid;
  grid-auto-rows: 200px; /* every automatic row is 200px tall */
}
```

### gap

`gap` sets the space between rows and columns, never at the outer edges. It works the same way in [flexbox](https://markodenic.com/css-flexbox/). One value sets both; two values set `row-gap` then `column-gap`.

```css
.grid {
  gap: 16px;      /* rows and columns */
  gap: 32px 16px; /* 32px between rows, 16px between columns */
}
```

### Span columns and rows

Grid lines are numbered from 1, and negative numbers count from the end, so `1 / -1` always means “the full width”.

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

.full-width {
  grid-column: 1 / -1; /* from the first line to the last */
}
```

### grid-template-areas

Name the regions of your layout and draw them as text. Each string is a row, each word a cell, and a `.` leaves a cell empty. Then assign elements with `grid-area`. Changing the layout at a breakpoint becomes a matter of redrawing the strings. Or draw the areas visually with my [CSS grid generator](https://markodenic.com/tools/css-grid-generator/) and copy the code.

```css
.page {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.page > header { grid-area: header; }
.page > aside  { grid-area: sidebar; }
.page > main   { grid-area: main; }
.page > footer { grid-area: footer; }

/* Mobile: stack everything */
@media (max-width: 700px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas: "header" "main" "sidebar" "footer";
  }
}
```

### Center anything with place-items

`place-items` is shorthand for `align-items` and `justify-items`. On a grid, `place-items: center` is the shortest way to [center a div](https://markodenic.com/center-a-div/) both ways.

```css
.center {
  display: grid;
  place-items: center;
  min-height: 300px;
}
```

## Grid vs Flexbox

Grid is two-dimensional: you define the columns and rows first, and items fill them. [Flexbox](https://markodenic.com/css-flexbox/) is one-dimensional: items flow in a single row or column and size themselves from their content. Use Grid for page layouts and anything that must line up in both directions (card grids, dashboards, forms), and Flexbox inside components (navigation bars, buttons with icons, toolbars).

## FAQ

### What does 1fr mean in CSS?

One fraction of the free space in the grid. After fixed-size columns and gaps are taken out, the rest is split between the `fr` columns in proportion to their numbers.

### How do I make a responsive grid without media queries?

Use `grid-template-columns: repeat(auto-fill, minmax(250px, 1fr))`. The browser fits as many 250px+ columns as it can and stretches them to fill the row.

### What’s the difference between auto-fill and auto-fit?

With few items, `auto-fill` keeps empty columns (items stay their minimum size), while `auto-fit` collapses them (items stretch to fill the row). With enough items to fill the row, they look the same.

### Why does my 1fr column overflow?

A `1fr` column can’t shrink below its content’s minimum width, like a long URL or a wide `pre` block. Use `minmax(0, 1fr)` instead. Then you can [truncate text](https://markodenic.com/text-overflow-ellipsis/) inside it with an ellipsis.

### Is CSS Grid supported in all browsers?

Yes. CSS Grid has worked in all major browsers since 2017.

Related: [CSS Flexbox guide](https://markodenic.com/css-flexbox/), [CSS calc()](https://markodenic.com/css-calc/) and [CSS tips you won’t see in most tutorials](https://markodenic.com/css-tips/).
