1

I want to hide div when it is empty, through CSS, preferably, but I already applied a style that I don't want to sacrifice.

Here is my code rendered as:

<div class="auto-style2"></div>

Here is my second style:

.auto-style2 {
    width: 1100px;
    height: 200px;
    overflow-y: scroll;
    border:1px solid #d3d3d3;
}

I tried: empty-cells: hide;, but that didn't work.

The link below recommended, div:empty { display: none };

But since my div is already using a different style, I would think that it would have to be done through that style only; I think it is going to ignore div:empty; because the second is applied to my div.

Related: How to hide/remove a DIV when empty

4
  • Do you expect C# in the answer? if not there is no need to use the tag
    – TheGeneral
    Commented Jun 12, 2019 at 4:36
  • i am coding in c#...but it is generating all this other html i am working with....i thought c# coders will be good audience to answer.... should i remove c#? Commented Jun 12, 2019 at 4:37
  • .auto-style2:empty { display: none; } Commented Jun 12, 2019 at 4:47
  • ^ His rendered code is not empty.
    – JasonB
    Commented Jun 12, 2019 at 4:48

2 Answers 2

3

You can use:

`.auto-style2:empty { display: none }`

This will work along with your existing .auto-style2{...}.

Note: For empty to work, it has to be literally empty. If there any visible white spaces it wont work.

.auto-style1 {
    width: 1100px;
    height: 200px;
    overflow-y: scroll;
    border:1px solid #d3d3d3;
}

.auto-style1:empty{
  display:none;
}

.auto-style2 {
    width: 1100px;
    height: 200px;
    overflow-y: scroll;
    border:1px solid #d3d3d3;
}

.auto-style2:empty{
  display:none;
}
.auto-style1 shows up cause of white spaces.
<div class="auto-style1"> </div>

.auto-style2 is hidden below this line.
<div class="auto-style2"></div>

5
  • while there were no syntactical complaints, it didn't work. I am still seeing a big blank box on screen with scroll bar on the right side... I am trying to figure out how to get rid of that, because it is rendered as empty div (only with style 2 applied). Commented Jun 12, 2019 at 4:51
  • Are you absolutely sure there are no insvisible chars in the div ? Commented Jun 12, 2019 at 4:52
  • in my second example .auto-style2, notice there are no blank spaces inside the div. like so, <div class="auto-style2"></div> Commented Jun 12, 2019 at 4:54
  • 1
    NOTE: as mentioned by answer-er, in order to get this to work, I had to remove all spaces... If there is a single blank space within div tag, it will fail. Commented Jun 12, 2019 at 5:10
  • 2
    @HillOfBeans Yes, :empty works only for really empty divs. There's a CSS working-draft for :blank which would select all empty divs, even if they have whitespace in it. But it's not supported by any browser yet, so it can take a while until it's usable-
    – elveti
    Commented Jun 12, 2019 at 6:00
0

You can give the div an #ID and then do:

ID:empty{display: None}

Not the answer you're looking for? Browse other questions tagged or ask your own question.