content logo

Learn HTML:

Html form tag

The HTML <form> tag represents a form in an HTML document.

The <form> tag is used in conjunction with form-associated elements. To create a form, you typically nest form-associated elements inside the opening/closing <form> tags. You can also use the form attribute within those elements to reference the ID of the form to use.

Syntax

The <form> tag is written as <form></form> with any number of form-associated elements nested between the start and end tags. The <form> tag usually has an action attribute specified (which specifies the page that will process the form). It can also have other attributes. See below under "Template" for a list of attributes that can be used with the <form> element.

Example

<form action="/html/tags/html_form_tag_action.cfm" target="_blank" method="get">
<p><label>First name: <input type="text" name="first_name" maxlength="100" style="width:120px;"></label></p>
<p><label>Last name: <input type="text" name="last_name" maxlength="100" style="width:120px;"></label></p>
<input type="submit" value="Submit">
</form>

Using an Element's form Attribute

It is possible to associate an element with a form by using that element's form attribute. Form-associated elements have this attribute that allow you to explicitly specify which form should be used for that element.

If using an element's form attribute, you must specify the id of the form you wish to associate the element with.

In the following example, I've placed all the form-associated elements outside of the <form> element. But I've purposely associated only two elements with the form (to demonstrate the effect of the form attribute). The first name has been associated with a form but the last name has not. Therefore, the last name is not submitted with the form and the action page doesn't acknowledge the users' last name.

Differences Between HTML 4 & HTML 5

The accept attribute was dropped in HTML5.

HTML5 introduced two attributes that weren't in HTML 4. These are:

  • autocomplete
  • novalidate

Also, HTML5 has added the ability for form-associated elements to become associated with a <form> element by using the form-associated element's form attribute.

To see more detail on the two versions see HTML5 <form> Tag and HTML4 <form> Tag. Also check out the links to the official specifications below.

#HTML Form Example #HTML Form Code #HTML Form Tutorial