0

I'm trying to make a grid which should look something like this

what it should look like

but it renders as this

what it actually looks like

here is my HTML code

<div style="display: flex; flex-wrap: wrap">
    <div class="item" style="height: 400px; background-color: #ddd"></div>
    <div class="item" style="height: 200px; background-color: #000"></div>
    <div class="item" style="height: 200px; background-color: #ececec"></div>
    <div class="item" style="height: 700px; background-color: #DC0F0F"></div>
    <div class="item" style="height: 400px; background-color: #B429A2"></div>
    <div class="item" style="height: 200px; background-color: #009EBA"></div>
    <div class="item" style="height: 200px; background-color: #694141"></div>
    <div class="item" style="height: 400px; background-color: #29B436"></div>
    <div class="item" style="height: 200px; background-color: #74B2BD"></div>
    <div class="item" style="height: 700px; background-color: #DCDA0F"></div>
</div>

The item class has a static width: 33%.

I understand that doing something like this in JS is quite possible. But I am looking for a pure CSS solution.

Thanks

Brian
  • 3,850
  • 3
  • 21
  • 37
darthNeophyte
  • 116
  • 1
  • 4

4 Answers4

2

You need to add flex-direction and align-content properties to your wrapper element. and some fixed height.... Check the DEMO

CSS

.wrapper {
  height: 1400px;
  display: flex; 
  flex-wrap: wrap;
  flex-direction: column;
  align-content: flex-start;
}

.item {
  width:33%;
}

HTML

<div class="wrapper">
    <div class="item" style="height: 300px; background-color: #ddd"></div>
    <div class="item" style="height: 200px; background-color: #000"></div>
    <div class="item" style="height: 200px; background-color: #ececec"></div>
    <div class="item" style="height: 700px; background-color: #DC0F0F"></div>
    <div class="item" style="height: 400px; background-color: #B429A2"></div>
    <div class="item" style="height: 200px; background-color: #009EBA"></div>
    <div class="item" style="height: 200px; background-color: #694141"></div>
    <div class="item" style="height: 400px; background-color: #29B436"></div>
    <div class="item" style="height: 200px; background-color: #74B2BD"></div>
    <div class="item" style="height: 700px; background-color: #DCDA0F"></div>
</div>
Ronen Cypis
  • 21,182
  • 1
  • 20
  • 25
2

Your best option is to use Masonry but if you insist on CSS only solution then you should use columns Here is Demo

.wrapper {
 -webkit-column-count: 3; /* Chrome, Safari, Opera */
 -moz-column-count: 3; /* Firefox */
  column-count: 3;
 -webkit-column-gap: 0; /* Chrome, Safari, Opera */
 -moz-column-gap: 0; /* Firefox */
  column-gap: 0;
  width: 100vw;
}

.wrapper div {
  display: inline-block;
  width: 100%;
  vertical-align: top;

}
<div class="wrapper">
    <div class="item" style="height: 400px; background-color: #ddd"></div>
    <div class="item" style="height: 200px; background-color: #000"></div>
    <div class="item" style="height: 200px; background-color: #ececec"></div>
    <div class="item" style="height: 700px; background-color: #DC0F0F"></div>
    <div class="item" style="height: 400px; background-color: #B429A2"></div>
    <div class="item" style="height: 200px; background-color: #009EBA"></div>
    <div class="item" style="height: 200px; background-color: #694141"></div>
    <div class="item" style="height: 400px; background-color: #29B436"></div>
    <div class="item" style="height: 200px; background-color: #74B2BD"></div>
    <div class="item" style="height: 700px; background-color: #DCDA0F"></div>
</div>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • your CSS solution works perfectly. So why do you insist on Masonry? – darthNeophyte Dec 22 '15 at 14:26
  • `:)` i don't insist on Masonry but i think its better solution since it's built for this exact purpose, also support on css columns is like this http://caniuse.com/#search=column. – Nenad Vracar Dec 22 '15 at 14:30
0

Adding a few more flex properties fixed it. I added:

flex-direction: column;
align-content: flex-start;

might not be exactly what you need but you should be able to adjust it to your needs

See the JS Fiddle

CSS Tricks guide to flex-box

thebigtine
  • 191
  • 2
  • 12
-1
<div style="display: flex; width: 100%;">
  <div style="display: flex; flex-direction: column; width: 33%;">
    <div class="item" style="height: 400px; background-color: #ddd"></div>
    <div class="item" style="height: 700px; background-color: #DC0F0F"></div>
  </div>
  <div style="display: flex; flex-direction: column; width: 33%;">
    <div class="item" style="height: 200px; background-color: #000"></div>
    <div class="item" style="height: 400px; background-color: #B429A2"></div>
    <div class="item" style="height: 400px; background-color: #29B436"></div>
  </div>
  <div style="display: flex; flex-direction: column; width: 33%;">
    <div class="item" style="height: 200px; background-color: #ececec"></div>
    <div class="item" style="height: 200px; background-color: #009EBA"></div>
    <div class="item" style="height: 200px; background-color: #74B2BD"></div>
    <div class="item" style="height: 200px; background-color: #694141"></div>
    <div class="item" style="height: 700px; background-color: #DCDA0F"></div>
  </div>
</div>
Sachin Mour
  • 635
  • 7
  • 16