tags:

views:

91

answers:

4

According to http://www.w3.org/TR/html401/interact/forms.html#h-17.4, an input element should end with a single > and not a />. Though that most browsers can handle an input element that ends with />, is such an input element valid according to HTML syntax rules? In other words are elements like <input ... /> and <br /> valid in HTML 4?

(This question is about HTML and not XHTML!!!)

+4  A: 

The syntax is valid in some places but doesn't mean the same as in XHTML, so don't use them.

In HTML 4 <foo /> (where foo is the name of an element defined as EMPTY) means the same as <foo>> which means the same as <foo>&gt; (although almost no browser supports the syntax correctly, Emacs-W3 used to, but broke compatibility with the standard in favour of rendering so-called HTML compatible XHTML 1.0 documents correctly).

This is, therefore, valid in places where you can have a &gt; such as anywhere you are allowed an <img> but not in other places (such as an <hr> that is a child element of the <body> (in Strict)).

The interaction with the rules for optional start and end tags adds more complication. In a Transitional document, this is valid:

<link …/>
<h1>Hello, world</h1>

and means:

<link>
</head>
<body>
&gt;
<h1>Hello, world</h1>

This shorthand syntax could be useful, or at least a time saver, for things like:

<title/The quick brown fox/

instead of the more verbose:

<title>The quick brown fox</title>

… but the syntax has never been well supported and the specification says it should be avoided.

David Dorward
Do you know if Emacs-W3 supported NET syntax in full?
Alohci
@Alohci — No idea, I just got a shock when it littered `>` characters all over my XHTML documents back in 2000ish (which led to me learning a lot about how NET "worked" in HTML)
David Dorward
+1  A: 

Solved actually... according to W3C HTML 4 validator it's better not to use this style of writing element names in HTML 4:

NET-enabling start-tag requires SHORTTAG YES
<br />
The sequence <FOO /> can be interpreted in at least two different ways, 
depending on the DOCTYPE of the document. For HTML 4.01 Strict, the '/' terminates 
the tag <FOO (with an implied '>'). However, since many browsers don't interpret it 
this way, even in the presence of an HTML 4.01 Strict DOCTYPE, it is best to avoid 
it completely in pure HTML documents and reserve its use solely for those written in XHTML.
Bytecode Ninja
Yes, as Rowland Shaw noted, **it is acceptable, but not valid**.
fmark
@fmark — you have it backwards, it is valid (sometimes) but not acceptable. The validator message quoted above is a warning, not an error.
David Dorward
@DavidDorward -- Looks like you are right, according to the HTML spec. I stand corrected!
fmark
A: 

From the HTML Compatibility Guidelines:

Include a space before the trailing / and > of empty elements, e.g. <br />, <hr /> and <img src="karen.jpg" alt="Karen" />. Also, use the minimized tag syntax for empty elements, e.g. <br />, as the alternative syntax <br></br> allowed by XML gives uncertain results in many existing user agents.

Sjoerd
The guidelines for making browsers handle XHTML as HTML don't have a great deal do with what is valid in HTML (since they deal with what browsers do, not what they should do)
David Dorward
Agreed. My answer was more meant as an interesting comment on a related subject than as a definitive answer. I guess I should make it a comment the next time.
Sjoerd