开发者

Is it necessary to put INPUT elements in a DIV element?

开发者 https://www.devze.com 2022-12-18 03:23 出处:网络
Why this give error in W3c html validation ? I\'m using HTML 4.01 Strict doctype. <form method=\"get\" action=\"/search\" id=\"search\">

Why this give error in W3c html validation ? I'm using HTML 4.01 Strict doctype.

<form method="get" action="/search" id="search">

   <input type="text" value="search" maxlength="80" class="textbox" >

</form>

and this does not?

  <form method="get" action="/search" id="search">
    <div>
       <input type="text" value="search" maxlength="80" class="textbox" >
    </div>
  </form>

This is error

document type does not allow element "INPUT" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "PRE", "DIV", "ADDRESS" start-tag

Is it necessary to put i开发者_JS百科nput in a div?


<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

A form can contain a block element (with the exception of another form) or a script element.

An input is not a block element, but most block elements may contain inline elements, which include inputs.


With strict validation you need a block element around your input fields; Best choice here would be to go with a <fieldset>.


The official reason is that in HTML4 strict the FORM elements can only contain block elements, but block elements are in turn allowed to contain form inputs. Hence, the structure form->block element->form input is what has to be used.

I cannot find a serious reason, WHY it is so. It sounds like the authors of HTML did not want to allow forms that start on one line, then wrap over to next line and then have a submit button somewhere on the third line (which you can still achieve, by redefining the display property of a form, if you need. For instance, this article on 24ways just states that "it is a difference between Transitional and Strict standards".

Another comparison states (they are talking about xhtml vs html, but the idea is the same):

In XHTML, the elements need to be coded in a semantic manner. Tables and forms can not be included in paragraphs, but form elements, being inline elements, need to be contained within a semantic block level element, such as a paragraph or table cell.

This makes me think that FORM element is not "semantic enough" to contain other elements. It is not normally meant to be used for marking up your code, it is more like a technical element which shows where to post the data to. Hence, it technically is a BLOCK element, but it needs something more "semantic" to contain the actual input fields.

0

精彩评论

暂无评论...
验证码 换一张
取 消