P粉4045397322023-08-22 09:40:21
You can use :has()
CSS pseudo-class
But its browser support is limited (currently not supported by Firefox).
P粉7948519752023-08-22 00:17:37
Currently there is no way in CSS to select the parent of an element in all browsers.
Selectors Level 4 Working DraftIncludes a :has()
pseudo-class that will provide this capability. It would be similar to jQuery's implementation, but is currently not supported by Firefox.
li:has(> a.active) { /* styles to apply to the li tag */ }
Firefox is the only major browser that currently does not support it by default.
In the meantime, if you need to select a parent element with full cross-browser support, you will have to use JavaScript in Firefox.