totn HTML

HTML: <label> tag

This HTML tutorial explains how to use the HTML element called the <label> tag with syntax and examples.

Description

The HTML <label> tag is used to generate a caption for a user input such as a <button>, <input>, <meter>, <output>, <progress>, <select> or <textarea> tag. This tag is also commonly referred to as the <label> element.

Syntax

In HTML, the syntax for the <label> tag is:

<body>
<form action="process.php">
   <label for="company">Company: </label>
   <input type="text" name="company" id="company"><br>
   <label for="address">Address: </label>
   <input type="text" name="address" id="address"><br>
   <input type="submit" value="Submit">
 </form>
</body>

Sample Output


To associate a <label> with an <input> control, you must specify an id value for the <input> that matches the for attribute on the <label>: (in this example, the value "company")

<label for="company">Company: </label>
<input type="text" name="company" id="company">

Or you can embed the <input> inside of the <label> as follows: (which implicitly associates the two)

<label>Company:
  <input type="text" name="company">
</label>

Attributes

In addition to the Global Attributes, the following is a list of attributes that are specific to the <label> tag:

Attribute Description HTML Compatibility
for Used to associate the label to the another control. In the example of an <input>, the id value for the <input> must match the value provided in the for attribute to associate the two elements. HTML 4.01, HTML5
form The form that the label belongs to HTML 4.01, HTML5

Note

  • The HTML <label> element is found within the <body> tag. It is used to display a caption for interactive controls on a web page.

Browser Compatibility

The <label> tag has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • Edge Mobile
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the <label> tag below, exploring examples of how to use the <label> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.