Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Hide element if child is empty [duplicate]

Tags:

html

css

Is it possible to hide a parent element, if its child is empty? I know there is the :empty selector, but it only works, if the parent doesn't contain anything, including HTML elements.

This is my HTML:

<div class="row">
    <div class="center">
        <span class="text-danger label-promotion"><strong></strong></span>
    </div>
</div>

And my CSS, which sadly doesn't work this way, but I think you get what I'm trying to do:

.label-promotion:empty {
    display: none;
}

I want the <span> not to appear if is empty and I'd like to avoid JS for this. Is that possible?

like image 621
TheKidsWantDjent Avatar asked Mar 30 '17 15:03

TheKidsWantDjent


People also ask

What is empty pseudo class?

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.


1 Answers

If the child of .label-promotion will always be a <strong>, you can do:

.label-promotion strong:empty {
    display: none;
}

to hide the <strong>. However, you can not hide the <span> itself with CSS. See this answer to a similar question: I would like to hide my outer div if inner div is empty

like image 172
FuturrCoder Avatar answered Sep 20 '22 08:09

FuturrCoder