Why <div style="width:50%" /> is not rendered in xHTML?

Artem picture Artem · Oct 24, 2009 · Viewed 53k times · Source

Why <div style="width:50%" /> is not rendered in xHTML? If I do <div style="width:50%"> &nbsp; </div> only then it is rendered. Do I miss something? Will min-width help? I use strict xHTML 1.0

Is it possible to fix it with CSS only or I must change html markup?

Answer

random picture random · Oct 24, 2009

Because DIV is not a self-closing element in either XHTML or HTML.

You must pair the opening tag (<div>) with the closing (</div>) .

On the other part of your question:

<div style="width:50%">&nbsp;</div>

<div style="width:50%"></div>

The difference between is that in the first there is a non-breaking space character that fills up the DIV. In the second there is nothing. That is why you will not see anything render on the second version.

Also, XML and XHTML are not the same thing. The latter borrows the convention of self-closing a tag if it's not part of a matching pair.