在彼此下面添加多个 div 框并循环

Adding multiple div boxes under each other and looping

我想创建一个评级网站,但目前我正在努力让它们看起来像这样:Desired Result

目前我遇到的问题是:

Current Position

这是我的 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
  }
?>

这是我的相关 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;  
}

非常感谢任何帮助

我会继续将您的物品包装在容器中,如下所示:

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

boxboxsmall 中删除浮点数并将其添加到 floating-item

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

为了避免您以后遇到麻烦,请不要忘记清理您的物品:

http://learnlayout.com/clearfix.html

用父容器包装 .floating-box.floating-boxsmall

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

然后在您的子容器中,删除浮动并最终删除 left/right 边距,因为您在父容器中分配它们:

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

在你的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
  }
?>

尚未对此进行测试,但应该可以。您肯定需要一个父容器。一起玩吧。

#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>

这是fiddle