Shopify Development

Shopify Product Swatches: Dynamically Hide Color Options for Single-Variant Products

Hey store owners! Ever found yourself staring at your collection pages, wondering why a product with just one color option still shows a color swatch or, even worse, an "Available Colors" label that feels utterly redundant? You're not alone! This is a super common scenario, and it recently sparked a fantastic discussion in the Shopify Community that we at Shopping Cart Mover want to dive into with you.

The goal is simple: create a cleaner, more intuitive shopping experience. If a product only comes in one color, displaying a color swatch or a "Choose your color" prompt is not just unnecessary; it can actually confuse customers and make your store look less polished. It’s these small refinements that elevate your brand and improve user satisfaction.

Shopify Theme Editor Custom CSS section with code to hide product swatches.
Shopify Theme Editor Custom CSS section with code to hide product swatches.

The Challenge: Hiding Redundant Swatches and Labels

Our friend Geronim0 kicked off the Shopify Community thread grappling with this exact problem. They wanted to clean up their product listings, ensuring that swatches and any related text only appeared when there were actually multiple color variants to choose from. Initially, Geronim0 had implemented a Liquid-based solution within their swatches.liquid file to conditionally render the swatches:

{%- comment -%}
  This is a conceptual example of how one might try to hide swatches with Liquid.
  The actual solution discussed in the thread is CSS-based and more robust.
{%- endcomment -%}
{%- liquid
  assign product_has_multiple_colors = false
  for product_option in product_resource.options_with_values
    if product_option.name contains 'Color' or product_option.name contains 'Colour'
      if product_option.values.size > 1
        assign product_has_multiple_colors = true
        break
      endif
    endif
  endfor
-%}
{% if product_has_multiple_colors %}
  
{% endif %}

This Liquid snippet checks if a product has an option named "Color" (or "Colour") with more than one value. If so, it sets a flag to true, allowing the swatches to render. While this approach effectively hid the visual swatches, it introduced a new challenge: a manually added "Available Colors" label (or any other rich text block) was still appearing for single-color products, creating an awkward empty space and defeating the purpose of a clean UI.

This is a classic example of how front-end development often requires a layered approach. Sometimes, Liquid alone isn't enough, and you need the power of CSS to precisely target and hide elements based on their siblings or parent's children.

The Expert Solution: Leveraging Modern CSS with :has()

Enter Tim, a Shopify expert who provided an elegant and robust CSS solution. The key was to use the powerful, albeit relatively newer, CSS pseudo-class :has(). This allows you to select an element based on whether it contains (or doesn't contain) specific children.

Here's the code Tim provided, designed to be placed in your theme's "Custom CSS" section:

product-card:not(:has(.variant-option__swatch:nth-of-type(2))) product-swatches,
product-card:not(:has(.variant-option__swatch:nth-of-type(2))) product-price + rte-formatter {
  display:none;
}

Breaking Down the CSS Magic:

  • product-card:not(:has(.variant-option__swatch:nth-of-type(2))): This is the core of the solution.
    • product-card: Targets the main container for a product on your collection page.
    • :not(...): A negation pseudo-class. It selects a product-card that *does not* match the condition inside the parentheses.
    • :has(...): The game-changer! It checks if the product-card contains an element matching the selector within its own scope.
    • .variant-option__swatch:nth-of-type(2): This targets the *second* swatch element. If a product only has one color, it won't have a second swatch.

    So, this entire selector reads: "Select any product-card that *does not* contain a second swatch."

  • product-swatches: This is the element containing all your product swatches. When combined with the previous selector, it means "hide the product-swatches element if its parent product-card doesn't have a second swatch."
  • product-price + rte-formatter: This targets a rich text editor (rte-formatter) block that immediately follows the product-price element. This specifically addresses Geronim0's "Available Colors" label dilemma, assuming that label was rendered within such a block right after the price. The + is an adjacent sibling combinator, ensuring only the *next* sibling is affected.
  • display:none;: The standard CSS property to completely hide an element from the layout.

This solution is incredibly powerful because it's dynamic and purely conditional. It doesn't require complex Liquid logic for every possible scenario and handles both the swatches and related text blocks with a single, elegant CSS rule.

What if you need to hide 'everything after price'?

Tim also provided a more general solution for hiding all siblings after a specific element: product-price ~ *. The ~ is a general sibling combinator, which selects all siblings that follow the `product-price` element. While powerful, use this with caution, as it might hide elements you intend to keep visible.

Implementing the Solution on Your Shopify Store

Applying this fix is straightforward, even for those new to code:

  1. Log in to your Shopify Admin: Go to your store's backend.
  2. Navigate to Themes: In the left sidebar, click on "Online Store" > "Themes".
  3. Edit Your Theme: Find your current theme, click "Actions" > "Edit code".
  4. Access Custom CSS: In the theme editor, locate the "Assets" folder and find a file like base.css, theme.css, or custom.css. Alternatively, many modern themes offer a dedicated "Custom CSS" field within "Theme Settings" (look for a cog icon or "Customize" > "Theme settings"). This is the safest place for custom code.
  5. Paste the Code: Copy the CSS snippet provided above and paste it at the very end of your chosen CSS file or into the "Custom CSS" field.
  6. Save and Test: Save your changes and visit your collection pages to ensure the swatches and labels are now correctly hidden for single-color products.

Why This Level of Detail Matters for Your E-commerce Store

As Shopify migration experts, we often see merchants overlook these seemingly small details. However, a clean, intuitive user interface directly impacts your conversion rates and brand perception. When customers encounter redundant information or cluttered layouts, it can lead to confusion, frustration, and ultimately, abandoned carts.

Dynamically hiding elements like color swatches for single-variant products makes your store:

  • More Professional: It shows attention to detail and a commitment to a polished user experience.
  • Less Cluttered: A cleaner layout helps customers focus on essential product information.
  • More Intuitive: Customers won't waste time looking for color options that don't exist.
  • Faster Loading (Marginally): Hiding elements with CSS is more efficient than rendering them and then hiding them with JavaScript.

For those looking to build their first e-commerce presence or migrate an existing store, starting a Shopify store offers unparalleled flexibility and a robust ecosystem for growth. Mastering these types of customizations ensures your store not only looks great but also performs optimally.

Conclusion

The solution to hiding redundant product swatches and labels on Shopify collection pages is a testament to the power of well-placed CSS. By leveraging the :has() pseudo-class, you can create a more dynamic and user-friendly interface that adapts to your product data. This not only enhances the aesthetic appeal of your store but also contributes to a smoother, more efficient shopping journey for your customers.

At Shopping Cart Mover, we specialize in ensuring that every detail of your e-commerce platform is optimized for success, whether you're migrating to Shopify or refining your existing setup. Don't let small UI inconsistencies detract from your brand's potential!

Share:

Use cases

Explore use cases

Agencies, store owners, enterprise — find the migration path that fits.

Explore use cases