Bird
Raised Fist0
Bootsrapmarkup~5 mins

Masonry-style grid with columns in Bootsrap

Choose your learning style10 modes available
Introduction

A masonry-style grid arranges items like bricks in a wall, filling gaps nicely. It looks neat and uses space well.

Displaying photos of different heights in a gallery
Showing blog posts with varying content lengths
Organizing product cards with different descriptions
Creating a portfolio with projects of different sizes
Syntax
Bootsrap
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
  <div class="col">
    <div class="card">...</div>
  </div>
  <!-- more columns -->
</div>

Use row-cols-* classes to set how many columns appear on different screen sizes.

The g-3 class adds consistent spacing (gutters) between items.

Examples
Two columns per row with small gaps between items.
Bootsrap
<div class="row row-cols-2 g-2">
  <div class="col">Item 1</div>
  <div class="col">Item 2</div>
  <div class="col">Item 3</div>
</div>
One column on small screens, four columns on medium and larger screens.
Bootsrap
<div class="row row-cols-1 row-cols-md-4 g-4">
  <div class="col">Item A</div>
  <div class="col">Item B</div>
  <div class="col">Item C</div>
  <div class="col">Item D</div>
</div>
Sample Program

This example uses Bootstrap's row-cols-* classes to create a responsive masonry-style grid. Cards have different heights to show how the grid arranges them in columns with gaps.

Bootsrap
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Masonry Grid Example</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
  <style>
    .card {
      border: 1px solid #ccc;
      border-radius: 0.5rem;
      padding: 1rem;
      background-color: #f8f9fa;
    }
  </style>
</head>
<body>
  <main class="container my-4">
    <h1 class="mb-4">Masonry-style Grid with Columns</h1>
    <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
      <div class="col">
        <div class="card" style="height: 150px;">Short content</div>
      </div>
      <div class="col">
        <div class="card" style="height: 250px;">A bit taller content block to show variation.</div>
      </div>
      <div class="col">
        <div class="card" style="height: 100px;">Small block</div>
      </div>
      <div class="col">
        <div class="card" style="height: 200px;">Medium height content here.</div>
      </div>
      <div class="col">
        <div class="card" style="height: 180px;">Another block with different height.</div>
      </div>
      <div class="col">
        <div class="card" style="height: 130px;">Short again.</div>
      </div>
    </div>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Bootstrap's grid does not create a true masonry layout but arranges items in equal-width columns with gaps.

For a true masonry effect (items stacked tightly), you might need JavaScript libraries like Masonry.js or CSS Grid with special properties.

Use responsive row-cols-* classes to adapt the number of columns on different screen sizes.

Summary

Masonry-style grids arrange items in columns with gaps, making uneven heights look neat.

Bootstrap's row-cols-* classes help create responsive column layouts easily.

Cards or items with different heights show how the grid fills space nicely.

Practice

(1/5)
1. What does the Bootstrap class row-cols-3 do in a masonry-style grid?
easy
A. It sets the grid to have 3 rows and 3 columns.
B. It creates a grid with 3 equal-width columns in a row.
C. It adds 3 rows to the grid.
D. It makes each item span 3 columns.

Solution

  1. Step 1: Understand the row-cols-* class

    The row-cols-3 class in Bootstrap sets the number of columns per row to 3, making each column take equal width.
  2. Step 2: Apply to masonry-style grid

    In a masonry grid, this means items will be arranged in 3 columns, adjusting height but keeping column count fixed.
  3. Final Answer:

    It creates a grid with 3 equal-width columns in a row. -> Option B
  4. Quick Check:

    row-cols-3 means 3 columns [OK]
Hint: Remember row-cols sets number of columns per row [OK]
Common Mistakes:
  • Confusing rows with columns
  • Thinking it sets total grid items
  • Assuming it controls item height
2. Which of the following is the correct Bootstrap syntax to create a responsive masonry grid with 4 columns on large screens and 2 columns on small screens?
easy
A. <div class='row row-cols-4 row-cols-sm-2'>
B. <div class='row row-cols-lg-4 row-cols-sm-2'>
C. <div class='row row-cols-2 row-cols-lg-4'>
D. <div class='row row-cols-4 row-cols-lg-2'>

