Is it OK to use a self closing DIV tag?

Html

Html Problem Overview


> Possible Duplicate:
> Are self-closing tags valid in HTML5?

For example:

<div id="myDiv" />

Something would then be done to populate this div using Javascript.

Is this valid HTML?

Html Solutions


Solution 1 - Html

No. HTML 4.x doesn't have any concept of self closing tags.

It is valid in XHTML.

Solution 2 - Html

Div's are not valid self closing tags. To have an empty div it would be better to do this:

<div id="myDiv"></div>

Solution 3 - Html

According to the XML declaration and the XHTML 1.0 and 1.1 document definitions, this is fine: the null-end tag (>) may be used when immediately following the null-end start tag closer (/), and your code is equivalent to <div id="myDiv"></div>.

It's a different matter entirely whether any particular consumer will be able to process this correctly.

The SGML declaration used by HTML 4.01 allows tag shortening, but it has a different syntax for the null-end tags; there you can write <div id="abc"/this is a non-empty div/. Again, mileage may vary as for browser support. (My money is on "none".)

Future versions of HTML (HTML5? if that name is still alive) are no longer implemented as SGML languages, and therefore they simply allow what they say they do, without recourse to a formal grammar.

Solution 4 - Html

Solution 5 - Html

I ran these two blocks of code through the W3C validator. Copy and paste the code into the input under the Validate by Direct Input tab to see the results for yourself.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>title</title>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" >
  </head>
  <body><div id="Mydiv" /></body>
</html>

The code block with Doctype of transitional HTML 4.01 failed the validation process.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  </head>
  <body><div id="Mydiv" /></body>
</html>

When I added the XHTML 1.0 transitional doctype, changed the meta tag to a self closing tag, and added in the html xmlns line, the validation passed.

So to answer the first half of your question, it is valid HTML under the XHTML 1.0 Transitional doctype. Whether or not you can use javascript to properly populate it, I am not sure.

Solution 6 - Html

No, it's valid XML (not HTML), and as far as I know, will only be accepted if the document is send with an application/xml mimetype.

However, it may work with XHTML, and the XHTML Doctype declaration.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbe MiesslerView Question on Stackoverflow
Solution 1 - HtmlMark ByersView Answer on Stackoverflow
Solution 2 - HtmlscrappedcolaView Answer on Stackoverflow
Solution 3 - HtmlKerrek SBView Answer on Stackoverflow
Solution 4 - Htmldefau1tView Answer on Stackoverflow
Solution 5 - HtmljoshuariojasView Answer on Stackoverflow
Solution 6 - HtmlAndersView Answer on Stackoverflow