One way to creatively position images is by using random numbers. You can use the following CSS code to determine their placement:
position:absolute; top: x px; left: y px;
To achieve this, generate random values for x
and y
with a random number generator in JavaScript. Then, place each image within a div
container and apply the CSS rules.
This sample code illustrates how images can be randomly positioned on a page.
You can further enhance the logic to prevent overlapping of images.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="main"></div>
<script>
images = ['airplane.png', 'boat.png', 'fruits.png', 'tulips.png', 'pool.png']
$(document).ready(() => {
images.forEach((image, index) => {
var img = $('<img width="100px" height="100px" id="img-' + index + '" src="https://homepages.cae.wisc.edu/~ece533/images/' + image + '">');
img.attr('style', 'position:absolute;top:' + getRandomNumber() + 'px;left:' + getRandomNumber() + 'px');
img.appendTo('#main');
});
function getRandomNumber() {
return Math.ceil(Math.random() * 600);
}
});
</script>