Say a div has three child divs. I want to get the elements only if one of its children has specific class. Is it possible to do it with css?
Asked
Active
Viewed 127 times
-1
-
there is no parent selector in css yet. – 4dgaurav Aug 13 '14 at 09:31
-
That would make your CSS browse the DOM upward, it's not possible. Consider using Javascript to add a class to the parent when you add the class to any of the three children. – singe3 Aug 13 '14 at 09:32
-
possible duplicate of [Is there a CSS parent selector?](http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – web-tiki Aug 13 '14 at 09:32
-
you can try out with jQuery using method Closet() because in jquery parent selector is not there – Pranay Rana Aug 13 '14 at 09:34
-
may be you can use `is('.someclass')` – Benjamin Aug 13 '14 at 09:38
2 Answers
0
This isn't possible with CSS alone. You will have to do some javascripting like this.. Replace selector classes with your own.
$(document).ready(function() {
var hasChild = $('div.wrapper div.childClass').length > 0;
if (hasChild) {
// add specific class;
$('div.wrapper').addClass('specific');
}
});
Then you will be able to style the specific class;

Mark Rijsmus
- 627
- 5
- 16