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-swatchlabel with a newvalue, add a.cv-swatch--yourcolorrule that sets--cv-swatch, then add matching.cv-shotimages and.cv-thumbbuttons with the samedata-color. - Add a view: give a new
.cv-shotand.cv-thumbdata-view="3"for every color. --cv-accenton.cv-product: the green of the button, stock note and focus rings.--cv-terracotta,--cv-seafoamand--cv-stone: the swatch fill colors.--cv-fade: the cross-fade duration between photos.aspect-ratioon.cv-stage: change4 / 5to1 / 1for 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.