ProtoTyPus
ProtoTyPus

Reputation: 1324

Divs don't float how I would

I've 5 divs in a single div, that I would make it floats 2 at left and 3 at right:

.container {
  height: 400px;
  border: 1px solid black;
}
.first, .second, .third, .fourth, .fifth {
  width: 50%;
}

.first {
  height: 300px;
  background-color: red;
  float: left;
}

.second {
  height: 100px;
  background-color: blue;
  float: left;
}

.third {
  height: 100px;
  background-color: green;
  float: right;
}

.fourth {
  height: 200px;
  background-color: yellow;
  float: right;
}

.fifth {
  height: 100px;
  background-color: aquamarine;
  float: right;
}
<div class="container">
    <div class="first"> FIRST </div>
    <div class="second"> SECOND </div>
    <div class="third"> THIRD </div>
    <div class="fourth"> FOURTH</div>
    <div class="fifth"> FIFTH </div>
</div>

I would like that they were placed:

FIRST and SECOND at left
THIRD, FORTH and FIFTH at right.
Instead, they are placed like:
FIRST and FIFTH at left
SECOND, THIRD and FOURTH at right.

Do you know how fix it? Here there is my code: https://jsfiddle.net/82bkdbpn/

Upvotes: 2

Views: 61

Answers (2)

Matthias Seifert
Matthias Seifert

Reputation: 2073

You can wrap the divs in Columns, and float these two. Or you can use flexbox, there is a very good answer from Nenad Vracar about this.

Here is an example with two column wrapper div elements:

.container {
  height: 400px;
  border: 1px solid black;
}

.col {
  width: 50%;
  float: left;
}

.first {
  height: 300px;
  background-color: red;
}

.second {
  height: 100px;
  background-color: blue;
}

.third {
  height: 100px;
  background-color: green;
}

.fourth {
  height: 200px;
  background-color: yellow;
}

.fifth {
  height: 100px;
  background-color: aquamarine;
}
<div class="container">
  <div class="col">
    <div class="first"> FIRST </div>
    <div class="second"> SECOND </div>
  </div>
  <div class="col">
    <div class="third"> THIRD </div>
    <div class="fourth"> FOURTH</div>
    <div class="fifth"> FIFTH </div>
  </div>
</div>

Upvotes: 0

Nenad Vracar
Nenad Vracar

Reputation: 122047

Since you have defined fixed height on container element you can use Flexbox to do this and define flex-direction: column.

.container {
  height: 400px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
.first, .second, .third, .fourth, .fifth {
  width: 50%;
  height: 100px;
}
.first {
  height: 300px;
  background-color: red;
}
.fourth {
  height: 200px;
  background-color: yellow;
}
.second {background-color: blue;}
.third {background-color: green;}
.fifth {background-color: aquamarine;}
<div class="container">
  <div class="first"> FIRST </div>
  <div class="second"> SECOND </div>
  <div class="third"> THIRD </div>
  <div class="fourth"> FOURTH</div>
  <div class="fifth"> FIFTH </div>
</div>

Upvotes: 4

Related Questions