Creating a random photos section using HTML and JavaScript involves dynamically fetching and displaying random images each time the page loads or a button is clicked. For this example, we’ll use the Unsplash API to fetch random images. You’ll need an Unsplash API access key, which you can get by signing up at Unsplash Developer.
Explanation
- HTML Structure:
- The HTML structure includes a container with a title, a div with the ID
photo-gridfor displaying the photos, and a button for loading more photos.
- The HTML structure includes a container with a title, a div with the ID
- CSS Styling:
- The
.photo-gridclass uses CSS Grid to create a responsive grid layout for the photos. - The
imgelements within the grid are styled with rounded corners and a box shadow. - The
buttonis styled for a modern look with hover effects.
- The
- JavaScript Functionality:
- The
accessKeyvariable stores your Unsplash API access key. - When the DOM content is loaded, the
loadRandomPhotosfunction is called to fetch and display random photos. - The
loadPhotosButtonis set up with an event listener to load more photos when clicked. - The
loadRandomPhotosfunction fetches 10 random photos from the Unsplash API and appends them to thephoto-griddiv.
- The
Replace 'YOUR_UNSPLASH_ACCESS_KEY' with your actual Unsplash API access key. This script fetches random photos from Unsplash and displays them in a grid layout, allowing users to load more photos by clicking the button.


+91 7905834592
Enquiry Now
piyushmnm@gmail.com
piyush.gupta384
Reviews
There are no reviews yet. Be the first one to write one.