Do I need to add INPUT elements to a DIV element?

Why does this give an error in validating W3c html? I am using HTML 4.01 Strict doctype.

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

and is it not?

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

This is mistake

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

Do I need to put input in a div ?

+6
html
source share
3 answers

The official reason is that in HTML4 lines, FORM elements can only contain block elements, but in turn, blocking elements can contain form elements. Therefore, the structure form->block element->form input is what you need to use.

I canโ€™t find a serious reason why this is so. It seems that the HTML authors did not want to allow forms that start on one line, then wrap to the next line and then have a submit button somewhere on the third line (which you can still achieve by overriding the form's display property if you need to. For example, this 24-way article simply states that "this is the difference between transitional and strict standards."

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

In XHTML, elements must be encoded in a semantic manner. Tables and forms cannot be included in paragraphs, but form elements that are inline elements must be contained in a semantic element of a block level , such as a paragraph or table.

This makes me think that the FORM element is not "semantic" enough to contain other elements. This is usually not intended to label your code, it is more like a technical element that shows where to send data. Therefore, it is technically a BLOCK element, but it needs something more โ€œsemanticโ€ to contain the actual input fields.

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

A form may contain block (except for another form) or a script element.

Input is not a block element, but most elements of a block can contain inline elements that include inputs.

+3
source share

With rigorous validation, you need a block element around your input fields; The best choice here is to go with <fieldset> .

+3
source share

All Articles