On this page11 sections
  1. Learning Objectives
  2. 1. Your First Grid
  3. 2. The fr Unit and repeat()
  4. 3. Spanning Cells
  5. 4. A Responsive Grid With No Media Queries
  6. 5. Named Areas: a Whole Page
  7. 6. Grid or Flexbox?
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 051: Grid: The Container

Category8: Flexbox & Grid
Lecture051 of 057
Estimated video length16 min
TargetCSS3 · any modern browser
LevelIntermediate

Flexbox handles one line at a time. Grid handles rows and columns together: photo galleries, card layouts, whole pages with a header, sidebar and footer. You draw the grid on the parent, and the children fill it in.


Learning Objectives

  • Create columns and rows with grid-template-columns and grid-template-rows
  • Use the fr unit and repeat()
  • Make items span several columns or rows
  • Build a responsive card grid with auto-fit and minmax()
  • Lay out a page with grid-template-areas

1. Your First Grid

Example

html
<style>
  .grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
  .grid div { background: #4c6ef5; color: white; padding: 20px; text-align: center; border-radius: 6px; }
</style>

<div class="grid">
  <div>1</div><div>2</div><div>3</div>
  <div>4</div><div>5</div><div>6</div>
</div>

1fr 1fr 1fr = three equal columns. Items fill the cells in order, starting a new row when one is full.


2. The fr Unit and repeat()

fr means "one share of the free space".

You writeColumns
1fr 1frTwo equal halves
2fr 1frTwo thirds + one third
200px 1frA fixed 200px column + the rest
repeat(4, 1fr)Four equal columns (short for 1fr 1fr 1fr 1fr)

Example: a sidebar and content:

html
<style>
  .layout { display: grid; grid-template-columns: 160px 1fr; gap: 12px; }
  .layout > * { padding: 16px; border-radius: 6px; }
  aside { background: #fff3bf; }
  main  { background: #e7f5ff; }
</style>

<div class="layout">
  <aside>Sidebar (160px)</aside>
  <main>Main content takes the rest</main>
</div>

3. Spanning Cells

Example

html
<style>
  .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .grid div { background: #12b886; color: white; padding: 18px; border-radius: 6px; }
  .wide { grid-column: span 2; background: #f76707 !important; }
  .tall { grid-row: span 2; background: #ae3ec9 !important; }
</style>

<div class="grid">
  <div class="wide">spans 2 columns</div>
  <div class="tall">spans 2 rows</div>
  <div>3</div><div>4</div><div>5</div>
</div>

4. A Responsive Grid With No Media Queries

repeat(auto-fit, minmax(160px, 1fr)) means: "fit as many columns as you can, each at least 160px wide, and share any leftover space". Drag the preview's corner to see the columns change.

Example

html
<style>
  .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
  .card  { border: 1px solid #dee2e6; border-radius: 10px; padding: 14px; }
</style>

<div class="cards">
  <div class="card"><strong>HTML</strong><p>Structure</p></div>
  <div class="card"><strong>CSS</strong><p>Style</p></div>
  <div class="card"><strong>JavaScript</strong><p>Behaviour</p></div>
  <div class="card"><strong>Flutter</strong><p>Apps</p></div>
</div>

This one line is behind most card layouts on the web.


5. Named Areas: a Whole Page

Name the areas, then draw the layout as text.

Example

html
<style>
  .page {
    display: grid;
    grid-template-columns: 140px 1fr;
    grid-template-areas:
      "header header"
      "side   main"
      "footer footer";
    gap: 8px;
  }
  .page > * { padding: 14px; border-radius: 6px; }
  header { grid-area: header; background: #364fc7; color: white; }
  aside  { grid-area: side;   background: #fff3bf; }
  main   { grid-area: main;   background: #e7f5ff; min-height: 120px; }
  footer { grid-area: footer; background: #343a40; color: white; }
</style>

<div class="page">
  <header>Header</header>
  <aside>Sidebar</aside>
  <main>Main content</main>
  <footer>Footer</footer>
</div>

6. Grid or Flexbox?

Use Flexbox when…Use Grid when…
Items go in one line (a nav bar, a button row)You need rows and columns together
Item sizes come from their contentThe layout decides the sizes

They work well together: a Grid page layout, with Flexbox inside each card.


Common Beginner Mistakes

1. display: grid on the wrong element. Like Flexbox, it goes on the parent.

2. Percent columns plus gap. 33% 33% 33% plus a gap overflows. 1fr columns subtract the gap for you.

3. Area names that don't form rectangles. Each named area must be one rectangle in grid-template-areas.

4. Forgetting grid-area on the children. The template alone doesn't place anything.


Practice Questions

  1. Write the CSS for a grid with four equal columns and a 16px gap.
  2. What does 1fr mean?
  3. How do you make one item stretch across three columns?
  4. What does repeat(auto-fit, minmax(200px, 1fr)) do?
  5. Give an example of a layout better suited to Grid than Flexbox.
  6. Write grid-template-columns for a 250px sidebar followed by flexible content.

Practice Answers

  1. .grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
  2. One share of the free space left in the grid.
  3. grid-column: span 3;
  4. It fits as many columns as possible, each at least 200px wide, and stretches them to fill the row.
  5. A photo gallery, a card grid, or a page with header, sidebar, content and footer.
  6. grid-template-columns: 250px 1fr;

Key Takeaways

  • display: grid + grid-template-columns draws the grid; children fill it in order.
  • fr shares free space; repeat() saves typing.
  • span stretches items; grid-template-areas lays out whole pages.
  • repeat(auto-fit, minmax(…, 1fr)) gives responsive cards in one line.