0

Let's say that I have this structure on my website:

.elem {
  display: inline-block;
  border: 2px dotted blue;
  float: left;
}
<div>
  <div class="elem">Some content on 1 line</div>
  <div class="elem">Some content on 3 lines<br>second line<br>third line</div>
  <div class="elem">Some content on 2 lines<br>second line</div>
  <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
  <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
  <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
</div>

Here is what it looks like:

content

Let's say that the browser is big enough so there can be 3 divs in each column. I want the 4th div to appear on the first column and on the second row. I don't know how big the individual divs might be (in height, I can set the width later on) so I can't hardcode a height into each divs.

How can I do this so they would align? Any help would be appreciated.

157 239n
  • 349
  • 3
  • 15

2 Answers2

4

You can play with flex to get the desired results.

.content {
    display: flex;
    flex-flow: row wrap;
}

.elem {
    border: 2px dotted blue;
    width: 200px;
    height: auto;
  }
<div class="content">
    <div class="elem">Some content on 1 line</div>
    <div class="elem">Some content on 3 lines<br>second line<br>third line</div>
    <div class="elem">Some content on 2 lines<br>second line</div>
    <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
    <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
    <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
</div>
Manuel Reyes
  • 260
  • 2
  • 10
-1

You can try inline-flex like this,

.elem {
  border: 2px dotted blue;
  display: inline-flex;
 }
<div>
  <div class="elem">Some content on 1 line</div>
  <div class="elem">Some content on 3 lines<br>second line<br>third line</div>
  <div class="elem">Some content on 2 lines<br>second line</div>
  <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
  <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
  <div class="elem">Some content2 on 3 lines<br>second line<br>third line</div>
</div>
charan kumar
  • 2,119
  • 2
  • 20
  • 26