← All elements

Filter Chips With :has()

by @admin · 11 views

<div class="pc-filter">
  <div class="pc-filter__chips">
    <input type="radio" name="pc-cat" id="pc-f-all" checked>
    <label for="pc-f-all">All</label>
    <input type="radio" name="pc-cat" id="pc-f-tees">
    <label for="pc-f-tees">Tees</label>
    <input type="radio" name="pc-cat" id="pc-f-caps">
    <label for="pc-f-caps">Caps</label>
    <input type="radio" name="pc-cat" id="pc-f-totes">
    <label for="pc-f-totes">Totes</label>
  </div>
  <ul class="pc-filter__grid">
    <li data-cat="tees">Linen Tee</li>
    <li data-cat="caps">Dad Cap</li>
    <li data-cat="totes">Canvas Tote</li>
    <li data-cat="tees">Pocket Tee</li>
    <li data-cat="caps">Trucker Cap</li>
    <li data-cat="totes">Market Tote</li>
  </ul>
</div>

About this layout

A category filter where radio chips hide non-matching products by styling the grid from a :has() check on the parent, with no JavaScript.

How it works

The chips are radio inputs with styled labels. A :has() rule on the parent checks which radio is selected and hides the products whose data-cat value does not match, so the grid filters itself with no script. The visible products fade in with a short animation when the choice changes.

Ways to customise it

  • Add a chip by adding a radio, a label and a matching rule.
  • Change the three column grid to two or four.
  • Change the chip colours in the checked state.

Good to know

The radios are visually hidden but still focusable, so the chips work with the arrow keys and show a focus outline. :has() is supported in all current browsers, and without it the grid would simply show every product.

Open an example, copy the HTML and the CSS, and paste them into your page. There is nothing to install and no JavaScript to load.

More in Layout

View all →