Jump to content

Padding around dynamic generated rectangles


Go to solution Solved by 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

Edited by Monkuar
  • Solution

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.

Edited by Monkuar
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.