What is the best way to create a collage of images with a random and unique arrangement?

Recently, I stumbled upon a website and was intrigued by the unique effect of images randomly appearing on the screen.

I'm curious about how this effect can be achieved. Is it possible to use CSS Grid to divide the screen and designate some grid items as image elements? I admit I am not proficient in CSS and would appreciate if someone could provide examples demonstrating how this can be done.

Answer №1

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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

TextInput in React Native form not registering any user input

I'm facing an issue with typing in the TextInput component. The letter gets typed and then instantly deleted, indicating a problem with state updating. Can anyone provide any insight on this? class myContainerComponent extends Component { constr ...

Utilizing multiple classes in HTML for a button element

  I've recently come across the concept that elements can have multiple classes. It's interesting, isn't it? .rfrsh-btn { background-image:url(../../upload/rfrsh_nb_grey.png); ... } .submit { font-size: 0.85em; paddi ...

Can the Live Search fields be cleared?

In my current project, I am utilizing php and html files with Ajax to display the contents of a MySQL database on a webpage. The main file for displaying the contents is index.php, which retrieves data from fetch.php. I found helpful guidance on how to set ...

Utilize Javascript to acquire the redirected URL

Seeking assistance with making a GET call to an API endpoint that redirects me. I have enabled CORS to access the Location, but struggling to find a way through Ajax or Fetch to retrieve only the Redirect URL. Any help would be highly appreciated. ...

Developing a user-friendly widget for a website that is not optimized for mobile devices

Here's the backstory: I'm currently in the process of creating a mobile-friendly widget for my clients. Unfortunately, they have web pages that are not optimized for mobile devices, and it doesn't seem like that will change anytime soon. Th ...

Is there a way to effortlessly append a slug to my URL in a Node.js platform?

Currently facing challenges with my templates. As I develop an e-commerce platform, I am using a json file containing product details to create cards for each product. Due to the large number of products, creating individual pages for each one is not feas ...

Preparing data in the Vuex Store efficiently for an AJAX request

Dealing with a Vuex store that holds around 30 fields has been quite the challenge for me over the past couple of days. I've been struggling to properly gather the data before sending it through an AJAX post method. Being aware of the reactivity of Vu ...

The input-group-btn is positioned to the right of the field, appearing to float

The input-group-btn for the targetDate field remains fixed on the right side of the input group despite screen width variations until I introduce the automatically generated CSS from the hottowel generator. I require assistance in identifying which aspect ...

Is there a way to adjust the input type date format to show mm/dd/yy?

My input type is set to "date" with the default format of mm/dd/yyyy. However, I would like to change it to mm/dd/yy. Is there a way to adjust the format of a date input? Date: <input type="date" id="date" name="date" value="mm/dd/yy" required/> ...

Material Design Angular2 Toolbar

I am currently working on designing a toolbar using Material and Angular2. My main challenge is formatting the toolbar in such a way that the search bar appears centered in white, while the "Create Project" button and other buttons are aligned to the right ...

Using JavaScript to create customized checkboxes is a useful way to

I am looking to develop a JavaScript code that saves all the checkboxes selected by a user. When the user clicks on the finish button, the code should display what they have chosen (text within the label). Admittedly, I am unsure of how to proceed and wou ...

The minimum and maximum values specified in the InputProps are not being upheld

I am struggling with enforcing min/max character limits on my form. Here is the code I have: const handleSubmit = (event) => { //Make a network call somewhere event.preventDefault(); console.log("submitted"); } <form onSubmit={ha ...

The fixed div width suddenly switches to 100% without warning

I'm struggling with a basic design issue. My goal is to align the purple div next to the yellow div. Both of these divs are nested inside the white div, and the white div is enclosed within the blue div. When I float the yellow and purple divs to the ...

What is the method to retrieve the class name of an element when the div is created as '<div id 'one' class="element one"></div>'?

I have three elements named one, two, and three, declared as seen below: <div id =container> <div id 'one' class="element one"></div> <div id 'two' class="element two"></div> < ...

Error: The function utils.endOfDay does not exist

I recently started using React and integrated the Material-UI date picker in my project by following the guidelines provided in the documentation. For this implementation, I am utilizing moment.js. Below is the code snippet for the datetimepicker: impor ...

Is it advisable to initiate an AJAX call and allow the browser to cancel the request if needed?

When an AJAX request is made, it typically appears in the network tab in Chrome. However, if a client-based redirect occurs at the same time, the AJAX request may be cancelled. But does this mean that the request still reaches the server and executes as ...

What is the best way to extract a single value from my directive scope?

I am working with an AngularJS directive that has a non-isolated scope, but I have one variable, "isOpen," that needs to be isolated within the directive. Consider the following example: app.directive('myDir', function() { return { r ...

What is the process for customizing the arrow of a <select> dropdown menu exclusively?

Here is the code for a dropdown menu: <select id="dropdown"> <option value="">Go to page...</option> <option value="http://stackoverflow.com">CSS-Tricks</option> <option valu ...

CSS-enabled tabs built with React

Currently, I have a setup with 5 divs and 5 buttons where only one div is visible at a time when its corresponding button is clicked. However, I am looking for suggestions on how to improve the efficiency and readability of my code. If you have any best pr ...

Issues with parsing XML data when using the xml2js library

I am looking to extract and populate values from a large XML dataset into a mySQL table. The XML data structure is as follows: <BMS version="1.0"> <Summaries> <Summary Booking_strId="CWBL00D7CB8J8U" Booking_lngId="466244159" Trans_lngId="4 ...