Component Library

← Back to gallery

Colour swatch radio choices in HTML, CSS and vanilla JS

A copy-paste Colour swatch radio choices in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: radio, colour, swatches, labels, form

Live preview

Source (copy-ready)

<section class="example-radio-color-swatches" data-dcl-scheme="light" aria-labelledby="example-radio-color-swatches-title">
<header><h2 id="example-radio-color-swatches-title">Colour swatch radio choices</h2>
<p>Visual colour swatches always keep written colour names beside the native radio.</p></header>
<div class="parts">
<fieldset>
  <legend>Choose a label colour</legend>
  <div class="swatch-grid">
    <label class="swatch-label"><input type="radio" name="example-radio-color-swatches-colour" value="crimson" checked data-colour><span class="swatch swatch-red" aria-hidden="true"></span><span>Crimson</span></label>
    <label class="swatch-label"><input type="radio" name="example-radio-color-swatches-colour" value="ocean" data-colour><span class="swatch swatch-blue" aria-hidden="true"></span><span>Ocean blue</span></label>
    <label class="swatch-label"><input type="radio" name="example-radio-color-swatches-colour" value="forest" data-colour><span class="swatch swatch-green" aria-hidden="true"></span><span>Forest green</span></label>
    <label class="swatch-label"><input type="radio" name="example-radio-color-swatches-colour" value="violet" data-colour><span class="swatch swatch-purple" aria-hidden="true"></span><span>Violet</span></label>
  </div>
