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.
HTML5 Document
If you created a new web page in HTML5, your <label> tag might look like this:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5 Example by www.techonthenet.com</title>
</head>
<body>
<h1>Heading 1</h1>
<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>
</html>
In this HTML5 Document example, we have created two <label> tags. The first <label> tag is the caption for the company <input> and the second <label> tag is the caption for the address <input>. Each <label> is associated to the <input> when the id
from the <input> tag matches the for
attribute on the <label> tag.
HTML 4.01 Transitional Document
If you created a new web page in HTML 4.01 Transitional, your <label> tag might look like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTML 4.01 Transitional Example by www.techonthenet.com</title>
</head>
<body>
<h1>Heading 1</h1>
<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>
</html>
In this HTML 4.01 Transitional Document example, we have created two <label> tags. The first <label> tag is the caption for the company <input> and the second <label> tag is the caption for the address <input>. Each <label> is associated to the <input> when the id
from the <input> tag matches the for
attribute on the <label> tag.
XHTML 1.0 Transitional Document
If you created a new web page in XHTML 1.0 Transitional, your <label> tag might look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XHMTL 1.0 Transitional Example by www.techonthenet.com</title>
</head>
<body>
<h1>Heading 1</h1>
<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>
</html>
In this XHTML 1.0 Transitional Document example, we have created two <label> tags. The first <label> tag is the caption for the company <input> and the second <label> tag is the caption for the address <input>. Each <label> is associated to the <input> when the id
from the <input> tag matches the for
attribute on the <label> tag.
XHTML 1.0 Strict Document
If you created a new web page in XHTML 1.0 Strict, your <label> tag might look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XHTML 1.0 Strict Example by www.techonthenet.com</title>
</head>
<body>
<h1>Heading 1</h1>
<form action="process.php">
<div>
<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" />
</div>
</form>
</body>
</html>
In this XHTML 1.0 Strict Document example, we have created two <label> tags. The first <label> tag is the caption for the company <input> and the second <label> tag is the caption for the address <input>. Each <label> is associated to the <input> when the id
from the <input> tag matches the for
attribute on the <label> tag.
XHTML 1.1 Document
If you created a new web page in XHTML 1.1, your <label> tag might look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XHTML 1.1 Example by www.techonthenet.com</title>
</head>
<body>
<h1>Heading 1</h1>
<form action="process.php">
<div>
<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" />
</div>
</body>
</html>
In this XHTML 1.1 Document example, we have created two <label> tags. The first <label> tag is the caption for the company <input> and the second <label> tag is the caption for the address <input>. Each <label> is associated to the <input> when the id
from the <input> tag matches the for
attribute on the <label> tag.