Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter CSS selector based on child properties

Tags:

css

Given that there are no parent selectors in CSS and even in Python one must resort to XML to get to parent elements, how might one target the <li> element which contains the <span class="foo"> element?

<ul>
    <li><span class="bar">John</span></li>
    <li><span class="bar">Paul</span></li>
    <li><span class="foo">George</span></li>
    <li><span class="bar">Ringo</span></li>
</ul>

This is in CSS: no Javascript, jQuery, Selenium, or Ford Pantera in the environment.

like image 658
dotancohen Avatar asked Aug 10 '16 09:08

dotancohen


1 Answers

At the moment, you can't.

CSS Selectors Level 4 has a new selector :has

MDN Reference :has

The :has() CSS pseudo-class represents an element if any of the selectors, relative to the:scope of the given element, passed as parameters, matches at least one element. The :has() pseudo-class takes a selector list as an argument.

In your case it would be

`li:has(span.foo) {}`

At present though, no broswer supports this selector.

like image 177
Paulie_D Avatar answered Nov 15 '22 08:11

Paulie_D