A masonry-style grid arranges items like bricks in a wall, filling gaps nicely. It looks neat and uses space well.
Masonry-style grid with columns in 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.
<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>
<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>
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.
<!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>
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.
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
row-cols-3 do in a masonry-style grid?Solution
Step 1: Understand the
Therow-cols-*classrow-cols-3class in Bootstrap sets the number of columns per row to 3, making each column take equal width.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.Final Answer:
It creates a grid with 3 equal-width columns in a row. -> Option BQuick Check:
row-cols-3means 3 columns [OK]
- Confusing rows with columns
- Thinking it sets total grid items
- Assuming it controls item height
Solution
Step 1: Understand responsive classes order
Bootstrap applies classes from smallest to largest breakpoint. Sorow-cols-2sets default columns, androw-cols-lg-4overrides on large screens.Step 2: Check syntax correctness
<div class='row row-cols-2 row-cols-lg-4'>usesrow-cols-2for small/default androw-cols-lg-4for large screens, which is correct syntax.Final Answer:
<div class='row row-cols-2 row-cols-lg-4'> -> Option CQuick Check:
Default small cols then large override =<div class='row row-cols-2 row-cols-lg-4'>[OK]
- Using non-existent classes like row-cols-sm-2 without default
- Reversing breakpoint order
- Mixing up class names
<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?
Solution
Step 1: Analyze
Therow-cols-3andg-3row-cols-3class creates 3 equal columns. Theg-3adds consistent gaps between columns and rows.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.Final Answer:
Three cards side by side in equal-width columns with different heights stacked evenly. -> Option DQuick Check:
row-cols-3 + g-3 = 3 columns with gaps [OK]
- Assuming cards stack vertically by default
- Thinking cards overlap with different heights
- Confusing gaps with padding inside cards
<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?
Solution
Step 1: Check number of columns vs items
Therow-cols-4expects 4 columns per row. Here only 3 items exist, so the last column is empty, causing stretching or alignment issues.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.Final Answer:
Not enoughcolitems to fill 4 columns causes layout issues. -> Option AQuick Check:
Items less than columns can cause layout stretch [OK]
- Assuming gaps fix alignment issues
- Thinking
row-cols-4only works with cards - Replacing
colwith fixed width classes unnecessarily
Solution
Step 1: Understand Bootstrap responsive class order
Bootstrap applies classes from smallest to largest breakpoint. So you start withrow-cols-1for extra small, then override with larger breakpoints.Step 2: Verify class sequence for responsiveness
row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4correctly lists classes from smallest to largest breakpoint:row-cols-1,row-cols-sm-2,row-cols-md-3,row-cols-lg-4.Final Answer:
row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4 -> Option AQuick Check:
Responsive classes ordered smallest to largest =row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4[OK]
- Reversing breakpoint order
- Using only one breakpoint class
- Omitting default column count
Ready to prove it?
Pass the test and this pattern moves unknown
