Intermediate35 minCSSGrid

CSS Grid for Repeated Layouts

Learn when Grid is the better choice for galleries, card collections, and page regions that need rows and columns together.

Learning Objectives

By the end of this lesson, you'll be able to:

  • Recognise when a layout problem is two-dimensional
  • Use CSS Grid for repeated cards, galleries, and menu previews
  • Control columns and spacing from the grid container
  • Use repeat(), minmax(), and auto-fit for flexible repeated layouts
  • Understand Understand when named grid areas can clarify larger page regions

Why This Matters:

Intermediate layout work is about choosing the tool that matches the content. Grid gives you a clear way to arrange repeated content when rows and columns matter together.

Before You Start:

You should be familiar with:

Why Grid Belongs Here

By this point in the builder pathway, you have already worked with sections, reusable CSS patterns, Flexbox, and component-style structures. That matters because Grid works best when you already understand the pieces being arranged.

Use Grid when you need to arrange repeated or area-based content such as:

  • gallery tiles
  • menu item cards
  • feature cards
  • product or service listings
  • larger page regions with a main area and sidebar

Grid vs Flexbox

The simplest distinction is this: Flexbox is strongest for one main direction. Grid is strongest when rows and columns matter together.

Diagram comparing Flexbox as a one-direction layout tool with Grid as a rows-and-columns layout tool for repeated cards.
Use Flexbox when the relationship is mostly one-directional. Use Grid when the layout needs a repeated two-dimensional structure.

Helpful question: am I aligning a small group in one direction, or arranging repeated content across rows and columns?

▦ Checkpoint for Understanding

Pause here and check whether the Grid mental model is clear before you write grid code.

  1. What kind of layout problem is Grid especially good at solving?
  2. Why is Grid usually a better fit than Flexbox for a gallery of repeated cards?
  3. Predict what happens if you force a two-dimensional card layout into Flexbox and manually manage widths.
Show sample answers
  1. Grid is strongest when content needs rows and columns together, such as galleries, repeated card collections, or larger page regions.
  2. Because a gallery usually needs a two-dimensional arrangement where rows and columns work together, not just alignment in one main direction.
  3. The CSS usually becomes more fragile, because you are fighting the layout shape instead of using a tool designed for rows and columns.

How confident are you with this concept?

😕 Still confused | 🤔 Getting there | 😊 Got it! | 🎉 Could explain it to a friend!

Tracks and Gaps

A grid container creates tracks. A column track runs vertically. A row track runs horizontally. The repeated children inside the container become grid items.

.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1.5rem;
}

This example creates three equal columns. The gap property controls the spacing between grid items, which is usually cleaner than putting margins on each card.

Build a Repeatable Card Grid

For a repeated card layout, keep the repeated item structure simple and let the parent grid do the arranging.

<section class="section">
  <div class="container">
    <h2 class="section-title">Featured Dishes</h2>

    <div class="menu-grid">
      <article class="menu-card">...</article>
      <article class="menu-card">...</article>
      <article class="menu-card">...</article>
    </div>
  </div>
</section>

The grid does not replace the card component. It arranges the cards. That separation matters: the card owns its internal design, while the grid owns the relationship between repeated cards.

Make Repeated Layouts Flexible

A fixed three-column grid can work on large screens, but it may become cramped on smaller screens. This is where repeat(), auto-fit, and minmax() help.

.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1.5rem;
}
Diagram showing a repeated card grid adapting from three columns to two columns to one column as available width changes.
The minimum width protects each card from becoming too narrow. The flexible maximum lets each card share the available space.

Read the rule in plain English: make as many columns as will fit, but do not let each column get smaller than 16rem. If there is extra room, let the columns stretch evenly.

Use Named Areas for Larger Page Regions

Grid is also useful for larger page regions, especially when you want the CSS to describe the layout shape clearly. Named areas are not required for every grid, but they can make larger arrangements easier to read.

.feature-layout {
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-template-areas:
    "intro image"
    "details image";
  gap: 2rem;
}

.feature-intro { grid-area: intro; }
.feature-image { grid-area: image; }
.feature-details { grid-area: details; }
Diagram showing named Grid areas for hero text, image, content, aside, and call-to-action regions.
Named areas can make the CSS read like a map of the layout. Use them when the page shape is easier to understand with names than with numbers alone.

Grid Checklist

Before choosing Grid, run through this quick checklist:

  • Does the layout need rows and columns together?
  • Is there a parent container that should control the arrangement?
  • Are the repeated items structurally similar?
  • Would gap be cleaner than margins on each item?
  • Does the layout need to adapt as space changes?
  • Would named areas make the layout clearer?

