Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an input tag not allowed directly within a form tag?

Tags:

html

w3c

I just read the following at http://w3fools.com/#html_forms:

Non-block-level elements (such as <input>) are not valid directly inside <form> tags until HTML5.

I had never heard of anything along these lines, and every basic HTML tutorial I've seen seems to be just fine with putting input tags directly inside a form tag. So my question has three parts:

  • Is the above statement legitimate?
  • Why is this the case? (Was it simply an oversight, or were the creators of the HTML spec trying to prevent specific problems by creating this rule?)
  • What is the recommended way to construct a form with inputs? (Are we just supposed to create a div or a table directly inside the form tag?)
like image 951
StriplingWarrior Avatar asked Mar 25 '11 23:03

StriplingWarrior


People also ask

Does an input tag have to be in a form?

Yes, you can have a valid input without a form.

Can I use form tag inside form tag?

No, nested forms are forbidden. This means A FORM has a mandatory start tag, mandatory end tag and can contain anything in %block or SCRIPT, except other FORMs.

Can input tag be used outside form?

In HTML, <input type="button" /> is used to create buttons in an HTML form. Inside the <button> tag, you can place content like text or images. However, this is not the case with the buttons created with <input> tag. The button element has type=button as default outside the form.

Which attribute is not a part of the input tag?

1. Which of the following is not a type of attribute for input tag? Explanation: Day is not defined in the pre-defined attribute list of input tag. Week attribute defines week and year when used as attribute in input tag.


1 Answers

It's standards pedantics.

  • The statement is legitimate as far as the standard goes: in HTML 4.01, the definition for <form> specifies that it may only contain block elements or <script>. As far as what every browser in the world allows, it's fine.
  • I can only guess that they consider <form> to not be a layout tag at all, and they want all inline elements to be contained inside a block element.
  • Yes, you're supposed to place a <div>, <table>, <p>, or some other block presentational element inside the <form>.
like image 198
Anomie Avatar answered Oct 15 '22 09:10

Anomie