Solution

  1. Step 1: Understand responsive classes order

    Bootstrap applies classes from smallest to largest breakpoint. So row-cols-2 sets default columns, and row-cols-lg-4 overrides on large screens.
  2. Step 2: Check syntax correctness

    <div class='row row-cols-2 row-cols-lg-4'> uses row-cols-2 for small/default and row-cols-lg-4 for large screens, which is correct syntax.
  3. Final Answer:

    <div class='row row-cols-2 row-cols-lg-4'> -> Option C
  4. Quick Check:

    Default small cols then large override = <div class='row row-cols-2 row-cols-lg-4'> [OK]
Hint: Place smaller breakpoint class before larger breakpoint class [OK]
Common Mistakes:
  • Using non-existent classes like row-cols-sm-2 without default
  • Reversing breakpoint order
  • Mixing up class names
3. Given this Bootstrap markup:
<div class='row row-cols-3 g-3'>
  <div class='col'><div class='card'>Short</div></div>
  <div class='col'><div class='card'>A bit taller content</div></div>
  <div class='col'><div class='card'>Tallest card content here</div></div>
</div>

What will the visual layout look like in the browser?
medium
A. Cards overlapping because of different heights.
B. Three cards stacked vertically in one column.
C. Cards arranged in two columns with uneven widths.
D. Three cards side by side in equal-width columns with different heights stacked evenly.

Solution

  1. Step 1: Analyze row-cols-3 and g-3

    The row-cols-3 class creates 3 equal columns. The g-3 adds consistent gaps between columns and rows.
  2. Step 2: Understand card heights in masonry

    Cards have different heights but Bootstrap arranges them side by side in columns with gaps, so they do not overlap but appear stacked neatly.
  3. Final Answer:

    Three cards side by side in equal-width columns with different heights stacked evenly. -> Option D
  4. Quick Check:

    row-cols-3 + g-3 = 3 columns with gaps [OK]
Hint: row-cols sets columns; g-* sets gaps for neat spacing [OK]
Common Mistakes:
  • Assuming cards stack vertically by default
  • Thinking cards overlap with different heights
  • Confusing gaps with padding inside cards
4. You wrote this Bootstrap grid:
<div class='row row-cols-4'>
  <div class='col'>Item 1</div>
  <div class='col'>Item 2</div>
  <div class='col'>Item 3</div>
</div>

But the items appear stretched and not aligned in columns as expected. What is the likely cause?
medium
A. Not enough col items to fill 4 columns causes layout issues.
B. Missing the g-* class to add gaps between columns.
C. The row-cols-4 class only works with cards, not plain divs.
D. The col class must be replaced with col-4 for columns.

Solution

  1. Step 1: Check number of columns vs items

    The row-cols-4 expects 4 columns per row. Here only 3 items exist, so the last column is empty, causing stretching or alignment issues.
  2. Step 2: Understand Bootstrap behavior with fewer items

    Bootstrap fills columns evenly, but with fewer items than columns, the layout may look uneven or stretched.
  3. Final Answer:

    Not enough col items to fill 4 columns causes layout issues. -> Option A
  4. Quick Check:

    Items less than columns can cause layout stretch [OK]
Hint: Match number of items to columns for balanced layout [OK]
Common Mistakes:
  • Assuming gaps fix alignment issues
  • Thinking row-cols-4 only works with cards
  • Replacing col with fixed width classes unnecessarily
5. You want to create a masonry-style grid using Bootstrap that adapts to screen size: 1 column on extra small, 2 columns on small, 3 columns on medium, and 4 columns on large screens. Which is the best class combination to achieve this?
hard
A. row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4
B. row row-cols-lg-4 row-cols-md-3 row-cols-sm-2 row-cols-1
C. row row-cols-4 row-cols-3 row-cols-2 row-cols-1
D. row row-cols-1 row-cols-2 row-cols-3 row-cols-4

Solution

  1. Step 1: Understand Bootstrap responsive class order

    Bootstrap applies classes from smallest to largest breakpoint. So you start with row-cols-1 for extra small, then override with larger breakpoints.
  2. Step 2: Verify class sequence for responsiveness

    row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4 correctly lists classes from smallest to largest breakpoint: row-cols-1, row-cols-sm-2, row-cols-md-3, row-cols-lg-4.
  3. Final Answer:

    row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4 -> Option A
  4. Quick Check:

    Responsive classes ordered smallest to largest = row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4 [OK]
Hint: Order responsive classes from smallest to largest breakpoint [OK]
Common Mistakes:
  • Reversing breakpoint order
  • Using only one breakpoint class
  • Omitting default column count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard