You should have an algorithm for determining the placement of the next element, taking into account the set of existing elements and the bounding box (width and height of the container). I would start with plain English and just write it down first with a pen and paper - it's easier than a code.
jQuery width height, , , css top/left. position "absolute", position "relative". . jQuery API.
, , calculateOffset(element, container) (, {left: x, top: y}) jQuery elements jQuery container, - , :
elements.each(function() {
$(this).css(calculateOffset($(this), container));
});
, , calculateOffset. - , , ( top) , , ( left), .