Multiple Image Viewer w/ Thumbnails
Tags: Beginner
Here is a technique that I use when creating product detail pages commonly seen in most e-commerce sites. I use some simple javascript for the image switch and use CSS to crop the thumbnail images.
Lets get started!
First let’s identify the different elements that make up a typical product image viewer:
- Brand Logo
- Large (Main) Product View
- Thumbnails
HTML
Let’s start by creating a container for our image viewer and placing the logo and main three large product images inside.
<div class="photos"> <img src="Triumvir.gif" alt="Triumvir3.com" class="logo" /> <img src="ARTofWAR_BLK(3).jpg" alt="Black" /> <img src="ARTofWAR_RED.jpg" alt="Red" /> <img src="ARTofWAR_WHT.jpg" alt="White" /> </div>
Wrap each product image with a <div> and assign an Id of “Photo_ (# -number goes here)”>. Note that this ID will increment for each image used. Also add an inline style of “display: none;” on the images that should be hidden by default.
<div class="photos"> <img src="Triumvir.gif" alt="Triumvir3.com" class="logo" /> <div id="photo_1"><img src="ARTofWAR_BLK(3).jpg" alt="Black"/></div> <div id="photo_2" style="display:none;"><img src="ARTofWAR_RED.jpg" alt="Red" /></div> <div id="photo_3" style="display:none;"><img src="ARTofWAR_WHT.jpg" alt="White" /></div> </div>
Now we will be adding the thumbnails using a list.
<div class="photos"> <img src="Triumvir.gif" alt="Triumvir3.com" class="logo" /> <div id="photo_1"><img src="ARTofWAR_BLK(3).jpg" alt="Black"/></div> <div id="photo_2" style="display:none;"><img src="ARTofWAR_RED.jpg" alt="Red" /></div> <div id="photo_3" style="display:none;"><img src="ARTofWAR_WHT.jpg" alt="White" /></div> <ul class="thumbs"> <li><a href="javascript:void(0)" onclick="switch_product_img('photo_1', 3);"><img src="ARTofWAR_BLK(3).jpg" alt="Black" /></a></li> <li><a href="javascript:void(0)" onclick="switch_product_img('photo_2', 3);"><img src="ARTofWAR_RED.jpg" alt="Red" /></a></li> <li><a href="javascript:void(0)" onclick="switch_product_img('photo_3', 3);"><img src="ARTofWAR_WHT.jpg" alt="White" /></a></li> </ul> </div>
Note that for every “switch_proiduct_img” function in the onclick property, the
photo_# parameter needs to increment by one for each image.
CSS
In this example, I am centering the product viewer container for showcasing purposes only. In your scenario most likely it will be left aligned or right aligned next to the product name, price, description, and add to cart button. You may have to adjust this property accordingly to your scenario.
.photos {
overflow: hidden;
border: 10px solid #f0f0f0;
padding: 10px;
width: 400px;
margin: 0 auto;
}
We will center align the logo with the container and products.
.photos img.logo {margin: 0 auto; display:block;}
Give the main product view image some white space with a light border at the bottom.
.photos div img {
padding: 10px 0;
margin: 20px 0;
float: left;
border-bottom: 1px solid #ddd;
}
Cropping the Thumbnails
What we will be doing, is taking our original large preview image and only showing a portion of the image to create the thumbnail effect. We are not actually shrinking the image but simply cropping it. We will add this effect using the overflow: hidden; property.
.photos ul.thumbs {
margin: 0;
padding: 0 0 0 10px;
list-style: none;
width: 390px;
float: left;
}
.photos ul.thumbs li{
width: 110px;
height: 100px;
margin: 0 10px 0 0;
padding: 0;
float: left;
overflow: hidden;
position: relative;
border: 5px solid #ddd;
text-align: center;
}
.photos ul.thumbs li img {
position: absolute;
top: -180px;
left: -145px;
}
Lets finish this off by adding a hover effect for the modern browsers.
.photos ul.thumbs li:hover {
border: 5px solid #888;
-moz-opacity:.75;
filter:alpha(opacity=75);
opacity:.75;
}
Javascript Image Swap
Now we will add this simple javascript to make our switch.
<script language="javascript" type="text/javascript">
function switch_product_img(divName, totalImgs) {
for (var i=1; i<=totalImgs; i ) {
var showDivName = 'photo_' i;
var showObj = document.getElementById(showDivName);
if (showDivName == divName)
showObj.style.display = 'block';
else
showObj.style.display = 'none';
}
}
</script>
Conclusion
This is a simple and smooth way to do some simple image swapping for the product detail pages. One downside of the CSS Cropping technique is that depending on the kind of image (how much white space it has, the positioning of the shot in the image, random width/height sizes, etc), it may not always crop well. Especially if you have a large site with multiple sized images, you may find it difficult to always get the best cropping position. If anyone has a better suggestion or a solution to clear this issue, I would love to hear about it~
Related Posts
Author Bio
My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!
You can learn more about me or
Follow me on twitter for more updates and resources!
Did You Enjoy This Post?
Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.




+ Add Comment43 Peeps Have Spoken Their Minds...
Hmm.. I’ve used a similar (ish) technique before; I like the way you’ve done it, although, I’m a fan of unobtrusive scripting.
So if I was to do that, I’d just have the full size images (with possible captions) as my html:
and then I’d use say.. jQuery.. in order to add the thumbs:
$(“document”).ready(function(){
$(“.photos”).append(”)
$(“.photos”).find(“img”).each(function(){
$(this).hide();
$(this).parent().find(“.thumbs”).append(this).attr({
height: “100px”,
width: “100px”
}).bind(“click”, function(){
$(this).parent().find(“img”).hide().is(‘[src='+$(this).attr("src")+']‘).show();
});
});
$(“.photos > img:first”).show();
});
(please note, the JavaScript isn’t tested.)
So, basically, i’d add the thumbnails only to the Audience to which they could be relevant; which is those users with JavaScript, otherwise, we just show the list of full size images. I’d probably use very similar css however.
Nice Tutorial anyhow.. Those are just my two bobs worth on it.
… okay, HTML didn’t like posting.
Micheil, thanks for your advice and input!~ You have a very good point about not neglecting users w/out js, Ill be looking into your sample code :-) Thanks!
Nice work – this method will work with any size image:
<div style="border:solid 5px #006f50; padding:5px; background-color:#d8e9cd; width:150px; height:150px; position:relative; text-align:center;overflow:hidden;" >
<!-- Centred inside a width of 150px with 5px padding all the way round -->
<div style="width:1000px; height:1000px;line-height:1000px;text-align:center; top:-420px; left:-420px; position:absolute; overflow:hidden;">
<img src='http://www.google.co.uk/intl/en_uk/images/logo.gif' style="vertical-align:middle;"> <!-- You need spaces to make it work in Firefox -->
</div>
</div>
You should see a 150px square with a centered image cropped inside it
This works well for me showing member photos where I don’t know the size or ratio of the contact photos and I want it to be a standard size for layout.
Simply replace the ImageURL with one of your own and check that it is centre-cropped.
Dominic, thanks for your input! Your vertical align effect seems to take care of my alignment issue :-) I will go ahead and implement it to my other projects now, thank you very much~
Hi there, thanks for the clear tutorial. The only problem I come across is the fact the images of the thumbs won’t appear in the designated full size area… Is it possible the javascript of my navigation is interfering with the image gallery script? Thanks in advance!
I can def take a look~ Do you have a link Daan?
Hi Soh,
Solved the problem! Will continue editing the rest of the site now… Anyway thanks for the offer!
Hello Soh,
Unfortunately I find another problem. I can’t get the thing working properly if I have multiple image galleries on a single page. Would you mind taking a look? http://www.praktijk.io/test.html Thanks!
D
Tremendous help, thank you for posting this! Not trying to look a gift horse in the mouth, but here’s an enhancement I’m (unsuccessfully so far) trying to build off of it if you have any ideas. I’m trying to have a dropdown where the pictures change depending on the state of the dropdown. For example, in your demo have a dropdown that has shirts as one option and pants as another. When you change it from shirts to pants, the thumbnails and main pic all change to a set of pants pics. Just thought I’d throw it out in case you ever wanted an enhancement!
Awesome! When you have the link please share it, I’m always looking to improve my skills :-)
Hello Soh
I have the same problem as daan
Everything is working fine, until i press the thump to swap picture. Nothing happens.
The problem is with this line:
var showDivName = ‘photo_’ i;
it should have a ‘+’ in between ‘photo_’ and i. Like this:
var showDivName = ‘photo_’ + i;
Hello Soh
I’m looking for something very similar to this image viewer, but calling the images from a database instead of listing them, as this can constantly change and having a forward and back button either side of the tumbnail images. Can you help?
If i list the produst list code can someone help me place the multiple images coding in there because i have NO clue where to place it. Prostore is a little complicated for me.
.addedToCart {
background-color:#FFFAF4;
border:1px solid #E2A269;
margin-bottom:15px;
font-family:;
font-size:px;
color:#333333
}
.addedToCart A:link, .addedToCart A:visited, .addedToCart A:hover, .addedToCart A:active{
font-family:;
font-size:px;
color:#333333
}
This item was added to your cart
Photo Not Available
This item selling at eBay:
Listing Expires:
:
SKU:
N/A
ISBN:
MPN:
UPC:
Unit:
:
:
:
:
:
:
Price:
Price:
List Price: – Savings:
Price:
()
This item is out of stockin stock
Sale Ends:
E-mail a friend about this item.
Return to Catalog
I would appreciate with any help at all. Thanks.
nm the codes didn’t appear as i copied it. It won’t show up in this form
Can someone please check out my website and look at the coding in the produt detail page and explain to me what i can do to get multiple images. Thank you.
DulcetCouture.com
Amy
Hi,
Your code for showing the images is nice, i used it and looks good
Good one, galleries can be useful at times especially for projects etc
Thanks for posting them :)
Excellent information. Finding a few such solutions has taken hours in the past.
Thank u.
I like this app, I think I will add one. It seems to load fast, unlike some of the other image shows I have seen.
Thanks for the wonderful tutorial.
Do you know how to make this work for drop-downs instead of image thumbnails? I gave it a try, but it doesn’t work in Internet Explorer (typically…)
Hey Soh – nice script – just wondering, how would you make the images fade into one another on change?
Figured it myself actually, just change to:
if (showDivName == divName)
$(showObj).fadeIn(‘normal’);
else
$(showObj).fadeOut(‘normal’);
And set the main images positions to absolute.
Keep up the good work!
thanks for the great script Soh, for someone just starting to tinker with Javascript it is an easy & understandable.
I, like Sam, wanted to try out a fade. I understand what he is getting at but have experimented and am not fully getting it. if you or Sam could help out or or even just post the full updated JS that worked for Sam so that I can compare and see how the two combine that would be greatly appreciated.
thanks again!
test page is here: http://www.souldesigngroup.com/client/miguel/show
Hi! Thanks for the tutorial. It’s really usefull!
But I have a this problem:
I would like that moreover the thumb changes the big images, the link go to a place in the web site with a named anchor.
The code”
For example, in firefox this code run but in chrome or safari the last thumb doesn’t run. You have to click two times and then it’s ok.
Thank very much
Sorry but i forgot the code:
a href=”javascript:void(0)” onclick=”switch_product_img(‘photo_1′, ’5′); href=’#obra’”></a
Hi Soh,
if I want to duplicate and list 3 multiple-image-viewer in the same page, how can I do it?
E.g.
Product 1 swap -> red, black, blue,white etc
Product 2 swap -> red, black, blue,white etc
Product 3 swap -> red, black, blue,white etc
The gist is something like abercrombie’s, where products have different thumbnails to swap over, all in a single page.
Imagine I have 3 products in a gallery page now, each product has swappable thumbnails in different colors.
The problem I am facing now is when I click on product 2 thumbnails, Product 1 main images turn hidden. How could the conditional statement be re-written?
Thanks if anyone can provide some light. Many thanks.
Hi Soh
Thanks for the great tutorial, I’m having real issues getting my product image, logo and thumbnails to center in the containing div which I’ve called ‘artwork’ instead of ‘photos’. If you could take a look I’d be very grateful, I suspect there’s a previous style interfering but I can’t figure it out.
thanks so much, I even got it working in WordPress.
one question: is it possible to use preformatted thumbnails? And if, so how to code the code? I want to use a cropped 75*75 px version of the main pic…
tnx in advance!
Here is a solution to the cropping problem of the thumbnail images; it will resize the image to fit into the div:
.photos ul.thumbs li img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
Is there a way to ensure that only when the thumbnail is clicked the high quality image is loaded, as i am using much smaller thumbnails in place of the full size image to speed up the loading of the page?
Thank you very much. This works great. I used the functionality to show thumbnails that when clicked popped up a lightbox of the image.
Hi,
I am having a problem that I need some guidance on.
For some reason, the image won’t display after there are three images. When you click on the fourth image thumbnail, nothing happens.
I’ve cut and pasted the code exactly, and just named it “photo_4″.
Why does it do this?
Thank you!!!
Wait, I just figured it out!
If anyone else has this problem, just fix this part:
“switch_product_img(‘photo_1′, 4) (the number “4″ tells the coding how many photos to view fully)
The full code is below.
<img src="ARTofWAR_BLK(3).
Its really very nice. Thanks for sharing it
Thanks so much for posting this viewer, I like how it looks. Mine doesn’t work though, the images and thumbs show up fine but they do not swap when clicked on.
I also cannot figure out how to get a bit of space between the large image and the thumbs, and also between the thumbs themselves without everything moving down or to the right (when I adjust the margin or padding pixels). My images need to be left and top justified and aligned as shown, with just 10 px in between everything. How can I adjust the css for this please?
http://www.kilograph.net/samples.html
Thank you
sorry, I posted the wrong path to the site, this one is correct:
http://www.kilograph.net/test/samples.html
thanks!
how ca i add two or three galleries in the same page?
Great tutorial. I like Sam would like to be able to fade in the images. I tried his suggestion but could not get it to work. Any suggestions?
Great tutorial. Do you happen to know how to add text that swaps when you click the thumbnail so could put in available sizes and other product info next to each image that swaps when the image does.
Speak Your Mind...