Is there a way to select with CSS only an item based on what it contains? For example, I am looking for something like this:
div:contains(#childDivId) {
// some code
}
Many thanks for your help
Cheers
Is there a way to select with CSS only an item based on what it contains? For example, I am looking for something like this:
div:contains(#childDivId) {
// some code
}
Many thanks for your help
Cheers
This is commonly called a "parent selector", and it doesn't exist in CSS. Even the future plans for a :has()
selector indicate that it will only work as a selector from inside JavaScript, evidenced by the fact that it's not included in the "fast profile". So you need JS for this, plain and simple.
Unfortunately there is no such thing in css3 yet. But CSS4 got :has() selector. http://www.w3.org/TR/selectors4/
This matches any F element that is preceded by an E element.
E ~ F { property: value; }
Using Javascript
// JavaScript code:
document.getElementsByClassName("childDiv")[0].parentNode;
// jQuery code:
$('.childDiv').parent().get(0); // This would be the childDiv's parent <div>.