Keep the tool choice honest: if the content only needs to stack naturally, use normal flow. If it only needs one-direction alignment, use Flexbox. Grid earns its place when the layout shape is two-dimensional.

Guided Practice

Build a flexible card grid

Use Grid to arrange repeated cards while keeping the card component itself separate from the layout container.

Step 1: Start with repeated content

Choose a simple repeated pattern such as menu cards, featured dishes, testimonials, or gallery tiles. Write the HTML as a parent wrapper with several repeated child items.

The parent will become the grid container. The repeated items will become grid items.

💡 Need a hint?
Keep the HTML simple: one wrapper and repeated cards or tiles inside it.
Use semantic elements for the repeated items when possible, such as article for cards.

Step 2: Create the grid container

Add display: grid to the parent wrapper, then define columns and gap. Start with a simple fixed number of columns before making it responsive.

💡 Need a hint?
Use gap for spacing between grid items instead of margins on each card.
Start simple so you can see the effect of each property clearly.

Step 3: Make the grid more flexible

Replace the fixed columns with repeat(auto-fit, minmax(...)) so the layout can create as many useful columns as the available space allows.

💡 Need a hint?
The minmax minimum protects cards from becoming too narrow.
auto-fit lets the grid adapt without needing a media query for every width.

You are on track if you can:

  • ☐ You used one parent grid container with repeated child items
  • ☐ You used gap to control spacing between repeated items
  • ☐ You created a grid that can support rows and columns together
  • ☐ You can explain why Grid fits this layout better than Flexbox

Independent Practice

💪 Independent Practice: Choose a Grid-worthy layout

Now apply Grid to a fresh repeated layout without step-by-step support.

Your Task:

Choose a small repeated layout, such as a gallery, menu preview, feature-card section, or service list. Write the parent grid CSS and enough repeated HTML items to show how the layout works.

Keep the focus on the Grid decision. You are not building a full page.

Requirements:
  • Choose a layout where rows and columns genuinely matter
  • Create one parent grid container with repeated child items
  • Use gap for spacing between items
  • Use either fixed columns, repeat(), or auto-fit/minmax() deliberately
  • Write one sentence explaining why Grid is the right tool
Stretch Goals (Optional):
  • Add a responsive version using repeat(auto-fit, minmax(...))
  • Try a named-area layout for a larger section with text and media

Success Criteria:

CriteriaYou've succeeded if...
Grid-worthy layoutThe learner chooses a repeated or area-based layout where rows and columns genuinely matter.
Clean grid containerThe parent element controls columns and gaps instead of each item carrying scattered spacing rules.
Responsive thinkingThe grid has a plan for narrower screens through flexible tracks, media queries, or a clear stacked fallback.
Tool choiceThe learner can explain why Grid is a better fit than Flexbox or normal flow for this case.

Recap

CSS Grid gives you a clean way to arrange repeated content when rows and columns matter together. It is especially useful for galleries, card collections, menu previews, and larger page regions. The key is to let the parent grid describe the layout while each repeated item keeps its own internal structure.

Lesson Complete: You Can Use Grid for Repeated Layouts

Key Takeaways:

  • CSS Grid is best for layouts where rows and columns matter together.
  • The grid container controls the structure; the repeated children become grid items.
  • gap, repeat(), minmax(), and auto-fit help repeated layouts stay consistent and flexible.
  • Grid is not a replacement for Flexbox. It is a different tool for a different layout shape.

Learning Objectives Review:

Look back at what you set out to learn. Can you now:

  • ✅ Explain when Grid is a better layout choice than Flexbox Check!
  • ✅ Create a repeatable card or gallery grid with rows, columns, and gap Got it!
  • ✅ Use repeat(), minmax(), and auto-fit for flexible repeated layouts Can explain it!
  • ✅ Recognise when named grid areas can make larger page regions easier to understand Could teach this!

If you can confidently answer "yes" to most of these, you're ready to move on!

Think & Reflect:

Grid Thinking

  • Which part of your current project feels most like a true rows-and-columns problem?
  • Where have you previously used Flexbox when Grid would have described the layout more clearly?

Responsive Layout

  • How does a repeatable grid help the layout adapt without rewriting every card?
  • What minimum card width would keep your content readable?

🎯 Looking Ahead:

Next, you will refine reusable components responsively so shared sections and layout patterns continue to work across different screen sizes.

Recommended Next Steps

Continue Learning

Ready to move forward? Continue with the next tutorial in this series:

Responsive Refinement for Reusable Components

Additional Resources

Deepen your understanding with these helpful resources:

Progress tracking is disabled. Enable it in to track your completed tutorials.