Are we supposed to be able to trust empty DIVs to show in HTML5?

Problem

Having seen advice seemingly change over the years regarding use of empty DIVs (ie. <DIV CLASS="somediv"></DIV>) I'm confused as to the current thinking over whether or not to use &nbsp; when a DIV will have no inner HTML.

I can find no definitive confirmation over whether we can rely on all modern browsers to display background color and image correctly at the specified width & height when there is no inner HTML, so I'm thinking maybe we can't rely on it - yet it's such a seemingly basic area.

I have even seen suggestions that empty DIVs should never be used - but do specs really state it is 'wrong' to have empty DIVs, or is it just unreliable? (I've tried finding reference to them, but maybe I'm using the wrong terms).

To illustrate, here are 5 areas where I would normally use an empty DIV, in the absence of any recommended alternative:

  1. as a placeholder for content which will subsequently be fetched by XHR calls
  2. as a way to manually create space in a layout
  3. where an image is defined in CSS (as a background image, but will effectively be foreground)
  4. where the text will come from the CSS using .somediv:after{content:SOMETEXT}
  5. where CSS is used to display graph bars etc using solid background color

Maybe there are different answers for each of these, which might explain the complexity over this issue.

I have, of course, tried discovering already, but for example the SO question Is necessary to show an empty <div>? suggests to me there is a huge amount of "IMHO", "probably", "seems to work" in this area. I would expect that by now that some official consensus has been reached on best practice.

So.. should I use &nbsp; and if so should I set font-size to the same as the smaller of DIV width/height to ensure that space is filled in all browsers? Are there any other CSS tricks to ensure this will work in all browsers?

Problem courtesy of: user1543257

Solution

The browser is not going to discard or forget your container just because it does not have any contents (yet).

If you want the container to have a specific placeholder shape, then you might give it min-height, min-width, height and width and make sure it's display: block;.

If you are still unsure, you can fill it with a spacer.gif/png without padding and margin.

http://jsfiddle.net/APxNF/1/

Solution courtesy of: DanFromGermany

Discussion

Short answer. Yes, browsers will render the div even if there is no content.

Long answer, That might now always be the case. I have worked in the web for 8 years now and never had to use these, but here they are anyway.

jsFiddle demo

HTML

<div class="empty1"></div>
<div class="empty2"></div>
<div class="empty3"></div>

CSS

.empty1 {
    background: #FBB829;
    height: 100px;
    width: 100px;
}

.empty2:before {
    content: "\00a0";
}
.empty2 {
    background: #FF0066;
    height: 100px;
    width: 100px;
}

.empty3 {
    background: #F02311;
    min-height: 1px;
    height: 100px;
    width: 100px;
}

Sources:

Discussion courtesy of: screenmutt

This recipe can be found in it's original form on Stack Over Flow.