tags:

views:

271

answers:

3

I was just testing this and in both IE8 & Chrome I see the same thing, empty (styled) divs are rendered differently depending which way you do it. It annoys me because the former seems so much neater.

Why?

EDIT: thanks for the clarifications on XHTML Vs HTML. Currently I have this:

<html>
<head>
<meta http-equiv="Content-Language" content="en-gb" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Test</title>

What is a better choice? I'd prefer XHTML as I believe it's a bit nicer.

+18  A: 

In XHTML (served with Content-Type of application/xhtml+xml), <div /> would indeed work. But in HTML mode (text/html), then no; HTML is not XML, and XML empty tag syntax is not recognised.

Chris Jester-Young
And to add to what Chris wrote, you can specify a doctype of xhtml (instead of html) in your html page as described here: http://www.w3schools.com/tags/tag_doctype.asp. The browser should take the doctype into account when rendering the page.
Dave Paroulek
It actually doesn't use the doctype when deciding how to render the page (well, whether or not to render as xml). It uses the server's reported content-type. Serving as application/xml+xhtml is a mistake in general, so you should use an html doctype and html syntax. See http://hixie.ch/advocacy/xhtml
Kyle Butt
@Kyle that sounds wrong, I'm sure XHTML is OK if done right...
John
Problem is, XTML served as `text/html` is nothing more than **invalid HTML markup**, and it can't be served as `application/xml+html` since IE does not support it
kemp
+6  A: 

<div /> is invalid markup. Since a div can't be self closing (it would just render an empty div), some browsers might render it differently. The HTML 4.01 specs state that divs (and spans) have to have both a start and end tag.

Also take a look at this question.

Brandon
+5  A: 

XHTML and HTML work differently.

In XHTML the 2 are identical and are semantically no different.

In plain HTML, you cannot self close tags. And making your tags self close is not bad, it just doesn't make a difference. The self closures are ignored.

So in plain HTML <div/> is seen as <div> which is never closed. So you need the </div> to close the tag.

HTML browsers have a hardcoded list of self closing tags and handle it for you.

Squeegy