Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

css - :empty selector for parent element

I have a div with a header element and a ul in which I load li items:

<div class="listContainer">
    <header>Title</header>
    <ul class="list">
        <li>Test Element</li>
        <li>Test Element</li>
        <li>Test Element</li>
    </ul>
</div>

In this case the whole .listContainer needs to be visible.

But is it possible to hide the whole container with the CSS :empty selector, if .list is empty, like this:

<div class="listContainer">
    <header>Title</header>
    <ul class="list">
    </ul>
</div>

Right now I'm using the :empty selector to hide the ul, but the whole .listContainer needs to be hidden.

.list:empty { display: none; }

I know that it is possible with JavaScript, but in this case I need to do it with CSS alone, but I am not sure if this is possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best I can offer (bearing in mind that there is no parent-selector for CSS), is to reorganise your HTML to the following:

<div class="listContainer">
    <ul class="list"></ul>
    <header>Title</header>
</div>
<div class="listContainer">
    <ul class="list">
        <li>Non-empty</li>
    </ul>
    <header>Title</header>
</div>

And use the following CSS:

.listContainer {
    position: relative;
    border: 2px solid #000;
}

.listContainer header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
}

.listContainer .list {
    margin-top: 2em;
}

.list:empty,
.list:empty + header {
    display: none;
    height: 0;
    margin: 0;
    overflow: hidden;
}

JS Fiddle demo.

This does, unfortunately, require some ugly hacking to position the header element, and doesn't precisely hide the .listContainer (since, again, this isn't possible based upon a child element), however it does approximate your requirement.

With the same HTML as above, but using the flex-box model (as currently, as of this time and date, implemented in Webkit) to reorder the elements' display, and thus avoid the position: absolute ugliness:

.listContainer {
    border: 1px solid #000;
    display: -webkit-flex;
    -webkit-flex-direction: column;
    -webkit-flex-wrap: nowrap;
}

.listContainer header {
    display: -webkit-flex-block;
    -webkit-order: 1;
    -webkit-flex: 1 1 auto;
}

.listContainer .list {
    display: -webkit-flex-block;
    -webkit-flex-direction: column;
    -webkit-order: 2;
    -webkit-flex: 1 1 auto;
}

.listContainer .list:empty,
.listContainer .list:empty + header {
    width: 0;
    height: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
    display: none;
}

JS Fiddle demo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...