Is it correct to use DIV inside FORM?

HtmlSemantic Markup

Html Problem Overview


I'm just wondering what are you thinking about DIV-tag inside FORM-tag?

I need something like this:

<form>
  <input type="text"/>
  <div> some </div>
  <div> another </div>
  <input type="text" />
</form>

Is it general practice to use DIV inside FORM or I need something different?

Html Solutions


Solution 1 - Html

It is totally fine .

The form will submit only its input type controls ( *also Textarea , Select , etc...).

You have nothing to worry about a div within a form.

Solution 2 - Html

It is completely acceptable to use a DIV inside a <form> tag.

If you look at the default CSS 2.1 stylesheet, div and p are both in the display: block category. Then looking at the HTML 4.01 specification for the form element, they include not only <p> tags, but <table> tags, so of course <div> would meet the same criteria. There is also a <legend> tag inside the form in the documentation.

For instance, the following passes HTML4 validation in strict mode:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
</head>
<body>
<form id="test" action="test.php">
<div>
  Test: <input name="blah" value="test" type="text">
</div>
</form>
</body>
</html>

Solution 3 - Html

You can use a <div> within a form - there is no problem there .... BUT if you are going to use the <div> as the label for the input dont ... label is a far better option :

<label for="myInput">My Label</label> 
<input type="textbox" name="MyInput" value="" />

Solution 4 - Html

> Definition and Usage > > The

tag defines a division or a section in an HTML document. > > The
tag is used to group block-elements to format them with > styles. http://www.w3schools.com/tags/tag_div.asp

Also DIV - MDN

> The HTML

element (or HTML Document Division Element) is the > generic container for flow content, which does not inherently > represent anything. It can be used to group elements for styling > purposes (using the class or id attributes), or because they share > attribute values, such as lang. It should be used only when no other > semantic element (such as
or

Solution 5 - Html

It is wrong to have <input> as a direct child of a <form>

And by the way <input /> may fail on some doctype

Check it with http://validator.w3.org/check


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

<input type="text" />

The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").

Solution 6 - Html

Your question doesn't address what you want to put in the DIV tags, which is probably why you've received some incomplete/wrong answers. The truth is that you can, as Royi said, put DIV tags inside of your forms. You don't want to do this for labels, for instance, but if you have a form with a bunch of checkboxes that you want to lay out into three columns, then by all means, use DIV tags (or SPAN, HEADER, etc.) to accomplish the look and feel you're trying to achieve.

Solution 7 - Html

As the others have said, it's all good, you can do it just fine. For me personally, I try to keep a form of hierarchical structure with my elements with a div being the outer most parent element. I try to use only table p ul and span inside forms. Just makes it easier for me to keep track of parent/children relationships inside my webpages.

Solution 8 - Html

I noticed that whenever I would start the form tag inside a div the subsequent div siblings would not be part of the form when I inspect (chrome inspect) henceforth my form would never submit.

<div>
<form>
<input name='1st input'/>
</div>
<div>
<input name='2nd input'/>
</div>
<input type='submit'/>
</form>

I figured that if I put the form tag outside the DIVs it worked. The form tag should be placed at the start of the parent DIV. Like shown below.

<form>
<div>
<input name='1st input'/>
</div>
<div>
<input name='2nd input'/>
</div>
<input type='submit'/>
</form>

Solution 9 - Html

Absolutely not! It will render, but it will not validate. Use a label.

It is not correct. It is not accessible. You see it on some websites because some developers are just lazy. When I am hiring developers, this is one of the first things I check for in candidates work. Forms are nasty, but take the time and learn to do them properly

Solution 10 - Html

No, its not

<div> tags are always abused to create a web layout. Its symbolic purpose is to divide a section/portion in the page so that separate style can be added or applied to it. [w3schools Doc] [W3C]

It highly depends on what your some and another has.

HTML5, has more logical meaning tags, instead of having plain layout tags. The section, header, nav, aside everything have their own semantic meaning to it. And are used against <div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncethView Question on Stackoverflow
Solution 1 - HtmlRoyi NamirView Answer on Stackoverflow
Solution 2 - HtmlAlex WView Answer on Stackoverflow
Solution 3 - HtmlManseView Answer on Stackoverflow
Solution 4 - HtmlHabibView Answer on Stackoverflow
Solution 5 - Htmlstefan bachertView Answer on Stackoverflow
Solution 6 - HtmlClintonView Answer on Stackoverflow
Solution 7 - HtmlJT SmithView Answer on Stackoverflow
Solution 8 - HtmlVincent OmondiView Answer on Stackoverflow
Solution 9 - Htmluser2774731View Answer on Stackoverflow
Solution 10 - HtmlStarxView Answer on Stackoverflow