Skip to content

Colour and size variants in HTML and CSS

A colour and size picker for a product page: the visual, price and stock follow the choice, and sold-out sizes are disabled.

Here is an excerpt of this component's code in HTML and CSS. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in HTML and CSS

index.html
<!doctype html>
<html lang="fr">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Variantes de couleur et de taille</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="demo">
      <form class="pvc" data-pvc data-stock='{"ecru":{"S":6,"M":2,"L":0,"XL":4},"marine":{"S":0,"M":7,"L":5,"XL":1},"rouille":{"S":3,"M":0,"L":2,"XL":0}}' aria-labelledby="pvc-titre">
        <div class="pvc-visual" style="--pvc-color: #1f2f52" data-pvc-visual>
script.js
document.querySelectorAll("[data-pvc]").forEach((root) => {
  const on = (el, type, fn) => el.addEventListener(type, fn);
  const form = root;
  const stock = JSON.parse(form.dataset.stock);
  const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });
  const field = (name) => form.querySelector(`[data-pvc-${name}]`);
  const picked = (name) => form.querySelector(`input[name="pvc-${name}"]:checked`);
  const colorName = (input) => input.closest("label").textContent.trim();

  const update = () => {
    const color = picked("couleur");
    const left = stock[color.value];
style.css
:root {
  --pvc-card: #ffffff;
  --pvc-text: #18202f;
  --pvc-muted: #4b5568;
  --pvc-line: #cfd6e0;
  --pvc-visual: #eef2f7;
  --pvc-ring: #1f2f52;
  --pvc-ring-bg: #ffffff;
  --pvc-accent: #1f2f52;
  --pvc-accent-text: #ffffff;
  --pvc-low: #9a3412;
  --pvc-selected: #eef2f7;

Free component: copy it freely, including for commercial projects.

E-commerce components in HTML and CSS
  • A limited-time offer bar: countdown to a fixed date, one-click copyable promo code and a fallback message once the offer is over.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A sales card showing the discount that grows with the cart amount (-10% from €50, -15% from €100…) with a progress bar towards the next tier.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A Christmas gift guide filtered by budget and theme, with the number of ideas announced and an empty state offering to show everything again.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • The last order dates for delivery before Christmas, per shipping method, with the days left worked out from the visitor’s date.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A cart that shows live how much is left to add for free shipping, with a progress bar and recalculated totals.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A wishlist for an online shop: add from product cards, a summary with total and removal, remembered in the browser.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component