Bird
Raised Fist0
CSSmarkup~5 mins

Grid vs flexbox in CSS

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Grid and Flexbox help arrange things on a webpage neatly. They make layouts easy and flexible without messy code.

When you want to create a full page layout with rows and columns.
When you need to align items in a single row or column.
When you want to build a photo gallery with equal-sized boxes.
When you want items to adjust size and position on different screen sizes.
When you want to center content both horizontally and vertically.
Syntax
CSS
/* Flexbox container */
display: flex;

/* Grid container */
display: grid;

Flexbox works mainly in one direction: row or column.

Grid works in two directions: rows and columns together.

Examples
This arranges items in a row, centered horizontally and vertically.
CSS
/* Flexbox example */
.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
This creates three equal columns with space between them.
CSS
/* Grid example */
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}
Sample Program

This page shows two sections: one uses Flexbox to arrange three boxes in a row with space around them. The other uses Grid to create three equal columns with boxes inside. Both have borders and spacing so you can see the difference clearly.

CSS
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Grid vs Flexbox Example</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 2rem;
    }
    h2 {
      margin-bottom: 0.5rem;
    }
    .flex-container {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      align-items: center;
      border: 2px solid #4a90e2;
      padding: 1rem;
      margin-bottom: 2rem;
      gap: 1rem;
    }
    .grid-container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 1rem;
      border: 2px solid #e24a4a;
      padding: 1rem;
    }
    .box {
      background-color: #f0f0f0;
      border: 1px solid #ccc;
      padding: 1rem;
      text-align: center;
      font-weight: bold;
      user-select: none;
    }
  </style>
</head>
<body>
  <section>
    <h2>Flexbox Layout</h2>
    <div class="flex-container" role="list">
      <div class="box" role="listitem">Item 1</div>
      <div class="box" role="listitem">Item 2</div>
      <div class="box" role="listitem">Item 3</div>
    </div>
  </section>
  <section>
    <h2>Grid Layout</h2>
    <div class="grid-container" role="list">
      <div class="box" role="listitem">Item A</div>
      <div class="box" role="listitem">Item B</div>
      <div class="box" role="listitem">Item C</div>
    </div>
  </section>
</body>
</html>
OutputSuccess
Important Notes

Flexbox is great for simple layouts in one direction, like menus or toolbars.

Grid is better for complex layouts with rows and columns, like entire pages or galleries.

Both work well on small screens and can be combined for powerful designs.

Summary

Flexbox arranges items in a row or column, good for simple linear layouts.

Grid arranges items in rows and columns, good for complex layouts.

Use the right tool depending on how you want your content arranged.

Practice

(1/5)
1. Which CSS layout method is best for arranging items in both rows and columns simultaneously?
easy
A. Flexbox
B. Grid
C. Float
D. Position absolute

Solution

  1. Step 1: Understand layout capabilities

    Flexbox arranges items in one direction: row or column, but not both at once.
  2. Step 2: Identify the method for two-dimensional layout

    Grid allows arranging items in rows and columns simultaneously, making it suitable for complex layouts.
  3. Final Answer:

    Grid -> Option B
  4. Quick Check:

    Two-dimensional layout = Grid [OK]
Hint: Grid = rows + columns; Flexbox = single row or column [OK]
Common Mistakes:
  • Thinking Flexbox can handle rows and columns at the same time
  • Confusing float with layout methods
  • Assuming position absolute arranges items in grid
2. Which of the following is the correct CSS syntax to create a flex container?
easy
A. display: grid;
B. flex-container: true;
C. flex-direction: grid;
D. display: flex;

Solution

  1. Step 1: Identify the property to create flex container

    The CSS property to create a flex container is display: flex;.
  2. Step 2: Check other options for correctness

    display: grid; creates a grid container, not flex. flex-direction: grid; is invalid. flex-container: true; is not a valid CSS property.
  3. Final Answer:

    display: flex; -> Option D
  4. Quick Check:

    Flex container syntax = display: flex [OK]
Hint: Flex container always uses display: flex [OK]
Common Mistakes:
  • Using display: grid instead of flex
  • Trying to set flex-direction to grid
  • Using non-existent CSS properties
3. Given the CSS below, what will be the layout of the items inside .container?
.container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.item {
  width: 100px;
  height: 50px;
  background-color: lightblue;
}
medium
A. Items arranged horizontally with 1rem gap
B. Items arranged in a grid with 1rem gap
C. Items arranged vertically with 1rem gap
D. Items stacked with no gap

Solution

  1. Step 1: Analyze flex container properties

    The container uses display: flex; and flex-direction: column;, so items stack vertically.
  2. Step 2: Understand gap effect

    The gap: 1rem; adds space between items vertically in column direction.
  3. Final Answer:

    Items arranged vertically with 1rem gap -> Option C
  4. Quick Check:

    flex-direction: column + gap = vertical spacing [OK]
Hint: flex-direction: column stacks items vertically [OK]
Common Mistakes:
  • Assuming flex defaults to row direction
  • Ignoring the gap property
  • Thinking items form a grid with flex
4. Identify the error in this CSS code if the goal is to create a 3-column grid layout:
.grid-container {
  display: flex;
  grid-template-columns: repeat(3, 1fr);
}
medium
A. Using flex display with grid-template-columns property
B. Missing grid-gap property
C. Incorrect repeat syntax in grid-template-columns
D. Missing width on container

Solution

  1. Step 1: Check display property

    The container uses display: flex;, which does not support grid-template-columns.
  2. Step 2: Understand property compatibility

    grid-template-columns works only with display: grid;. Mixing flex display with grid properties causes no effect or errors.
  3. Final Answer:

    Using flex display with grid-template-columns property -> Option A
  4. Quick Check:

    grid-template-columns requires display: grid [OK]
Hint: Grid properties need display: grid, not flex [OK]
Common Mistakes:
  • Mixing flex display with grid properties
  • Assuming repeat syntax is wrong
  • Forgetting to set display property
5. You want to create a responsive photo gallery with rows and columns that adjust automatically. Which CSS approach is best and why? A developer tries this CSS:
.gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.photo {
  flex: 1 1 200px;
  height: 150px;
}
Why might CSS Grid be a better choice here?
hard
A. Grid allows explicit control of rows and columns, making layout predictable and consistent.
B. Flexbox automatically creates rows and columns without extra code.
C. Grid requires less CSS code than flexbox for any layout.
D. Flexbox does not support gaps between items.

Solution

  1. Step 1: Analyze flexbox gallery approach

    Flexbox with wrapping creates rows but columns depend on item size and wrapping, which can be uneven and unpredictable.
  2. Step 2: Compare with grid advantages

    Grid lets you define explicit rows and columns, so the gallery layout is consistent and easier to control across screen sizes.
  3. Final Answer:

    Grid allows explicit control of rows and columns, making layout predictable and consistent. -> Option A
  4. Quick Check:

    Responsive rows + columns = Grid best choice [OK]
Hint: Use Grid for predictable rows and columns in galleries [OK]
Common Mistakes:
  • Assuming flex-wrap creates perfect grid layouts
  • Thinking Grid always uses less CSS
  • Believing flexbox cannot have gaps