1

I have this html structure:

<div>
<div class="parent">
<div>
  <div class="label">
    <div class="xyz"></div>
  </div>
</div>
</div>
<div class="parent">
<div>
  <div class="label">
  </div>
</div>
</div>
</div>

I want to hide the parent if it is contains div with class="xyz" I tried that:

.parent div.xyz {
  display: none;
}

but seems like my selector does not work.

Sergino
  • 10,128
  • 30
  • 98
  • 159

3 Answers3

0

In css, there's no parent element selector. In your case, you can use jquery to achieve the solution.

$('.parent:has(.xyz)').addClass('hide');

.hide {
  visibility: hidden;
}
Nithya Rajan
  • 4,722
  • 19
  • 30
0

div.xyz .parent {
  display: none;
}
<div>
  <div class="xyz">
    <div class="parent">
      <div>
        <div class="label">
          <div class="xyz"></div>
        </div>
      </div>
    </div>
  </div>

  <div class="parent">
  <div>
    <div class="label">
    </div>
  </div>
  </div>
</div>

You can not select an ancestor, just descendants, an example of how you could do is like this:

<div class="xyz">
<div class="parent">
  <div class="label">
    <div class="xyz"></div>
  </div>
</div>
<div class="parent">
  <div class="label">
  </div>
</div>
</div>

div.xyz .parent {
  display: none;
}

in this case you will hide both .parent class

See more about descending selector

to select a parent or element inside another wapper you will need to use javascript

TheDev
  • 407
  • 2
  • 12
0

You can Hide the parent element with the following jquery

jQuery('.parent').each(function(){
    if (jQuery(this).find('div.xyz').length != 0) {
        jQuery(this).closest('.parent').hide();
    }
});

Hope it will helpful.

krishna
  • 76
  • 7