Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would someone want to wrap form elements inside of <div> tags? [closed]

Tags:

html

css

For example. here is simple form (that I often use):

<form class="login">
      <input class="login_username" type="text" value="username"/>
      <input class="login_password" type="text" value="password"/>
      <input type="submit" value="Login"/>
</form>

But I read some books, and see some of my friends often do:

<form class="login">
   <div class="login_username">
      <input class="login_input" type="text" value="username"/>
   </div>
   <div class="login_password">
      <input class="login_password" type="text" value="password"/>
   </div>
      <input type="submit" value="Login"/>
</form>

The difference is: they often wrap all components inside a div tag, and then assign its a class name (although inside div tag has only one component). I don't know which advantages when they do this.

Thanks :)

like image 370
Trần Kim Dự Avatar asked Mar 12 '15 13:03

Trần Kim Dự


People also ask

Do you need to close div tags?

Div tag has both open(<div>) and closing (</div>) tag and it is mandatory to close the tag. The Div is the most usable tag in web development because it helps us to separate out data in the web page and we can create a particular section for particular data or function in the web pages.

What is the purpose of your div tags?

The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!

Can we use div tag inside form tag?

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 .

Can you wrap a div in a link?

Using this as a example. Yes, putting an anchor around a block is valid in HTML5 specs. Try cutting and pasting the script into the free online validator or pasting the link to your web page.


1 Answers

WHATWG: 4.10.1.1 Writing a form's user interface

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 text control. 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:

<form>
  <p><label>Customer name: <input></label></p>
</form>

WAI: Forms > Grouping Controls

Grouping related form controls makes forms more understandable for all users, as related controls are easier to identify... In this example, the div element has role=group to indicate that the contained elements are members of a group and the aria-labelledby attribute references the id for text that will serve as the label for the group...

<div role="group" aria-labelledby="shipping_head">
    <div id="shipping_head">Shipping Address:</div>
    <div>
        <label for="shipping_name">
      <span class="visuallyhidden">Shipping </span>Name:
    </label><br>
        <input type="text" name="shipping_name" id="shipping_name">
    </div>
    […]
</div>
<div role="group" aria-labelledby="billing_head">
    <div id="billing_head">Billing Address:</div>
    <div>
        <label for="billing_name">
      <span class="visuallyhidden">Billing </span>Name:
    </label><br>
        <input type="text" name="billing_name" id="billing_name">
    </div>
    […]
</div>

It also recommends to use <fieldset> and <legend> elements. <fieldset> element provides a container for related form controls, and the <legend> element acts as a heading to identify the group.

Other than those, Styling and Manipulating are the main reasons for lots of developers.

like image 185
Stickers Avatar answered Oct 15 '22 01:10

Stickers