Piero Virgilio
Piero Virgilio

Reputation: 37

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.

Upvotes: 2

Views: 2303

Answers (4)

R. Bagdi
R. Bagdi

Reputation: 11

Please try this code

<style>
 div {
  border: 1px solid gray;
  width: 33.1%;
 }
.left {
  float: left;
  height: 200px;
}
.right {
  float: left;
  height: 100px;
}
.center {
  margin: 0 auto;
  float:left;
  height: 50px;
}
</style>
<div class="left">left-top</div>
<div class="center">center-top</div>
<div class="right">right-top</div>
<div style="clear:both;"></div>
<div class="left" style="clear:left;">left-bottom</div>
<div class="center">center-bottom</div>
<div class="right" style="clear:right;">right-bottom</div>

Upvotes: 0

Johannes
Johannes

Reputation: 67799

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;
}

Upvotes: 1

dhaval raythatha
dhaval raythatha

Reputation: 84

You can try this one.

Html Code

<div class="left">left-top</div>
<div class="right">right-top</div>
<div class="left">left-bottom</div>
<div class="clearfix"></div>
<div class="right">right-bottom</div>
<div class="center">center-top</div>
<div class="center">center-bottom</div>

Css Code

.left, .right, .center {border: 1px solid red;width: 30%;margin:2px;}
.clearfix{clear:both;}
.left {float:left;}
.right { float:left;}
.center {float:left;}

check fiddle https://jsfiddle.net/Dhavalr/9cyq8tu9/

Upvotes: 2

Jeanclaude
Jeanclaude

Reputation: 58

try using this style:

div {
  border: 1px solid red;
  width: 30%;
  display:inline-block;
}

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



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

Upvotes: 0

Related Questions