10

Possible Duplicate:
Is there a CSS parent selector?

I am able to hide a DOM tree which look in this way by using .closest().

<div class='parent'>
  <!-- some html code -->
  <div class='child'>
  </div>
  <!-- some html code -->
<div>

$('.child').closest('parent').hide();

It will be possible to get the same effect just by using CSS?
If yes, how?

Community
  • 1
  • 1
Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134

3 Answers3

4

No selector currently exists that can select a previous or parent element.

There is a level 4 selector that is currently being developed.

So in the future, you may be able to do something like this:

!.parent > .child { display: none; }

But until then, you'll have to stick with

$('.child').parent();

in jQuery.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Matt Kieran
  • 4,174
  • 1
  • 17
  • 17
2

No. See for yourself, no such selector exists in CSS3, and not in CSS2 either.

Josh Davenport-Smith
  • 5,456
  • 2
  • 29
  • 40
-7

Might be able to use this

.child:parent .parent{display:none;}

http://css-tricks.com/parent-selectors-in-css/

Josh Bedo
  • 3,410
  • 3
  • 21
  • 33
  • 16
    *"Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS, not even in CSS3"* (cit.) - those are only suggested implementation, not actual implementation – Fabrizio Calderan Oct 03 '12 at 13:03
  • 1
    It's incredible just how many people have misconstrued that article as suggesting that such a selector does exist. – BoltClock Oct 03 '12 at 13:23
  • aaaand that's why we have jQuery. – Ben Sewards Oct 03 '12 at 14:02