razki
razki

Reputation: 1229

Adding multiple div boxes under each other and looping

I'm looking to create a rating website however currently I'm struggling to get them to look like so: Desired Result

Currently I have this problem:

Current Position

Here is my html/php:

<div id="center">
<?php
  for ($i = 0; $i<3; $i++) {
    ?><div class="floating-box">No Content</div>
    <div class="floating-boxsmall">No Content</div>
    <?php
  }
?>

Here is my relevant CSS:

   #center {
    position: fixed;
    top: 50%;
    left: 30%;
    margin-top: -100px;
    margin-left: -150px;
}

.floating-box {
    float: left;
    width: 150px;
    height: 75px;
    margin: 10px;
    border: 3px solid #0076be;  
}
.floating-boxsmall {
    float: left;
    width: 150px;
    height: 15px;
    margin: 10px;
    border: 3px solid #0076be;  
}

Any Help is greatly appreciated

Upvotes: 0

Views: 1060

Answers (3)

Gokul Prasad
Gokul Prasad

Reputation: 90

#center{

  margin:0px auto;

}

.floating-box, .floating-boxsmall{
 display:block;
 float:left;
  margin:10px 5px;
  background-color:#ccc;
  text-align:center;  
  padding:20px;

}

.floating-box{
  min-height:100px;
}
<div id="center">
<div class="floating-box">No Content</div>
    <div class="floating-boxsmall">No Content</div>
    <div class="floating-box">No Content</div>
    <div class="floating-boxsmall">No Content</div>
    <div class="floating-box">No Content</div>
    <div class="floating-boxsmall">No Content</div>
    </div>

Here is the fiddle

Upvotes: 0

SG_Rowin
SG_Rowin

Reputation: 622

Wrap .floating-box and .floating-boxsmall with a parent container:

.parent{
  width: 150px;
  margin:10px;
  float:left;
}

Then in your child containers, remove floating and eventually left/right margin because you are assigning them in your parent container:

.floating-boxsmall{
  float:left;
  margin: 10px 0;
}
.floating-box{
  float:none;
  margin:0px; 
}

IN YOUR PHP:

<?php
  for ($i = 0; $i<3; $i++) {?>
    <div class="parent">
      <div class="floating-box">No Content</div>
      <div class="floating-boxsmall">No Content</div>
    </div>
    <?php
  }
?>

Haven't tested this but it should work. You need a parent container for sure. Play with it.

Upvotes: 0

MarioD
MarioD

Reputation: 1703

I would go ahead and wrap your items in a container like so:

<?php for ($i = 0; $i<3; $i++) { ?>
  <div class="floating-item">
    <div class="floating-box">No Content</div>
    <div class="floating-boxsmall">No Content</div>
  </div>
<?php } ?>

Remove the float from box and boxsmall and add it to floating-item instead.

#center {
  position: fixed;
  top: 50%;
  left: 30%;
  margin-top: -100px;
  margin-left: -150px;
}

.floating-item {
  float: left;
}

.floating-box {
  width: 150px;
  height: 75px;
  margin: 10px;
  border: 3px solid #0076be;  
}

.floating-boxsmall {
  width: 150px;
  height: 15px;
  margin: 10px;
  border: 3px solid #0076be;  
}

And to save you from future headaches, don't forget to clear your items:

http://learnlayout.com/clearfix.html

Upvotes: 1

Related Questions