LESSON 4.7
Building a JavaScript Photo Gallery
Create an interactive image gallery with minimal code
Put your HTML, CSS, and a little JavaScript together to build a clickable photo gallery, a great feature for any portfolio or photography site.
💡 The Concept
A photo gallery brings HTML, CSS, and JavaScript together in a single project with an immediate, visible result. You do not need to understand JavaScript to build this one. The JavaScript is a single statement repeated once per thumbnail, and the only editing it requires is swapping out image file names. The concept section explains exactly what each part does so you can make those edits with confidence.
Here is what the gallery looks like:

You can try the finished gallery in JSFiddle before reading the code: https://jsfiddle.net/smetoxen/9d4jrr3n
How It Works
The gallery has two parts:
- A large preview image at the top that displays the selected photo.
- A row of small thumbnail images below it. Each thumbnail has an
onclickattribute. When a visitor clicks a thumbnail, one line of JavaScript swaps the preview image’ssrcto match the thumbnail that was clicked.
Exercise: Try It Yourself
Step 1. Open the page where you want to add the gallery. If you do not have a page yet, copy your website template and rename the copy baby-animals.html. You will be adding the gallery code inside the <main> section of that page.
Step 2. Copy this HTML into your page. The images point to a live URL so the gallery works immediately when you open it in the browser:
<div class="gallery">
<!-- Main preview image -->
<div class="preview">
<img id="preview" src="http://whitebuffalosolutions.com/WB-Template/images/photo1.jpg"
alt="Gallery preview" height="600" width="800" />
</div>
<!-- Thumbnail row -->
<div class="gallery-thumbnails">
<img onclick="document.getElementById('preview').src = document.getElementById('img1').src;" id="img1" src="http://whitebuffalosolutions.com/WB-Template/images/photo1.jpg" alt="Photo 1" />
<img onclick="document.getElementById('preview').src = document.getElementById('img2').src;" id="img2" src="http://whitebuffalosolutions.com/WB-Template/images/photo2.jpg" alt="Photo 2" />
<img onclick="document.getElementById('preview').src = document.getElementById('img3').src;" id="img3" src="http://whitebuffalosolutions.com/WB-Template/images/photo3.jpg" alt="Photo 3" />
<img onclick="document.getElementById('preview').src = document.getElementById('img4').src;" id="img4" src="http://whitebuffalosolutions.com/WB-Template/images/photo4.jpg" alt="Photo 4" />
<img onclick="document.getElementById('preview').src = document.getElementById('img5').src;" id="img5" src="http://whitebuffalosolutions.com/WB-Template/images/photo5.jpg" alt="Photo 5" />
<img onclick="document.getElementById('preview').src = document.getElementById('img6').src;" id="img6" src="http://whitebuffalosolutions.com/WB-Template/images/photo7.jpg" alt="Photo 6" />
</div>
</div>
Two things to notice:
- The main preview image has
id="preview". This is how JavaScript finds it. - Each thumbnail has a unique
id(img1,img2, etc.) and anonclickattribute containing JavaScript.
Step 3. Copy this CSS into your stylesheet:
.gallery .preview {
width: 100%;
height: auto;
}
.gallery .gallery-thumbnails {
margin: 0 auto;
text-align: center;
}
.gallery .gallery-thumbnails img {
width: 100px;
height: 75px;
margin: 3px 10px 10px 0;
}
.gallery .gallery-thumbnails img:hover {
opacity: .8;
cursor: pointer;
}
.gallery .preview img#preview {
padding: 1px;
max-width: 100%;
height: auto;
margin: 0 auto;
display: block;
}
Step 4. Save and open the page in your browser. Click each thumbnail — the gallery should work right away.
Assignment: Switch to the Baby Animal Gallery
Now replace the URL images with the baby animal photos. The zip below contains six photos already cropped, resized, and optimized at 800 × 600 px using the Images 1-2-3 steps.
Download: baby-animals.zip
Step 5. Add the downloaded image files to your images folder.
Step 6. In each <img> tag in the HTML, find the src= attribute — that is the part that tells the browser where to find the image. Replace the long URL with a local path pointing to your downloaded file. For example, the first thumbnail currently reads:
src="http://whitebuffalosolutions.com/WB-Template/images/photo1.jpg"
Change it to:
src="images/elephant.jpg"
Do this for the preview image and all six thumbnails, using this list:
- Preview image and
img1:images/elephant.jpg img2:images/macsque.jpgimg3:images/monkey.jpgimg4:images/penguin.jpgimg5:images/swan.jpgimg6:images/turtle.jpg
Step 7. Update the alt text on each image to describe the animal.
Step 8. Save and test. The gallery should now show your baby animal photos.
Using Your Own Images
When you are ready to use your own photos in a gallery, run each image through the Images 1-2-3 process before adding it:
- Crop each photo to a 4:3 ratio so all your gallery images are the same shape.
- Resize to 800 × 600 px to match the dimensions set in the HTML.
- Optimize with TinyPNG to keep file sizes small and the page fast.
Then update the src path and alt text for each <img> tag. Make sure the preview src and the first thumbnail src point to the same photo so the gallery looks correct when the page loads.
❓ Frequently Asked Questions
onclick is an HTML attribute that runs JavaScript when the element is clicked. The value of the attribute is the JavaScript code to execute. In this gallery, each onclick contains one statement that updates the preview image. It is the simplest way to add interactivity to a page without a separate script file.
The onclick JavaScript uses document.getElementById() to locate a specific element by its id. If two thumbnails shared the same id, the JavaScript would always find the first one, and clicking the second would swap in the wrong photo. Every id on a page must be unique.
Yes. Add another <img> tag inside the .gallery-thumbnails div, give it a unique id (img5, img6, and so on), and copy the onclick pattern from an existing thumbnail, updating the id to match the new one. There is no limit to the number of thumbnails.
When the page loads, the preview image shows whatever src you gave it in the HTML. If that does not match the first thumbnail, the gallery looks inconsistent before any clicking happens — the visitor sees a preview photo that does not correspond to any highlighted thumbnail. Starting both on the same image keeps the initial state clean.
★ Key Takeaways
A photo gallery is a great way to see JavaScript in action. This lesson walked you through building a simple gallery where clicking a thumbnail updates the main image on the page. Next, you will learn how to design a simple logo using HTML and CSS. A clean logo makes a site look polished and professional right away.
Google Ads
The costs of this website are partially offset by revenue from Google Ads.Tools of the Trade
HTML/CSS Lessons
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
