views:

72

answers:

3

Hello!

I was reading a book about AJAX and the writer said that one should always add a space inside of empty div tags so as to not risk compatibility problems in "some browsers".

So this would be wrong <div></div> and this would be right <div> </div>.

Question: Is he an idiot or does he know something?

Thank you.

+4  A: 

He's not an idiot. IE 8 (possibly earlier versions as well?) will subtly mess up your layout if your empty div is really empty; adding a comment seems to be the suggested way of dealing with it, but apparently a space works as well.

Wooble
+1 I find using a comment weirder than using a space, because a comment isn't supposed to influence *anything* whereas a space character is still text, somehow. Then again, that's the way IE is — always the quirky odd one out.
BoltClock
Wooble: That doesn't mean he's not an idiot, it just means saying what he said doesn't prove it. :D
Francisc
Either I haven't used a lot of empty divs so far, or I haven't noticed any such subtle problems. Could you please give an example of when this may mess things up?
deceze
I think that if you use a div as a spacer (for example) setting its height and width to say 20px (`<div style="width:20px;height:20px;"></div>`) it doesn't always render so in some versions of IE unless you add a space or comment as indicated in the answers here inside the div.
Francisc
+1  A: 

i usually add &nbsp; instead of the empty space!

Mouhannad
+1  A: 

I would add a comment so that nothing is displayed. This does cause problems in ie8 for some reason!

janice
Thank you, Janice.
Francisc