CSS Color Swatch Picker
Round color swatches for product variants, built on radio buttons with a ring around the chosen one.
<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>
.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;
}
About this CSS Color Swatch Picker
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. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- transitions
- box shadows
- Flexbox
- the :has() selectorChrome 105+, Safari 15.4+, Firefox 121+.
- the :checked state (no JavaScript)
- keyboard focus styles
- custom properties
Customize it
The colors in this snippet. Search and replace them to match your brand:
#3f3f46#fff#6366f1
How to use this color swatch picker
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.swatchesclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.