Color Swatch Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Let the user pick one color from a set, and submit it with a form. Swatches with the same name form a group, value sets the color of each swatch, and the checked one submits its value.
A checkerboard shows through the translucent last swatch.
The controls resize and reshape the swatches.
<form class="color-swatch-basic"> <div class="swatches" aria-label="Color" role="radiogroup"> <sv-color-swatch aria-label="Ocean" checked name="color" value="#2563eb"></sv-color-swatch> <sv-color-swatch aria-label="Sand" name="color" value="oklch(80% 0.08 80)"></sv-color-swatch> <sv-color-swatch aria-label="Forest" name="color" value="#15803d"></sv-color-swatch> <sv-color-swatch aria-label="Rose" name="color" value="#e11d48"></sv-color-swatch> <sv-color-swatch aria-label="Frosted glass" name="color" value="rgb(37 99 235 / 30%)" ></sv-color-swatch> </div>
<output>color=#2563eb</output></form>
<script type="application/x-typescript" module> const form = document.querySelector<HTMLFormElement>(".color-swatch-basic")!; const output = form.querySelector("output")!;
form.addEventListener("submit", event => event.preventDefault());
form.addEventListener("change", () => { output.textContent = `color=${new FormData(form).get("color")}`; });</script>
<style> .color-swatch-basic { display: grid; gap: 1em; place-content: center; justify-items: center;
.swatches { display: flex; flex-wrap: wrap; gap: 0.75em; justify-content: center; }
output { font-family: monospace; font-size: 0.85em; color: var(--sv-gray-3); } }</style>Custom Check
Show your own mark on the checked swatch. An SVG in the check slot replaces the default check mark, and :state(--checked) scales it in.
Reduced Motion
This animation will not play for you because your system is set to reduce motion.
<div class="color-swatch-check" aria-label="Highlight" role="radiogroup"> <!-- Heart --> <svg class="symbols" aria-hidden="true"> <symbol id="color-swatch-heart" viewBox="0 0 24 24"> <path d="M12 20.5l-1.4-1.3C5.4 14.5 2 11.4 2 7.6 2 4.5 4.4 2 7.5 2c1.7 0 3.4.8 4.5 2.1C13.1 2.8 14.8 2 16.5 2 19.6 2 22 4.5 22 7.6c0 3.8-3.4 6.9-8.6 11.6L12 20.5z" /> </symbol> </svg>
<sv-color-swatch aria-label="Lemon" checked name="highlight" value="#fde047"> <svg class="mark" aria-hidden="true" slot="check"><use href="#color-swatch-heart" /></svg> </sv-color-swatch> <sv-color-swatch aria-label="Mint" name="highlight" value="#86efac"> <svg class="mark" aria-hidden="true" slot="check"><use href="#color-swatch-heart" /></svg> </sv-color-swatch> <sv-color-swatch aria-label="Sky" name="highlight" value="#7dd3fc"> <svg class="mark" aria-hidden="true" slot="check"><use href="#color-swatch-heart" /></svg> </sv-color-swatch> <sv-color-swatch aria-label="Lilac" name="highlight" value="#d8b4fe"> <svg class="mark" aria-hidden="true" slot="check"><use href="#color-swatch-heart" /></svg> </sv-color-swatch></div>
<style> .color-swatch-check { display: flex; gap: 0.75em; justify-content: center;
.symbols { display: none; }
sv-color-swatch { --sv-color-swatch-sz: 2.5em; --sv-color-swatch-bdr-rad: 0.5em; }
.mark { inline-size: 50%; block-size: 50%; fill: #1f2937; scale: 0; transition: scale 200ms cubic-bezier(0.34, 1.56, 0.64, 1); }
sv-color-swatch:state(--checked) .mark { scale: 1; }
@media (prefers-reduced-motion: reduce) { .mark { transition: none; } } }</style>