Hide parent div , when no child div inside

How to set display none (or add css style) on parent div, if it doesn?t contain any child div? Child div is dynamically attached by system, so we need to set a css class on parent, when is no child inside. Any jquery idea?

parent div

Answers 1

  • You don't need jQuery/ javascript/ angular or anything else

    CSS3's :empty pseudo selector can help


    .displayNoneWhenEmpty:empty{
      display:none;
    }
    <div class="displayNoneWhenEmpty">Not Empty</div>
    
    <div class="displayNoneWhenEmpty"></div>

    Inspect after running the script, and see that style applies to empty div


Related Articles