Discover how to make your photo galleries look perfectly balanced without endless manual tweaks!
Why Masonry-style grid with columns in Bootsrap? - Purpose & Use Cases
Imagine you are creating a photo gallery webpage. You want the pictures to fit neatly in columns, but the images have different heights.
You try to place each image one after another in rows, adjusting their positions manually.
Manually positioning each image is slow and frustrating.
If one image is taller, it leaves awkward gaps below shorter images in the same row.
Every time you add or remove a photo, you must rearrange many others to keep the layout tidy.
A masonry-style grid with columns automatically stacks items in vertical columns, filling gaps naturally.
This means images of different heights fit together like puzzle pieces without manual adjustment.
<div> <img src="img1.jpg" style="position:absolute; top:0; left:0;"> <img src="img2.jpg" style="position:absolute; top:0; left:150px;"> <!-- Manually set positions --> </div>
<div class="container"> <div style="column-count: 3; column-gap: 1rem;"> <div class="mb-3" style="break-inside: avoid;"> <img src="img1.jpg" class="img-fluid w-100"> </div> <div class="mb-3" style="break-inside: avoid;"> <img src="img2.jpg" class="img-fluid w-100"> </div> </div> </div>
You can create beautiful, gap-free galleries that adapt automatically to different image sizes and screen widths.
Online portfolios or product listings where photos vary in height but need to look organized and balanced on any device.
Manually placing items in rows causes gaps and is hard to maintain.
Masonry grids stack items in columns, filling vertical gaps naturally.
Bootstrap's column classes make building masonry-style layouts easy and responsive.
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
