1

I have been wondering about usage of HTML <form> tag. I needed it for my <input> elements that are inside <div> containers.

<form>
    <div id="container1">
        <input type="text" name="first" id="name">
    </div>
    <div id="container2">
        <input type="text" name="second" id="surname">
    </div>
</form>

Is it correct and natural to do this? would this capture all input elements inside form? If not is there any better way or any other way to achieve this?

ShellRox
  • 2,532
  • 6
  • 42
  • 90

2 Answers2

2

Using div inside form tag is absolutely acceptable, but you can try to use label tag instead of div in this case. More information is here.

Community
  • 1
  • 1
Antonio
  • 901
  • 5
  • 12
  • Hello, thanks for the answer, does label tag have advantages over div in this case? – ShellRox Nov 26 '16 at 08:21
  • It depends on your case. A label element helps user to have a focus on an input element after click on label. – Antonio Nov 26 '16 at 08:38
  • W3C says: The – Antonio Nov 26 '16 at 08:40
  • `label` should provide clear description of the element's purpose as it is being read to people with sight impairments by screen readers; if you want to have accessible website you should associate every form element with a `label` – pwolaq Nov 26 '16 at 08:46
1

form is a block-level element in HTML. Typically, block-level elements allow both block-level and inline children.

Both div and span are valid children of form.

This link would help you to understand better

http://www.w3.org/TR/html4/struct/global.html#h-7.5.3

but You can always use W3C Markup Validation Service to check your html.

Hope this would help. You can also read about Box Modle for more understanding on this subject.

Fenici
  • 461
  • 1
  • 5
  • 19