Piero Virgilio Piero Virgilio - 2 months ago 6
CSS Question

div on 3 columns using float

I'm trying to place 6 divs with different height on 3 columns.

I use float property for divs on the left and on the right and margin: 0 auto for central divs.

Using clear property I placed second row of divs under the first one, but I want each div is under the div with the same float option without blank space between them.

Instead they are aligned the lowest div.

Here's the fiddle: fiddle



div {
border: 1px solid red;
width: 30%;
}
.left {
float: left;
height: 200px;
}
.right {
float: right;
height: 100px;
}
.center {
margin: 0 auto;
height: 50px;
}

<div class="left">left-top</div>
<div class="right">right-top</div>
<div class="left" style="clear:left">left-bottom</div>
<div class="right" style="clear:right">right-bottom</div>
<div class="center">center-top</div>
<div class="center">center-bottom</div>





Thanks for help,

Piero.

Answer

Put them in 3 columns/DIVs 33.33% wide which you float:

https://jsfiddle.net/8Lbc5pq7/4/

HTML:

<div class="column">
<div class="left">left-top</div>
<div class="left">left-bottom</div>
</div>
<div class="column">
<div class="center">center-top</div>
<div class="center">center-bottom</div>
</div>
<div class="column">
<div class="right">right-top</div>
<div class="right" style="clear:right">right-bottom</div>
</div>

CSS:

div {
  border: 1px solid red;
  width: 95%;
}
.column {
  float: left;
  border: none;
  width: 33.33%;
}

.left {
  float: left;
  height: 200px;
}

.right {
  float: right;
  height: 100px;
}

.center {
  margin: 0 auto;
  height: 50px;
}
Comments