Skip Navigation

[Resolved] :checked css doesn't seem to have any effect on checkboxes

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 10 replies, has 2 voices.

Last updated by melanieG-2 1 year, 8 months ago.

Assisted by: Nigel.

Author
Posts
#2593265

Tell us what you are trying to do?
I'm trying to style the checkboxes with icons on this page: hidden link
I want the opacity of the label to be set at 0.5 if unchecked and 1 if checked.

I tried many css combinaison but nothing seems to work.
I tried for exemple:

.categories-filter input[type="checkbox"]:checked + label {
opacity: 1;
}
.categories-filter input[type="checkbox"]:not(:checked) + label {
opacity: 0.5;
}
In any cases the :checked property doesn't seem to work.

Thank you for your help!

#2593517

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2023-04-14 at 09.40.56.png

Hi Melanie

Inspecting the markup for the checkbox & label I note that it includes a non-breaking space element in between the input and the label (screenshot).

You are using the adjacent sibling connector (+ sign), but because of that space they are non-adjacent siblings, so you should use the general sibling combinator "~" in your selectors, e.g.

.categories-filter input[type="checkbox"]:checked ~ label {
opacity: 1;
}
#2593721

Great idea but it doesn't work neither 🙁

#2593761

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2023-04-14 at 17.19.01.png

It's probably your use of .categories-filter as part of the selector.

The checkbox inputs are not part of that div.

Try without it.

#2593879

Sorry, still not. It works on the other checkbox filter though (not the first I got that result neither).

#2595163

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I don't understand what you wrote.

Going back to check your site, I can see it working, though.

I can see that the style rules have been added, and checking and unchecking the checkboxes they are applied.

The unchecked checkboxes have labels with an opacity of 0.5, and the checked checkboxes have labels with an opacity of 1.0.

#2595415
Screen Shot 2023-04-17 at 7.40.50 AM.png

Sorry if I wasn't clear.

I'm talking about the checkboxes that have icons. (see screenshot) - those are checkboxes that have been styled. That's the ones I'm trying to apply the css to.

I'm not talking about the Amenities checkboxes. Does that make sense?

#2595439

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Ah, those are checkboxes (with custom styling).

The output of those is different. Whereas the other checkboxes are output with the label following the input (making it relatively straightforward to style the label depending on the status of the input), the output of these checkboxes has the input wrapped in the label, i.e. the input is a child of the label element.

Until fairly recently it was not possible to target parent elements with CSS, which is what is required in this scenario, but you can now use the :has pseudo-selector to do so.

I made a quick demo of it working here: hidden link

I'll let you add the details required to specifically target the correct group of filters.

#2595477

Makes sense, thank you! It worked well.

Can I ask you one last thing? What is the status by default of the label? Because all the pins are showing when you arrive on the page but the checkboxes seems to be unchecked.

Thank you for your help!

#2595613

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You mean all the map markers are showing when you first visit the page?

That's because when you visit the page you are seeing the unfiltered results (think Amazon rather than Google).

Once you start clicking checkboxes then the filters apply and only the relevant results are shown.

If you didn't like that behaviour it would be possible to use a conditional shortcode or block to only show the markers when a filter had been applied, but the details of the implementation would depend on whether you are using the blocks UI or the legacy shortcodes UI. (It needs implementing with a shortcode for the markers, but you can mix and match to some extent.)

#2595725

Thank you for your answer. I found a way to make it work as I wanted 🙂 thanks!