Skip to content

Product Image Gallery with Color Variants

Product detail block whose photo gallery, price and stock label switch when a color swatch is selected, with front and close-up thumbnails.

Also known as: product gallery with swatches, color variant selector, product detail images, product page color picker, ecommerce product gallery

Product Image Gallery with Color Variants live demo

Open in new tab

Pure CSS code (HTML, CSS, JS)

A product detail block where the photo gallery follows the selected color. Choosing a finish swaps the main image, the thumbnails, the price and the stock note. It is built with plain CSS and a small vanilla JavaScript file, and fits online stores selling items in several colors, such as homeware, clothing or accessories.

How it works

The root .cv-product is a two-column grid from 800px: .cv-gallery on the left and .cv-info on the right. On phones the columns stack and the thumbnails move below the photo.

.cv-stage holds every product photo as an img.cv-shot with two data attributes, data-color and data-view. The images are stacked on top of each other, and only the one with .is-active is visible, so switching photos is a short cross-fade with no layout jump. .cv-thumbs contains one button.cv-thumb for every color and view. The script hides the thumbnails of the other colors.

The finish picker is a fieldset of radio inputs inside .cv-swatch labels. Each radio stores its display name, price and stock text in data-cv-label, data-cv-price and data-cv-stock. On change the script calls show(color, '1'), updates .cv-color-name, .cv-price__value and .cv-stock, and writes a message to .cv-status. Clicking a thumbnail calls show() with that view. The Add to cart button is simulated with a short timeout; replace that block with a fetch() call to your cart endpoint.

Customization

  • Add a color: copy one .cv-swatch label with a new value, add a .cv-swatch--yourcolor rule that sets --cv-swatch, then add matching .cv-shot images and .cv-thumb buttons with the same data-color.
  • Add a view: give a new .cv-shot and .cv-thumb data-view="3" for every color.
  • --cv-accent on .cv-product: the green of the button, stock note and focus rings.
  • --cv-terracotta, --cv-seafoam and --cv-stone: the swatch fill colors.
  • --cv-fade: the cross-fade duration between photos.
  • aspect-ratio on .cv-stage: change 4 / 5 to 1 / 1 for square product photos.

The photos come from Unsplash (the close-ups are zoomed crops of the same images). Replace them with your own product shots.

Use cases

  • Product detail pages: choosing a color swatch changes the photos, price and stock label
  • Fashion and home stores: front and close-up thumbnails show each variant clearly
  • Ecommerce templates: price and stock status update for each selected color swatch on the page
  • Retail sites: shoppers compare color variants and photos without ever leaving the product page

Accessibility

The swatches are real radio buttons in a fieldset with a legend, so they work with Tab and the arrow keys and announce the selected finish. Thumbnails are buttons with aria-pressed and a visually hidden .cv-sr label, and the wishlist button toggles aria-pressed. Every photo has descriptive alt text. The .cv-status live region announces color changes and cart updates. Focus rings use :focus-visible, and prefers-reduced-motion removes the fade and scale transitions.

Usage notes

Give each copy a unique radio name and heading id. Without JavaScript the first color still shows correctly.

Product Image Gallery with Color Variants at a glance

Framework
Pure CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
None
Layout
Responsive, works on mobile
Category
Product Details
License
Free for personal and commercial use

Questions about this template

Can I use the Product Image Gallery with Color Variants code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

There is nothing to install. Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

No dependencies. Pure HTML, CSS and JavaScript.

Product Image Gallery with Color Variants – free Pure CSS product detail page template
Product Image Gallery with Color Variants · free Pure CSS product detail page template

More Product Details

Bootstrap Product Page – free Bootstrap 5 product detail page template

Bootstrap Product Page

Bootstrap 5 product details page with image gallery, color and size pickers, quantity stepper, and tabs for description, specifications and customer reviews.

Product Detail with Gallery – free Tailwind CSS product detail page template

Product Detail with Gallery

Dark Tailwind CSS product page section with a thumbnail image gallery, finish options, quantity stepper and add to cart button with loading state.

Back in Stock Request Form for Shopify – free Shopify product detail page template

Back in Stock Request Form for Shopify

Shopify product page section: a notify-me panel that appears only while the selected variant is sold out and sends the request through Shopify's contact form.

Ingredients and Benefits Section for Shopify – free Shopify product detail page template

Ingredients and Benefits Section for Shopify

Shopify section for beauty and food stores: key ingredient cards with icon or image and benefit, a free-from tag row and the full INCI list in a toggle.

Product Information Tabs for Shopify – free Shopify product detail page template

Product Information Tabs for Shopify

Shopify section: product description, details, shipping and care in ARIA tabs with arrow-key support, turning into an accordion on mobile. Tabs can show a page.

Size Guide Popup for Shopify – free Shopify product detail page template

Size Guide Popup for Shopify

Shopify section: a Size guide link on product pages that opens an accessible dialog with a size chart from blocks, a cm / in toggle and how-to-measure notes.