3

I've been trying to build a CSS only image gallery for my website. However, the columns are not displayed correctly and I couldn't find a reason for it.

This is what I see.

This

Both Chrome and FF generated the same view for me. When I change the resolution a bit it turns into this.

this

But it has a very small range, so when I shrink the page a few more pixels, the boxes go back to their initial positions (first image).

This is the CSS I'm using:

.media-container {
  column-count: 6;
  column-gap: 3%;
}

.media-container .item {
  margin-bottom: 20px;
  background-color: grey;
  display: inline-block;
}

.media-container .item img {
  width: 100%;
  opacity: 0;
}
<div class="media-container">
  <div class="item"><img src="image1.jpg" /></div>
  <div class="item"><img src="image2.jpg" /></div>
  <div class="item"><img src="image3.jpg" /></div>
  <div class="item"><img src="image4.jpg" /></div>
  . . .
</div>

What is the reason for this and how can I fix it?

2
  • 1
    @Rounin It's still the same when I zero it. Commented Apr 11, 2021 at 17:35
  • Fair enough, @Eddy88. That was my best guess from just looking at the CSS. But it's clearly not that. Keep persevering! Commented Apr 11, 2021 at 17:41

1 Answer 1

2

I would recommend using CSS grid in this case if possible:

.media-container { 
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 3%;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, it works better but for some reason the last item is clipped. Is it an expected issue when using grid or is my outer css in interfering? Edit: Okay, figured it out by using grid-template-rows. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.