Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Hide parent if all children are hidden

I have a list with groups in it, and use CSSOM to dynamically filter the contents using a text field. This is a way to implement a "search" using only CSS.

Unfortunately when the filter filters everything out, the group containers still remain visible. I'd need to also set display: none onto them using CSS somehow, otherwise I need to add a bunch of JS to control them.

Is this remotely possible? I know this is a big of a long shot, but is there a selector that can select elements whose children (fitting some selector) all must have a style selected on them?

Is it even possible if I greatly relax the constraints, where this might be a selector that selects elements whose children (fitting some selector) all must have a particular class?

like image 213
Steven Lu Avatar asked Jul 23 '17 04:07

Steven Lu


People also ask

How do I completely hide an element in CSS?

Completely hiding elements can be done in 3 ways: via the CSS property display , e.g. display: none; via the CSS property visibility , e.g. visibility: hidden; via the HTML5 attribute hidden , e.g. <span hidden>

How do I select all except first child CSS?

By using the :not(:first-child) selector, you remove that problem. You can use this selector on every HTML element. Another common use case is if you use an unordered list <ul> for your menu.

How do you target all children in CSS?

A child selector matches all child elements of a specified element. It is composed of two or more selectors that are separated by ">". This syntax selects all child elements.

Can CSS be used to find child => parent page element?

There is currently no way to select the parent of an element in CSS in a way that works across all browsers.


1 Answers

No, it's impossible only via CSS:

  1. There is no parent selector.
  2. There is no visibility selector, except something like :not([style*="display:none"]):not([style*="display: none"]) if you hide elements only using inline CSS.
  3. There is no CSS way to know if all children satisfy some condition.

This can be solved only using pure JS loops and conditions or via jQuery selectors like .parent:not(:has(:visible)).

like image 188
Vadim Ovchinnikov Avatar answered Oct 01 '22 03:10

Vadim Ovchinnikov