tags:

views:

480

answers:

7

Ran into a problem on my web page where the footer in the master page wasn't displaying correctly for one particular page. On that page, I had a

<div style="clear:both" /> at the bottom.

After banging my head at it for a while, I saw that all I needed to change to get the footer to show up properly was to change that line to:

<div style="clear:both"></div>

I don't understand why writing it this way should produce a different result. Aren't they semantically equivalent? I checked and double-checked that this was the only change I made. Flipping back and forth between the two would change the behavior of the footer.

So my question is... are those not equivalent? What's the difference between them?

Edit: The odd part is, they both do what I want to the stuff above them in the page. I mean, in the self-closing div tag's case, if I remove it entirely the page definitely reacts, so it must be doing SOMETHING with it and not just ignoring it completely.

+11  A: 

If I remember right - <div /> is invalid. Use <div></div> if you want it to work everywhere. The closing tag is required, so doing things like <div class="Clear" /> won't work.

All grouping elements will behave the same way. You'll see the same behavior with both DIV and SPAN.

EDIT: Found this link while looking at the link in the answer posted by @Donut - its a matrix that shows which elements have an optional closing tag requirement (among other things.) Looked interesting, so I thought I'd post it here to share with everyone else as well.

Scott Ivey
+ 1 for correct answer. I edited this answer only to make the html tag show up. (marked it as code)
David Stratton
Alternatively, you can use < and > in an answer (of course this is a comment and the rules are different)
pavium
Yeah - totally wasn't thinking when answering that one - thanks for fixing it for me :)
Scott Ivey
I thought span was an inline element.
Joel Coehoorn
...not to say span's end tag isn't required, though. It's just confusing in the context of the preceding sentence.
Joel Coehoorn
yeah, you're right. They are both GROUPING elements, not necessarily block. I'll update the answer to clarify.
Scott Ivey
+21  A: 

<div /> is not a valid markup. A self-closing tag is not permitted.

You need to use the full version <div></div>.

A self closing div tag would make no sense, since it will result in an empty div. An empty div is usually not rendered by most of the browsers.

Developer Art
Clearly, this seems to be the correct answer. I'm not understanding, though, if <div /> is invalid, why it still affects the stuff above it on the page as I hoped it would. I see this in FF3, FF2, IE6, and IE7. If I remove the self-closing div entirely, the page definitely behaves differently.
Sterno
It's only invalid under HTML 4.01. In the absence of a DOCTYPE, browsers are permitted to be more lenient in what they accept.
kdgregory
Because the <div/> is erroneously treated by browsers as a not yet closed <div> tag. So everything that goes after it is considered to be inside of it. Since the tag is never finished in the rest of the document, the document itself is malformed and results in rendering discrepancies.
Developer Art
@Sterno, i guess it just depends on how each browser's rendering engine deals with invalid doctype markup.
Chad
<div /> is valid (in XHTML), but if your XHTML is processed as HTML (i.e. by being served with a text/html content-type instead of the correct application/xhtml+xml) then it will be processed as tag soup and treated as an opening tag not a self-closing tag.
David Dorward
A: 

The first option is not valid html; the second is. <div /> really confuses IE, so always go for <div><div/>.

ilivewithian
fixed html tags, sentence structure.
Joel Coehoorn
+3  A: 

self terminating tags are valid in XML, but not in this case for HTML

Nico
A: 

why would you have a empty div tag on a page? <.../> tags are for tags with no contents.

Chad
Well, it's true, I found out I did want a <br> rather than a div. Really, it was the clear:both behavior that I wanted to make the page elements above it align correctly.
Sterno
+19  A: 
Donut
Scott Ivey
+6  A: 

It depends on the DOCTYPE that you're using.

For XHTML, which is XML, the two tags are equivalent. You would signal this to the browser by including one of the following DOCTYPEs as the first line in your page:

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"&gt;

For HTML 4.01, which is what all (?) browsers assume when there's no DOCTYPE, certain tags must be expressed as open-close. Most of the block-level tags require this, including such non-markup tags as <SCRIPT>. If you look at the WDG HTML documentation, you'll see whether a particular tag requires open-close or can be expressed as an empty tag, via the "syntax" item:

Syntax      <DIV>...</DIV>
kdgregory
Browsers switch between XML parsing and tag soup parsing based on the content-type, not the Doctype. The Doctype switches between Quirks and Standards mode, which has little impact on how the markup is parsed (but lots in how CSS and JS is treated).
David Dorward