Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the W3C advise wrapping input elements in <p> tags?

I've seen a lot of examples on the web where forms are laid out like so:

<form>     <p><input></p> </form> 

To my surprise, this is also described in the spec:

Any form starts with a form element, inside which are placed the controls. Most controls are represented by the input element, which by default provides a one-line text field. To label a control, the label element is used; the label text and the control itself go inside the label element. Each part of a form is considered a paragraph, and is typically separated from other parts using p elements. Putting this together, here is how one might ask for the customer's name:

Though this section is non-normative, it still seems to me that this is bad practice and not semantic. I suppose that the purpose is to put inputs on their own line, but shouldn't the display of these elements be controlled using CSS?

Is there a reason why the W3C advises forms be laid out this way? Am I missing something?

like image 907
Radu Avatar asked Aug 03 '11 23:08

Radu


People also ask

What is the purpose of input tag?

The input tag is used within < form> element to declare input controls that allow users to input data. An input field can be of various types depending upon the attribute type. The Input tag is an empty element which only contains attributes.

Can you put a div inside p tag?

The <div> tag can NOT be inside <p> tag, because the paragraph will be broken at the point, where the <div> tag is entered. To apply styles inside a paragraph use <span> tag, which is used with inline elements.

Is it important to name an input tag?

Never omit the name The data is packaged as a series of name-value pairs. The name for each name-value pair is the name attribute of each input, and the value is the user-entered (or pre-specified) value. Without the name attribute, an <input> element cannot provide its value to the server on form submission.

Is the most important element of form tag?

The most important form element is the <input> element. The <input> element can vary in many ways, depending on the type attribute. All HTML input types are covered in the next chapter.


2 Answers

If you are writing a form in a meaningful (read: semantic) way, you will want the flow of text to lead to the element:

<form>  <p><label for="firstName">Please enter your first name:</label><input id="firstName" type="text" /></p> </form> 

An even better way is to treat your form like a mad-libs script:

<form>   <p>Hello. My <label for="firstName">name</label> is <input id="firstName" type="text" />...</p> </form> 

A p element isn't the only way to mark-up a form. If a matrix of data is being added/edited, it's semantically valid to use a table.

In other cases, you might not want to use a wrapping element. It depends on what content you want to be serving up. Worry about styling it all after you've got your content sorted out.

like image 151
zzzzBov Avatar answered Sep 30 '22 06:09

zzzzBov


INPUT elements are inline, therefore it makes sense to wrap them in some sort of block element, so that there is a natural separation between them. Since the DIV has no margins by default, doesn't it make sense to use a paragraph?

like image 32
Abel Mohler Avatar answered Sep 30 '22 06:09

Abel Mohler