Creating a CSS product filter with numerous criteria for a unique user experience

While working on my website, I encountered a problem with making a filter work for multiple criteria simultaneously. Specifically, I am trying to filter by both color and size of a stone at the same time. Currently, I am using CSS to hide products that do not meet the specified criteria when a checkbox is checked:

input[id="example"]:checked

I have attempted to display the matching product and hide others by default, but that approach has not been successful. Are there any alternative solutions to achieve this? (I understand that using JavaScript would be easier, but it is not allowed for this particular project)

Answer №1

To enhance the assistance provided, it would be beneficial to include more HTML and incorporate additional CSS logic.

Here is a suggestion on how you can achieve this task using only CSS:

/* initially hide all items */
.items > * { display: none; }

/*
Toggle switches to display items based on matching criteria
Each item must meet at least one criterion to be visible 
*/
.filter#criteria-1:checked ~ .items .criteria-1 { display: block; }
.filter#criteria-2:checked ~ .items .criteria-2 { display: block; }
.filter#criteria-3:checked ~ .items .criteria-3 { display: block; }
<input class="filter" type="checkbox" id="criteria-1" checked>
<label for="criteria-1">Criteria 1</label>
<br>
<input class="filter" type="checkbox" id="criteria-2" checked>
<label for="criteria-2">Criteria 2</label>
<br>
<input class="filter" type="checkbox" id="criteria-3" checked>
<label for="criteria-3">Criteria 3</label>
<br>

<ul class="items">
  <li class="criteria-1 criteria-2">Item 1</li>
  <li class="criteria-1">Item 2</li>
  <li class="criteria-3">Item 3</li>
  <li class="criteria-2">Item 4</li>
  <li class="criteria-1 criteria-3">Item 5</li>
</ul>

Sandbox Link for Testing: https://codesandbox.io/s/wonderful-agnesi-4pe9y7

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can an element in React be styled using the ID alone, without relying on className?

I'm in the process of transitioning an outdated website to React, and I'd prefer not to overhaul all the existing CSS files that have already been written. Many elements currently have their styles defined using an id. Is there a workaround to ma ...

Customizing textarea colors in HTML

I'm attempting to change the color of my HTML textarea element using CSS and/or JavaScript, so that it displays different colors as I type. For instance, if I were to input "Hello world," I would like it to appear in green since it is a string. Howev ...

Can validity api provide a means of displaying errors in a list format?

Currently, I'm working on developing a contact form and attempting to utilize the validity API for the first time. Adapting to WCAG2.1 standards has posed some challenges, but I've managed to display errors underneath each field as I code. Howeve ...

What steps can I take to ensure a website designed for Firefox will work seamlessly on Safari and Chrome browsers as well?

As someone who is still learning about web development, I find myself struggling with browser compatibility issues. It's frustrating to see my website looking different in Chrome compared to Safari. While I know that browsers interpret code differentl ...

Tips for saving the latest counter value in CSS and showcasing it as content in HTML

Here is an example of how my CSS code is structured: .page-number:after { counter-increment: page; } If we were to display 6 pages, it would look something like this: .page-number:after { content: 'Page ' counter(page) ' of'; } ...

Tips for displaying the product title of an item in the Woocommerce shopping cart

Is there a way for me to display the title of the product in my Woocommerce store's cart on the checkout page? My store only allows one product in the cart at a time, and I want to customize the checkout page to show the product title. I've tri ...

Positioning elements within a Bootstrap column: Fixed versus Absolute positioning

Striving to maintain the absolute positioning of the right column as users scroll through the left column has proven to be a challenging task. Despite exploring various solutions from stackoverflow and google, none seem to effectively address this issue. ...

Can anyone explain why my navbar text is not aligning in the center?

I am struggling to center the text in one of my two navbars. As a newcomer to both navbars and bootstrap, I am having difficulty figuring out why I can't achieve this. Should I be making changes on the bootstrap side or can it be fixed within the < ...

The hover effect does not function on an SVG element when it is placed within an external file

I've been experimenting with SVG animation. My goal is to change the color of a circle when it's hovered over. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 437 ...

Tips for accessing the ID, Name and Class of a specific DIV element with the class "Extreme" upon clicking a button

Need help finding the ID of the outermost parent div <div class="panel" id="Kudapanel"> //Top-level Div <div class="Kudapanel1" id="Kudapanel1"> </div> <div> <div> <input type="button" id="yo ...

Differences in CSS text padding when rendered in Firefox compared to Chrome and other web

Seeking assistance with a site issue that has been causing frustration. I have spent the entire evening trying to solve it without success. The issue at hand involves modifying tag appearance after each article on my website. The problem arises when viewi ...

Adjustable DIV based on screen size

I am attempting to create a flexible div that will adjust its width and height proportionally based on the viewport size. However, in the example above, this method only seems to make the div expand horizontally without affecting the vertical height (wh ...

Including CSS in an HTML file does not function properly

I've been attempting to import a CSS file into an HTML document, but for some reason it's not working. I have also tried linking the path directly, but that hasn't worked either. <!DOCTYPE html> <html lang="en"> <head> < ...

Emphasize a specific word within a table row using Reactjs

There is a row object called 'user' that contains key-value pairs of columns and their values. My goal is to highlight all occurrences of a specific word in that row and then return the updated row within a ReactJS table. I attempted the followi ...

Is it possible to apply a single jQuery statement to multiple selectors within the same webpage?

It may seem like a minor issue, but I've encountered a small dilemma. I have SVG icons on my webpage that are supposed to reveal a hidden div when hovered over. While the first icon functions as expected, subsequent icons do not show the hidden div. A ...

Using conditional statements in CSS with the "if else"

Is it possible to use an if-else statement in CSS? This is the part where I would like to change the text color: <?php echo $status; ?> There are two statuses: Pending & Delivered. Pending should display in red and Delivered in green. Could I imp ...

Adjusting the size of absolutely positioned div elements

Currently, I am immersed in a project that involves a full-size slideshow background with content positioned above it. The header and footer are required to have fixed margins at the top and bottom, respectively. The central part needs to be resizable wit ...

Tips for successfully using `cols=""` within a grid container alongside a textarea

It seems that both Chrome and Firefox are not following the cols attribute on textarea elements within a grid container: .grid { display: grid; } textarea:not([cols]) { width: 100%; } <h2>Not in a grid container:</h2> <div> <tex ...

Viewing content below a clear, fixed element

Check out the image regarding the issue I'm currently working on a page that features a fixed header with a transparent background. My goal is to make the content below this fixed header disappear when scrolled under it, similar to how it would behav ...

HTML structure consisting of a 3 by 3 grid

Creating a 3x3 grid with cells that have varying heights depending on content is proving to be challenging. Except for the cells marked with red arrows, the rest of the grid has fixed height in pixels. I attempted to build a 3x3 grid using divs to experi ...