2

Can I achieve this layout with flexbox with the below document structure?

Layout

I want the big <img> on the left with two smaller images on the right and wrapping.

This is what I did, with display: flex on gallery-container and flex-wrap.

.container {
  height: 100%;
}

.container .gallery-container {
  background-color: #f6f6f6;
  display: flex;
  flex-wrap: wrap;
  width: 300px;
  align-items: flex-start;
}

.container .gallery-container .gallery-big-image {
  display: block;
  width: 200px;
  height: 200px;
  background: lavender;
}

.container .gallery-container .gallery-small-img {
  display: block;
  width: 100px;
  height: 100px;
  background-color: purple;
}
<div class="container">
  <div class="gallery-container">
    <div class="gallery-big-image">big</div>
    <div class="gallery-small-img">small</div>
    <div class="gallery-small-img">small</div>
    <div class="gallery-small-img">small</div>
    <div class="gallery-small-img">small</div>
    <div class="gallery-small-img">small</div>
  </div>
</div>

(codepen)

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701

2 Answers2

2

The layout is clunky and inefficient with flexbox, for reasons explained here: CSS-only masonry layout

However, the layout is relatively simple and easy with CSS Grid.

No changes to HTML.

.gallery-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, 100px);
  grid-auto-rows: 100px;
  width: 300px;
  background-color: #f6f6f6;
}

.gallery-big-image {
  grid-column: span 2;
  grid-row: span 2;
  background: lavender;
}

.gallery-small-img {
  background-color: purple;
  color: white;
}
<div class="container">
  <div class="gallery-container">
    <div class="gallery-big-image">big</div>
    <div class="gallery-small-img">small 1</div>
    <div class="gallery-small-img">small 2</div>
    <div class="gallery-small-img">small 3</div>
    <div class="gallery-small-img">small 4</div>
    <div class="gallery-small-img">small 5</div>
    <div class="gallery-small-img">small 6 (continues wrapping)</div>    
    <div class="gallery-small-img">small 7 (continues wrapping)</div>        
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

How about using grid layout instead?

.container {
  height: 100%;
}

.gallery-container {
  background-color: #f6f6f6;
  width: 300px;
  height: 100px;
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
}

.gallery-img {
  background: purple;
  width: 100px;
  height: 100px;
}

.gallery-img-large {
  background: lavender;
  width: 200px;
  height: 200px;
  grid-column-start: 0;
  grid-column-end: span 2;
  grid-row-start: 0;
  grid-row-end: span 2;
}
<div class="container">
  <div class="gallery-container">
    <div class="gallery-img-large">big</div>
    <div class="gallery-img">small</div>
    <div class="gallery-img">small</div>
    <div class="gallery-img">small</div>
    <div class="gallery-img">small</div>
    <div class="gallery-img">small</div>
  </div>
</div>
MTCoster
  • 5,868
  • 3
  • 28
  • 49