I don't think this is possible with CSS only, but it is definitely possible with Javascript.
You need to - find all divs with class parent
- find all those who have a child div with class child
- if there is no such child, set style.display = none
Now that is pure javascript , it can be a little more complicated. You can use getElementsByClassName from this question and then apply the above logic:
//getElementsByClassName from @CMS answer to the linked question var parentDivs = getElementsByClassName(document, "parent"); for(var i=0; i<parentDivs.length; i++) { var children = getElementsByClassName(parentDivs[i], "child"); if(!children || children.length == 0) { parentDivs[i].style.display = "none"; } }
With jQuery, this is much simpler:
$(".parent").each(function() { if($(this).children(".child").length == 0) { $(this).hide(); } });
Live Example: http://jsfiddle.net/nivas/JWa9r/
source share