From the HTML perspective, an image gallery is a web page consisting of a section containing multiple images organized in a specific format. Its purpose is to showcase a collection of photographs, product images, artwork, etc.
Below is an example of creating an image gallery vias HTML. To make it a bit more interactive, CSS and JavaScript is also added.
Here’s given the explanation of all the files being added in the code above:
<div>
element with class
name of image_gallery
.<img>
tags, each of them loading randomly generated images from the lorem picsum website.<div>
element of id
of expanded_img
.<span>
element with a class
name of close
; this is used to create a close button. Finally, an <img>
tag with a class
name of img_content
and id
of image
; this is to display the image.Lines 1–6:
showImage
, takes one argument—src
as the image source.id
via document.getElementById()
method, which are:
expanded_img
images
block
.src
to src
as the argument passed in the showImage
function.Lines 8–11
closeImage
gets the element expanded_img
by id
via document.getElementById()
method.display
property to none
.Free Resources