Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are self-closing input tags valid in HTML 4?

Tags:

html

html4

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!!!)

like image 392
Behrang Avatar asked Jul 08 '10 08:07

Behrang


People also ask

Are self closing tags valid in HTML?

Of course, they are valid but with little modification. Take an example a self-closing tag <br> . Even if you write <br/> or <br /> they will eventually be converted to <br> in the browser.

Which of the following is not a self closing tag in HTML?

There are also tags that are forbidden to be closed: img, input, br, hr, meta, etc.

Which of the following is a self closing tag in HTML?

The br tag inserts a line break (not a paragraph break). This tag has no content, so it is self closing.

Does an input element need a closing tag?

Syntax. The <input> tag is empty, which means that the closing tag isn't required. But in XHTML, the (<input>) tag must be closed (<input/>).


2 Answers

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.

like image 198
Quentin Avatar answered Oct 16 '22 08:10

Quentin


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.
like image 26
Behrang Avatar answered Oct 16 '22 08:10

Behrang