home

Filtering items with just CSS and HTML

This tutorial shows you how to let viewers filter your collection by category with just CSS and HTML, no JS needed! Maybe you have a gallery of art or pictures that can be filtered into groups. Maybe a collection of gifs? List of songs or books?

You can do this easily with a little JS, as shown in my JavaScript Cheat Sheet, but you may not be comfortable with JS.

Fortunately with HTML radio buttons and some CSS you can do this without JS, though it is a little more verbose. Heres my example, using HTML colors as the collection, that the viewer can sort by broad color category!

Color Sorting Example

Select a button to sort HTML colors by group! (automatically updates)

Sooo, how do you do this?

    HTML

  1. Create your collection: images, list items, spans styled as squares (my example), etc
  2. Now categorize the items! (this may be tedious depending on how many you have)
  3. Make radio buttons for each category
    <label>
        <input type="radio" id="pink" name="color">
        pink
    </label>
  4. So that was just the HTML, the buttons won't actually do any filtering yet, the user can only click on them.

    CSS (functional)

  5. The first thing to do is to hide the items so that they only show up when a button is selected:
    .collection:has(input:checked) span {
        display: none;}
  6. Display all of the items when the "all" radio button is selected
    .collection:has(#all:checked) span {
        display: inline-block;}
  7. Display only items of a category when its respective button is selected
    .collection:has(#pink:checked) span[class~="pink"] {
        display: inline-block;}

Yay! Your filter buttons work! Wait, you want to change how they look?

CSS (aesthetics)

This stuff isn't necessary, but if you'd like to customize the radio buttons to your page's theme, this will help.

If you just want to change the select color, you can use accent-color: purple; but its a little more complicated if you want to overide the default browser styling.

Remember that the input is just the little dot or box, label will style the whole button.

To style the little checkbox, first remove its default styling: input { appearance: none;}. You can then give it a border, width, height, background, etc. A normal border will make it square, use border-radius: 50%; to make it a circle (if the width and height are the same).

You'll then want to give it a different style for when its selected input:checked {}, likely by giving it a background color.

If you want to change the label style when the input is checked, use label:has(input:checked) {}

You may also want to style the button on hover; you can use label:hover input to style the checkbox when the label is hovered over, and label:hover to style the label itself. To override the checked styling on hover, use: label:has(input:checked):hover.

Thats all, have fun with your new found CSS power! Radio buttons and :has are really powerful tools that are behind some CSS hacks, such as page tabs and CSS only dark/light mode buttons! I will update my theme tutorial on how to do that.