简体   繁体   English

在foreach()函数中,DIV相互重叠

[英]Inside foreach() function the DIV is overlapping each other

I have a foreach() function written inside which there is a DIV container showing colors. 我在里面写了一个foreach()函数,里面有一个显示颜色的DIV容器。 Its working fine but they are overlapping each other. 它的工作正常,但彼此重叠。 How can I make this appear side by side with some gap say 10 pixels. 如何使它并排出现一些间隙,例如10像素。

Here is the code 这是代码

<?php foreach($mbs_colors as $color){ ?>
   <div class="membership-indicator" style="background: <?php echo $color; ?>; margin-top: 4px;"></div>
<?php } ?>

I also tried the following code but it only separates all from the 1st element and 2nd, 3rd, 4th etc overlaps each other making it visual appearance of only two elements. 我还尝试了以下代码,但它仅将所有内容与第一个元素分开,第二个,第三个,第四个等相互重叠,从而仅显示两个元素的外观。

<?php $j = 0; foreach($mbs_colors as $color){ ?>
   <div class="membership-indicator" style="background: <?php echo $color; ?>; margin-top: 4px; <?php if($j++ != 0){ echo "margin-left: 15px"; } ?>"></div>
<?php } ?>

An example is here of what I exactly want to do. 我确切想要做的就是一个例子。

CSS CSS

.membership-indicator {
  width: 12px;
  height: 12px;
  position: absolute;
}

You need to set display property to inline-block 您需要将display属性设置为inline-block

 .membership-indicator{ width: 12px; height: 12px; position: relative; display: inline-block; } 
 <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> <div class="membership-indicator" style="background: red; margin-top: 4px;"></div> 

You can do it with css. 您可以使用CSS做到这一点。

.membership-indicator {
  height: 12px;
  width: 12px;
  float: left;
  margin-right: -10px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM