Aral Code

Layout Lesson 21 of 33

CSS Grid — Rows, Columns and the fr Unit

Build two-dimensional layouts with CSS Grid — grid-template-columns, the fr unit, repeat(), gap, and responsive galleries with auto-fit and minmax.

Flexbox arranges items in one direction: a row or a column. CSS Grid arranges items in two directions at once: rows and columns, like a table or a spreadsheet. It's ideal for photo galleries, dashboards, product listings and full page layouts.

Your first grid

Just like Flexbox, Grid starts on the parent: display: grid. Then you describe the columns with grid-template-columns:

Example A 3-column grid
<style>
  .grid {
    display: grid;
    grid-template-columns: 100px 100px 100px;
    gap: 10px;
  }
  .grid div { background: #0f5c4d; color: white; padding: 18px; text-align: center; border-radius: 8px; font-family: Arial, sans-serif; }
</style>
<div class="grid">
  <div>1</div><div>2</div><div>3</div>
  <div>4</div><div>5</div><div>6</div>
  <div>7</div>
</div>

Three values = three columns. The items fill the grid left to right, then start a new row automatically. You didn't need to write any rows: Grid creates them as needed.

The fr unit

Fixed pixel columns don't adapt to the screen. The fr unit means "a fraction of the free space". 1fr 1fr 1fr gives three equal columns that always fill the container:

Example Flexible columns with fr
<style>
  .g { display: grid; gap: 8px; margin-bottom: 12px; }
  .g div { background: #2f6fd6; color: white; padding: 12px; border-radius: 6px; font: 14px monospace; text-align: center; }
</style>
<div class="g" style="grid-template-columns: 1fr 1fr 1fr;"><div>1fr</div><div>1fr</div><div>1fr</div></div>
<div class="g" style="grid-template-columns: 2fr 1fr;"><div>2fr</div><div>1fr</div></div>
<div class="g" style="grid-template-columns: 200px 1fr;"><div>200px</div><div>1fr (the rest)</div></div>

2fr 1fr makes the first column twice as wide as the second. 200px 1fr gives a fixed sidebar and a flexible main area, a very common page layout.

repeat()

Instead of writing 1fr many times, use repeat(count, size):

grid-template-columns: repeat(4, 1fr);   /* same as 1fr 1fr 1fr 1fr */

gap

gap sets the space between rows and columns. You can give two values, row-gap and column-gap: gap: 20px 10px;.

Rows

grid-template-rows sets row heights in the same way. Often you leave rows automatic, but you can set them when you need fixed heights:

grid-template-rows: 80px 1fr 60px;   /* header, content, footer */

Spanning across cells

An item can stretch across several columns or rows with grid-column and grid-row, using span:

Example Items that span
<style>
  .g { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; font-family: Arial, sans-serif; }
  .g div { background: #e3f1ec; border: 2px solid #0f5c4d; padding: 14px; border-radius: 8px; text-align: center; }
  .wide { grid-column: span 2; background: #fdf3dc !important; border-color: #f2b33d !important; }
  .tall { grid-row: span 2; background: #fbe9e6 !important; border-color: #b4372b !important; }
</style>
<div class="g">
  <div class="wide">span 2 columns</div>
  <div class="tall">span 2 rows</div>
  <div>4</div>
  <div>5</div><div>6</div><div>7</div>
</div>

Using line numbers

Grid lines are numbered, starting at 1 on the left edge. grid-column: 1 / 3 means "start at line 1, end at line 3" (two columns wide). grid-column: 1 / -1 means "from the first line to the last", which spans the full width no matter how many columns there are. That's a handy trick for a full-width header inside a grid.

Responsive galleries: auto-fit and minmax

This one line is one of the most useful in all of CSS:

grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));

It means: "make as many columns as will fit, each at least 180px and at most 1fr (stretching to fill the row)." On a laptop you might get five columns; on a phone, one or two. No media queries needed.

Example A responsive product gallery
<style>
  .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
    font-family: Arial, sans-serif;
  }
  .product { border: 1px solid #e3e0d6; border-radius: 12px; overflow: hidden; background: white; }
  .product img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: #fdf3dc; display: block; }
  .product p { margin: 8px 10px 12px; }
  .price { color: #0f5c4d; font-weight: bold; }
</style>
<div class="gallery">
  <div class="product"><img src="/img/mango.svg" alt=""><p>Dried Mangoes<br><span class="price">₱120</span></p></div>
  <div class="product"><img src="/img/halo-halo.svg" alt=""><p>Halo-Halo Mix<br><span class="price">₱95</span></p></div>
  <div class="product"><img src="/img/jeepney.svg" alt=""><p>Jeepney Keychain<br><span class="price">₱45</span></p></div>
  <div class="product"><img src="/img/school-logo.svg" alt=""><p>School Pin<br><span class="price">₱30</span></p></div>
  <div class="product"><img src="/img/mango.svg" alt=""><p>Mango Jam<br><span class="price">₱150</span></p></div>
</div>

Make the editor wider and narrower to see columns appear and disappear.

Grid or Flexbox?

  • Flexbox — for a line of things: a menu, a row of buttons, a header with logo and links, a card's contents. Content decides the size.
  • Grid — for rows and columns: galleries, card listings, dashboards, the overall page layout. You decide the structure.

They work great together. A common pattern is a Grid for the page and the card list, and Flexbox inside each card.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does grid-template-columns: 1fr 1fr 1fr; create?
2. How wide is the first column in grid-template-columns: 2fr 1fr;?
3. Which makes an item stretch across 2 columns?
4. What does repeat(auto-fit, minmax(200px, 1fr)) do?

By Sir Reyel · Last updated 2026-12-04