# Color Swatch Picker

> Round color swatches for product variants, built on radio buttons with a ring around the chosen one.

- Category: [CSS Form Elements](https://copy-css.com/category/forms)
- Tags: swatch, color, variant, radio, product, shop
- Live preview: https://copy-css.com/snippets/color-swatches
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<fieldset class="swatches">
  <legend>Color</legend>
  <label style="--c: #18181b"><input type="radio" name="color" aria-label="Black" checked></label>
  <label style="--c: #d6c3a5"><input type="radio" name="color" aria-label="Sand"></label>
  <label style="--c: #84a98c"><input type="radio" name="color" aria-label="Sage"></label>
  <label style="--c: #1e3a8a"><input type="radio" name="color" aria-label="Navy"></label>
  <label style="--c: #e11d48"><input type="radio" name="color" aria-label="Rose"></label>
</fieldset>
```

## CSS

```css
.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 0;
  padding: 0;
  border: 0;
}

.swatches legend {
  width: 100%;
  margin-bottom: 12px;
  padding: 0;
  color: #3f3f46;
  font: 700 14px system-ui, sans-serif;
}

/* Each label sets its own --c */
.swatches label {
  position: relative;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  transition: box-shadow 0.2s ease;
}

.swatches input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.swatches label:has(:checked) {
  box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--c);
}

.swatches label:has(:focus-visible) {
  outline: 2px solid #6366f1;
  outline-offset: 7px;
}
```

## Notes

- This CSS Color Swatch Picker is 45 lines of pure CSS (0.8 KB) with no JavaScript and no dependencies. It uses transitions, box shadows, Flexbox and the :has() selector.
- the :has() selector: Chrome 105+, Safari 15.4+, Firefox 121+.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
