Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Using Form Structure Tags on Your HTML5 Web Page

1 of 6 in Series: The Essentials of HTML5 Forms

From the beginning, web pages have had the ability to gather data from users. HTML5 has a standard but quite useful set of form elements you can use to get input. These form elements follow many of the same rules as other tags.

This figure shows a form containing all the main HTML form elements you may encounter:

image0.jpg

The tags you use to manage the general structure of the form include:

  • <form>: This tag contains the actual form; it encloses all the other form elements. The <form> tag should also include the action = "" attribute. This indicates that you do not plan to call a server-side script when the form is submitted.

  • <fieldset>: This special tag allows you to group a series of input elements together. It isn't required, but it can make complex forms easier to navigate. By default, a fieldset has a single border around it, but you can change this with CSS.

  • <legend>: You can add a legend to a fieldset to act as a label for the entire fieldset.

  • <label>: This marks text as the label associated with a particular input element. You can use the optional for attribute to specify which input element the label is associated with. Label tags are normally used to make CSS styling of forms easier to manage.

The code for a page using all the structure tags shown in the figure runs as follows:

<!DOCTYPE HTML>
<html lang = "en">
  <head>
    <title>formDemo.html</title>
    <meta charset = "UTF-8" />
  </head>
  <body>
    <h1>Form Demo</h1>
    <form>
      <fieldset>
        <legend>Text input</legend>
        <p>
          <label>Text box</label>
          <input type = "text"
                 id = "myText"
                 value = "text here" />
        </p>
        <p>
          <label>Password</label>
          <input type = "password"
                 id = "myPwd"
                 value = "secret" />
        </p>
    
        <p>
          <label>Text Area</label>
          <textarea id = "myTextArea"
                rows = "3"
                cols = "80">Your text here</textarea>
        </p>
      </fieldset>
      
      <fieldset>
        <legend>Selecting elements</legend>
        <p>
          <label>Select List</label>
     
          <select id = "myList">
            <option value = "1">one</option>
            <option value = "2">two</option>
            <option value = "3">three</option>
            <option value = "4">four</option>
          </select>
        </p>
    
        <p>
          <label>Check boxes</label>
           <input type = "checkbox"
                 id = "chkEggs"
                 value = "greenEggs" />
           <label for = "chkEggs">Green Eggs</label>
           
          <input type = "checkbox"
                 id = "chkHam"
                 value = "ham" />
          <label for = "chkHam">Ham</label>
        </p>
        <p>
          <label>Radio buttons</label>            
          <input type = "radio"
                 name = "radSize"
                 id = "sizeSmall"
                 value = "small"
                 checked = "checked" />
          <label for = "sizeSmall">small</label>
          
          <input type = "radio"
                 name = "radSize"
                 id = "sizeMed"
                 value = "medium" />
          <label for = "sizeMed">medium</label>
 
          <input type = "radio"
                 name = "radSize"
                 id = "sizeLarge"
                 value = "large" />
          <label for = "sizeLarge">large</label>
        </p>
      </fieldset>
      
      <fieldset>
        <legend>Buttons</legend>
        <p>
          <button type = "button">
            standard button
          </button>
          
          <input type = "button"
                 value = "input button" />
          <input type = "reset" />
          <input type = "submit" />
        </p>       
      </fieldset>     
    </form>
  </body>
</html>

The fieldset, legend, and label tags are not required, but they help you organize the page so it's easier to lay out with CSS. Proper use of these tags and CSS often makes your forms much easier to work with than the older table-based hacks.

blog comments powered by Disqus

SERIES
The Essentials of HTML5 Forms

Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.