Jump to content

Padding around dynamic generated rectangles


Monkuar

Recommended Posts

Here is my code:

for($i = 1; $i <= 30; $i++){
 
$pos1 = mt_rand(1,900);
$pos2 = mt_rand(1,100);
 
echo '<div class="itemLootBox Unique fadeIn" onclick="lootItem(this)" style="position:absolute;left:'.$pos1.'px;top:'.$pos2.'px">Lunar Rock</div>';
 
}

ffc89e21188adb41666bcf5be405e364.png

 

 

The dimensions of that container is:

Width: 980px

Height: 130px.

 

These correspond with $pos1 and $pos2 respectively.  I reduced 980 to 900 for the mt_rand because the boxes look weird when they get positioned to the edge. 

 

And my problem is, the divs are being randomly generated, but sometimes overlap others. Is there a way to add a function / if statement so I can check against $pos1, and $pos2 to add spacing so the boxes will never overlap? (They don't have to never overlap, but some type of padding would be way better).

 

Edit: Try to think of it as a grid like system, and each rectangle has it's own box of it's 'own' if you will. Wheres there is a padding around each grid. Not sure how to do this

Found the solution!

 

Sorry for the double post, but I cannot edit my post? :P

 

Anyways:

for($i = 1; $i <= 30; $i++){
 
$pos1 = mt_rand(1,30);
$pos2 = mt_rand(1,30);
 
echo '<div class="itemLootBox Unique fadeIn" onclick="lootItem(this)" style="left:'.$pos1.'px;top:'.$pos2.'px">Lunar Rock</div>';
 
}

I simply added FLOAT:LEFT to my .itemLootBox container in my css! Then gave it a 30 top, and left padding for each float :) Works great now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.