P粉1419112442023-08-22 00:53:02
You can use :has()
CSS pseudo-class
But it has limited browser support (Firefox is not supported yet).
P粉5365327812023-08-22 00:27:06
Currently there is no way in CSS to select an element's parent element so that it works correctly in all browsers.
Selectors Level 4 Working Draft contains a :has()
pseudo-class that will provide this functionality. It would be similar to jQuery's implementation, but is currently not supported by Firefox.
li:has(> a.active) { /* 应用于li标签的样式 */ }
Currently only Firefox does not support this feature by default.
Meanwhile, if you need to select a parent element with full cross-browser support, you will have to use JavaScript in Firefox.