Shopify Color Swatches Generator
Turn a Color option into clickable swatches on the product page or collection cards.
Your details
Your snippet file (snippets/color-swatches.liquid)
{%- comment -%}
Color swatches for the product page. Place this inside your {% form 'product', product %} block.
Clicking a swatch updates the form's own hidden variant id field.
{%- endcomment -%}
{%- liquid
assign target_option = null
for option in product.options_with_values
if option.name == "Color"
assign target_option = option
endif
endfor
-%}
{%- if target_option -%}
{%- style -%}
.ea-swatches{display:flex;flex-wrap:wrap;gap:.5rem;align-items:flex-start;list-style:none;margin:0;padding:0}
.ea-swatch{display:flex;flex-direction:column;align-items:center;gap:.25rem;cursor:pointer;background:none;border:0;padding:0;font:inherit;color:inherit;text-decoration:none}
.ea-swatch__dot{width:28px;height:28px;border-radius:50%;border:1px solid rgba(0,0,0,.15);box-shadow:0 0 0 2px #fff,0 0 0 3px transparent;background-size:cover;background-position:center;transition:box-shadow .15s ease}
.ea-swatch[aria-current="true"] .ea-swatch__dot,.ea-swatch:hover .ea-swatch__dot{box-shadow:0 0 0 2px #fff,0 0 0 3px #111}
.ea-swatch[data-unavailable] .ea-swatch__dot{opacity:.35}
.ea-swatch__label{font-size:11px;color:#555}
{%- endstyle -%}
<ea-color-swatches class="ea-swatches" data-fallback='{"black":"#111111","white":"#ffffff","navy":"#1b2a4a","olive":"#6b6b4a","sand":"#d8c7a5","red":"#b3352c"}'>
{%- for value in target_option.values -%}
<button type="button" class="ea-swatch" data-value="{{ value.name | escape }}" data-variant-id="{{ value.variant.id }}" aria-current="{{ value.selected }}"{% unless value.available %} data-unavailable{% endunless %} aria-label="{{ value.name | escape }}">
<span class="ea-swatch__dot" style="{% if value.swatch.color %}background-color: {{ value.swatch.color }};{% elsif value.swatch.image %}background-image: url({{ value.swatch.image | image_url: width: 64 }});{% endif %}"></span>
</button>
{%- endfor -%}
</ea-color-swatches>
<script>
if (!customElements.get('ea-color-swatches')) {
customElements.define('ea-color-swatches', class extends HTMLElement {
connectedCallback() {
var fallback = {};
try { fallback = JSON.parse(this.dataset.fallback || '{}'); } catch (e) {}
this.querySelectorAll('.ea-swatch').forEach(function (btn) {
var dot = btn.querySelector('.ea-swatch__dot');
if (dot.style.backgroundColor || dot.style.backgroundImage) return;
var hex = fallback[(btn.dataset.value || '').toLowerCase()];
if (hex) dot.style.backgroundColor = hex;
});
var form = this.closest('form');
this.addEventListener('click', function (e) {
var btn = e.target.closest('.ea-swatch');
if (!btn) return;
var variantId = btn.getAttribute('data-variant-id');
var idInput = form && form.querySelector('input[name="id"]');
if (variantId && idInput) {
idInput.value = variantId;
idInput.dispatchEvent(new Event('change', { bubbles: true }));
}
this.querySelectorAll('.ea-swatch').forEach(function (b) { b.setAttribute('aria-current', b === btn ? 'true' : 'false'); });
}.bind(this));
}
});
}
</script>
{%- endif -%}To install: in Shopify admin go to Online Store → Themes, click … next to your theme → Edit code. Under Snippets choose Add a new snippet, name it as shown above, paste this code and save. Add it inside your product template's {% form 'product', product %} block with {% render 'color-swatches', product: product %}, and make sure that form includes an <input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}"> for it to update.
This replaces your theme's own picker for this option. Hide or remove the theme's default one to avoid showing two.
Live preview
Native swatches, with a fallback
Since Shopify's Winter '24 edition, product options can carry real swatch data (a color or an image) through the Color category metafield, which themes read via product.options_with_values[n].swatch. This snippet reads that first, so it matches whatever colors you've already set up in Settings → Custom data → Products.
For values without a swatch set, it falls back to a name-to-hex list you edit here (for example Navy:#1b2a4a), so nothing renders as a blank circle.
Build it once for either spot, they use the same snippet: on the product page, clicking a swatch updates the hidden variant field inside your add-to-cart form. On collection cards, swatches are links to that exact variant (?variant=123), since a card isn't inside a product form.
Adding it to your theme
- 01
Add the snippet
Online Store → Themes → Edit code → Snippets → Add a new snippet, name it
color-swatches, paste the code. - 02
Product page
In your product template or section, inside the
{% form 'product', product %}...{% endform %}block, add{% render 'color-swatches', product: product, context: 'product' %}where you want the swatches. - 03
Collection cards
In your product card snippet (often
snippets/card-product.liquid), add{% render 'color-swatches', product: product, context: 'card' %}.
This replaces the theme's own variant picker for that option, so hide or remove the theme's default one for it to avoid showing two pickers.
Questions people ask
Why don't my swatches show the right color?
They only pull a real color automatically if you've set the Color category metafield on that product (Settings → Custom data → Products → Color). Otherwise they use your fallback list, matched by the option value's exact name.
Does this work with an option named something other than "Color"?
Yes, change the option name field to match yours exactly (case doesn't matter).
Will clicking a swatch update the price and image too?
It updates the selected variant, which most themes then reflect automatically once the form's hidden variant ID changes and fires a change event. If your theme doesn't listen for that, you may need a developer to wire up the price and gallery update.
Can I use swatch images (patterns) instead of solid colors?
Yes, if a swatch has an image set instead of a color, this renders that image inside the swatch automatically.
Related tools
Last reviewed 30 September 2026.
Sources: Shopify: Liquid swatch object · Shopify Help Center: adding color swatches using category metafields
