Reading history

CSS Grid Tutorial: Columns, fr, auto-fit and Areas

4 min read 0 views

Learn CSS Grid: grid-template-columns, the fr unit, repeat(), spanning cells, auto-fit with minmax() and grid-template-areas layouts.

CSS Grid is a two-dimensional layout system: it controls rows and columns at the same time. You define a grid on the parent, and items drop into its cells or span several of them. Grid is the most direct way to build whole page layouts, dashboards and galleries, and with auto-fit it can be responsive without a single media query.

In this lesson you will learn

  • How to define columns and rows
  • The fr unit and repeat()
  • Making items span several cells
  • Responsive grids with auto-fit and minmax()
  • Named layouts with grid-template-areas

Columns, fr Units and Spanning

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Grid</title>
  <style>
    .grid { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 8px; margin-bottom: 10px; }
    .grid div, .auto div { background: #00695c; color: white; padding: 10px; border-radius: 6px; }
    .wide { grid-column: span 2; }
    .auto { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 8px; }
  </style>
</head>
<body>
  <div class="grid"><div>1fr</div><div>2fr</div><div>1fr</div><div class="wide">spans 2 columns</div><div>1fr</div></div>
  <div class="auto"><div>A</div><div>B</div><div>C</div><div>D</div><div>E</div></div>
</body>
</html>

Result

  • display: grid makes a grid container.
  • grid-template-columns: 1fr 2fr 1fr creates three columns. The fr unit means a fraction of the free space, so the middle column is twice as wide as the others.
  • Items fill cells left to right, then move to the next row automatically.
  • grid-column: span 2 makes an item two columns wide.
  • repeat(auto-fit, minmax(100px, 1fr)) fits as many columns of at least 100px as the width allows, then stretches them to fill the row. Narrow your window to watch the second grid reflow.

Page Layout with Named Areas

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Grid areas</title>
  <style>
    body { margin: 0; font-family: sans-serif; }
    .page {
      display: grid;
      grid-template-columns: 150px 1fr;
      grid-template-rows: auto 1fr auto;
      grid-template-areas:
        "header header"
        "sidebar main"
        "footer footer";
      gap: 6px; height: 220px; padding: 6px;
    }
    .page > * { padding: 8px; border-radius: 6px; }
    header  { grid-area: header;  background: #b2dfdb; }
    nav     { grid-area: sidebar; background: #e0f2f1; }
    main    { grid-area: main;    background: #fff8e1; }
    footer  { grid-area: footer;  background: #eceff1; }
  </style>
</head>
<body>
  <div class="page">
    <header>Header</header>
    <nav>Sidebar</nav>
    <main>Main content</main>
    <footer>Footer</footer>
  </div>
</body>
</html>

Result

  • grid-template-areas draws the layout as text. Each quoted string is a row, and repeating a name makes an area span several cells.
  • Each element joins its area with grid-area: header; and so on.
  • To change the layout on phones, redefine the areas in a media query, for example stacking everything in one column.
@media (max-width: 600px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas: "header" "main" "sidebar" "footer";
  }
}

Grid Cheat Sheet

PropertyExample
grid-template-columns200px 1fr, repeat(3, 1fr)
grid-template-rowsauto 1fr auto
gap16px, or row-gap / column-gap
grid-column / grid-rowspan 2, 1 / 3 (from line 1 to line 3)
place-itemscenter centres every item in its cell

Common Mistakes

  1. Area names that do not form rectangles, which makes the whole template invalid.
  2. Confusing grid lines with cells: three columns have four lines, so the full width is 1 / 4 or 1 / -1.
  3. Using auto-fill when you wanted auto-fit: auto-fill keeps empty columns, auto-fit collapses them so items stretch.
  4. Setting display: grid on the wrong element; it belongs on the direct parent of the items.

Practice Exercises

Exercise 1. Create a gallery with 4 equal columns and 10px gaps.

Show answer
.gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

Exercise 2. Make the first gallery item fill the whole first row.

Show answer
.gallery > :first-child { grid-column: 1 / -1; }

Frequently Asked Questions

Can I use Grid and Flexbox together?

Yes, and it is common: Grid for the overall page, Flexbox inside individual components such as a navigation bar or a card’s footer.

Is CSS Grid supported in all browsers?

Yes. All modern browsers have supported CSS Grid since 2017.

Summary

  • display: grid controls rows and columns together.
  • fr shares free space; repeat() avoids repetition.
  • span and line numbers make items cover several cells.
  • auto-fit with minmax() and named areas make responsive layouts simple.

Next: CSS Positioning.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *