3

Is there a way to make my block-elements behave like in this example:


Desired effect:

|----|----|
|1111|2222|
|1111|----|
|1111|3333|
|----|----|
|4444|
|----|

Actual result:

|----|----|
|1111|2222|
|1111|----|
|1111|
|----|----|
|3333|4444|
|----|----|

.wrapper {
  background-color:red;
  width:220px;
}

.block {
  background-color:blue;
  height:100px;
  width:100px;
  
  display:inline-block;
  vertical-align:top;
  margin:4px;
}

.long {
  height:200px;
}
<div class="wrapper">
  <div class="block long">
  </div>
  <div class="block">
  </div>
  <div class="block">
  </div>
  <div class="block">
  </div>
</div>
Randy
  • 9,419
  • 5
  • 39
  • 56

4 Answers4

1

I think best option for this is to use masonry plugin.

$('.wrapper').masonry({
  columnWidth: 1,
  itemSelector: '.block'
});
.wrapper {
  background-color:red;
  width:220px;
}

.block {
  background-color:blue;
  height:100px;
  width: 100px;
  display: inline-block;
  margin:4px;
}

.long {
  height:180px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/masonry-layout@4.1.1/dist/masonry.pkgd.min.js"></script>
<div class="wrapper">
  <div class="block long"></div><div class="block"></div><div class="block"></div><div class="block"></div>
</div>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0
<div class="wrapper">
  <div>
    <div class="block long">

    </div>
    <div class="block">
    </div>
  </div>
  <div>
    <div class="block">
    </div>
    <div class="block">
    </div>
  </div>
</div>

this will have the desired effect with the same css

0

You can add to the .wrapper class,

column-count: 2;
column-gap: 0px;

.wrapper {
  background-color:red;
  width:220px;
  column-count: 2;
  column-gap: 0px;
}

.block {
  background-color:blue;
  height:100px;
  width:100px;
  
  display:inline-block;
  vertical-align:top;
  margin:4px;
}

.long {
  height:200px;
}
<div class="wrapper">
  <div class="block long">
  </div>
  <div class="block">
  </div>
  <div class="block">
  </div>
  <div class="block">
  </div>
</div>
Nick G
  • 1,953
  • 12
  • 16
0

use float:left to block properties

.wrapper {
  background-color:red;
 display: inline-block;
width: 220px;
}

.block {
  background-color:blue;
  height:100px;
  width:100px;
float: left;
  
vertical-align:top;
  margin:4px;
}

.long {
  height:200px;
}
<div class="wrapper">
  <div class="block long">
  </div>
  <div class="block">
  </div>
  <div class="block">
  </div>
  <div class="block">
  </div>
</div>
Jordi Flores
  • 2,080
  • 10
  • 16
  • 1
    This works for my example, but then if you have more elements there will be white-space on the left instead of on the right ;) – Randy Oct 19 '16 at 14:38