82

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?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • 1
    I would honestly say no. I feel that only certain tags can be self-closing in xhtml. These including
    , , . If it works though then feel free to use it.
    – Howdy_McGee Nov 01 '11 at 19:26

6 Answers6

53

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

It is valid in XHTML.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    This is correct - also, for those of us using AngularJS or similar frameworks a self closing DIV will break the application in IE8. – Stone Oct 08 '12 at 19:59
  • What do you call img tags, then? – Steven Linn Mar 11 '13 at 17:26
  • 1
    Just beware of using jquery as SOMETIMES it won't work right when you have a self-closing tag in a CERTAIN place. – Magnus Smith Mar 15 '13 at 10:39
  • @Stone - can you point me to an example/explanation? I don't see that anywhere. – gogogadgetinternet Nov 29 '13 at 17:08
  • @gogogadgetinternet - At the time i wrote this, there wasn't any examples available. I learned it because I was hired to make six large AngularJS apps IE8 compatible (May no longer break IE in the current AnularJS versio, not sure). That being said, it looks like there's a proper AngularJS doc about IE now: http://docs.angularjs.org/guide/ie – Stone Dec 01 '13 at 00:55
  • HTML 4 theoretically does have self-closing elements via an [obscure NETTAG feature](http://www.cs.tut.fi/~jkorpela/html/empty.html): `

    hello

    `. The W3C DTD Validator supports this oddity, but no browser ever did.
    – Kornel Jan 23 '14 at 14:02
  • 1
    @StevenLinn img elements are called "void elements", or in HTML 4.x lingo, "empty elements". – Mr Lister Jun 15 '14 at 20:12
15

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

<div id="myDiv"></div>
Curtis
  • 101,612
  • 66
  • 270
  • 352
scrappedcola
  • 10,423
  • 1
  • 32
  • 43
10

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 3
    +1 for more detail. Maybe a corresponding link would be helpful ^.^ – Howdy_McGee Nov 01 '11 at 19:31
  • 1
    @Howdy_McGee: Check the W3C, they have the DTDs as well as the (multiple versions of the) SGML declaration for HTML 4.01, and also the (mandatory) SGML declaration for XML. On Linux, look in `/usr/share/xml` and `/usr/share/sgml`. – Kerrek SB Nov 01 '11 at 19:33
3

Self Closing Tags in XHTML as implemented by browsers:

What are all the valid self-closing elements in XHTML (as implemented by the major browsers)?

Self Closing tags in html5:

Are (non-void) self-closing tags valid in HTML5?

Sander de Jong
  • 351
  • 6
  • 18
defau1t
  • 10,593
  • 2
  • 35
  • 47
3

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.

Community
  • 1
  • 1
1

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.

Anders
  • 660
  • 3
  • 12