</fieldset>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-radio-color-swatches");
  const instance = Array.from(document.getElementsByClassName("example-radio-color-swatches")).indexOf(root);
  if (instance > 0) {
    const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
    const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
    for (const element of [root, ...root.querySelectorAll("*")]) {
      for (const attr of singleReferences) {
        if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
      }
      for (const attr of tokenReferences) {
        if (element.hasAttribute(attr)) {
          const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
          element.setAttribute(attr, rewritten.join(" "));
        }
      }
      const href = element.getAttribute("href");
      if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
    }
  }
  const preference = matchMedia("(prefers-color-scheme: dark)");
  const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
  preference.addEventListener("change", apply);
  apply();
  const inputs = Array.from(root.querySelectorAll("[data-colour]"));
  const status = root.querySelector("[data-status]");
  function sync() {
    const selected = inputs.find((input) => input.checked);
    for (const input of inputs) input.closest(".swatch-label").toggleAttribute("data-active", input.checked);
    if (selected) status.textContent = selected.closest(".swatch-label").textContent.trim() + " selected.";
  }
  inputs.forEach((input) => input.addEventListener("change", sync));
  sync();
})();
</script></section>
<style>
.example-radio-color-swatches { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-radio-color-swatches a { color: #005c62; }
.example-radio-color-swatches button, .example-radio-color-swatches summary, .example-radio-color-swatches input, .example-radio-color-swatches select, .example-radio-color-swatches textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-radio-color-swatches button:hover, .example-radio-color-swatches summary:hover, .example-radio-color-swatches button[aria-selected="true"], .example-radio-color-swatches button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-radio-color-swatches dialog { color: #11292f; background-color: #ffffff; }
.example-radio-color-swatches [data-note] { color: #11292f; background-color: #e4edef; }
.example-radio-color-swatches [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-radio-color-swatches [data-success] { color: #14532d; background-color: #dcfce7; }
.example-radio-color-swatches [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-radio-color-swatches :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

.example-radio-color-swatches[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-radio-color-swatches[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-radio-color-swatches[data-dcl-scheme="dark"] button, .example-radio-color-swatches[data-dcl-scheme="dark"] summary,
.example-radio-color-swatches[data-dcl-scheme="dark"] input, .example-radio-color-swatches[data-dcl-scheme="dark"] select, .example-radio-color-swatches[data-dcl-scheme="dark"] textarea {
  color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-radio-color-swatches[data-dcl-scheme="dark"] button:hover, .example-radio-color-swatches[data-dcl-scheme="dark"] summary:hover,
.example-radio-color-swatches[data-dcl-scheme="dark"] button[aria-selected="true"], .example-radio-color-swatches[data-dcl-scheme="dark"] button[aria-pressed="true"] {
  color: #11292f; background-color: #8fe1da;
}
.example-radio-color-swatches[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-radio-color-swatches[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-radio-color-swatches[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-radio-color-swatches[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-radio-color-swatches[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-radio-color-swatches[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }

.example-radio-color-swatches, .example-radio-color-swatches * { box-sizing: border-box; min-width: 0; }
.example-radio-color-swatches { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-radio-color-swatches h2, .example-radio-color-swatches h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-radio-color-swatches header p, .example-radio-color-swatches p { margin-block: 0.5rem; }
.example-radio-color-swatches ul, .example-radio-color-swatches ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-radio-color-swatches dl { margin-block: 0.5rem; }
.example-radio-color-swatches dt { font-weight: 700; }
.example-radio-color-swatches dd { margin: 0 0 0.6rem; }
.example-radio-color-swatches fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-radio-color-swatches legend { padding-inline: 0.25rem; font-weight: 700; }
.example-radio-color-swatches button, .example-radio-color-swatches input, .example-radio-color-swatches select, .example-radio-color-swatches textarea {
  min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-radio-color-swatches input, .example-radio-color-swatches select, .example-radio-color-swatches textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-radio-color-swatches input[type="checkbox"], .example-radio-color-swatches input[type="radio"] {
  display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-radio-color-swatches input[type="file"] { padding: 0.45rem; }
.example-radio-color-swatches input[type="range"] { padding-inline: 0; }
.example-radio-color-swatches textarea { min-block-size: 6rem; resize: vertical; }
.example-radio-color-swatches button, .example-radio-color-swatches summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-radio-color-swatches button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-radio-color-swatches table { border-collapse: collapse; inline-size: 100%; }
.example-radio-color-swatches th, .example-radio-color-swatches td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-radio-color-swatches [hidden] { display: none !important; }
.example-radio-color-swatches [data-note], .example-radio-color-swatches [data-error], .example-radio-color-swatches [data-success], .example-radio-color-swatches [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-radio-color-swatches .parts { display: grid; gap: 1rem; }
.example-radio-color-swatches .field, .example-radio-color-swatches .fields { display: grid; gap: 0.4rem; }
.example-radio-color-swatches .fields { gap: 0.75rem; }
.example-radio-color-swatches .actions, .example-radio-color-swatches .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-radio-color-swatches .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-radio-color-swatches .plain-list { margin: 0; padding: 0; list-style: none; }
.example-radio-color-swatches .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-radio-color-swatches .choice input { margin-block-start: 0.25rem; }
.example-radio-color-swatches .choice-stack { display: grid; gap: 0.55rem; }
.example-radio-color-swatches .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-radio-color-swatches .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-radio-color-swatches .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-radio-color-swatches .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-radio-color-swatches .hint { font-size: 0.875rem; }
.example-radio-color-swatches .status { min-block-size: 1.5rem; margin-block: 0; }
.example-radio-color-swatches .two-column, .example-radio-color-swatches .split-layout { display: grid; gap: 1rem; }
.example-radio-color-swatches .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-radio-color-swatches .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-radio-color-swatches .visually-hidden {
  position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
  .example-radio-color-swatches .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .example-radio-color-swatches .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
  .example-radio-color-swatches { padding: 0.75rem; }
  .example-radio-color-swatches .actions, .example-radio-color-swatches .inline-row { align-items: stretch; }
  .example-radio-color-swatches .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-radio-color-swatches * { transition: none !important; } }

.example-radio-color-swatches .swatch-grid { display: grid; gap: 0.55rem; }
.example-radio-color-swatches .swatch-label { display: flex; gap: 0.55rem; align-items: center; border: 1px solid; border-radius: 0.5rem; padding: 0.55rem; }
.example-radio-color-swatches .swatch-label[data-active] { border-width: 3px; padding: calc(0.55rem - 2px); }
.example-radio-color-swatches .swatch { inline-size: 2rem; block-size: 2rem; border-radius: 999px; border: 2px solid; flex: 0 0 auto; }
.example-radio-color-swatches .swatch-red { background-color: #b91c1c; border-color: #7f1d1d; }
.example-radio-color-swatches .swatch-blue { background-color: #1d4ed8; border-color: #1e3a8a; }
.example-radio-color-swatches .swatch-green { background-color: #047857; border-color: #064e3b; }
.example-radio-color-swatches .swatch-purple { background-color: #7e22ce; border-color: #581c87; }
</style>

When to use

Use this for theme or product colour selection where the visual sample genuinely helps. Treat the swatch as decoration and the written name as the actual choice.

Avoid

Do not use colour-only swatches; people who cannot perceive the colour difference still need an unambiguous name.

Accessibility

Each option is a native radio inside its written label, so selection does not depend on hue or the decorative swatch. The selected written label receives a thicker border and the choice is announced in text. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.

Was this useful? Send feedback.