Summary -

In this topic, we described about the <label> tag along with detailed example.

Caption or label for the form control in HTML documents. The <label> tag used to represent caption on an HTML document. Mostly <label> tag defines a label for an <input> element.

It helps to understand what the field is while entering data. The tag can be written in two ways.

  1. The tag can be specified like <label></label> with the label text in between the opening and closing tags.The <label> tag attached in the form and near to <input> element.
  2. The tag can be specifies like <label for=""></label> with the element ID in between the double quotes of for attribute.The <label> tag is not required to be near to <input> element and <label> tag can be anywhere in the HTML document.

Syntax -

<label>HTML text here </label>

Optional Attributes -

AttributeDescriptionValues
forSpecifies the label associates with a form control.Form_id
formSpecify the explicit form_id to which form it associate withForm_id

Example -

<!DOCTYPE html>
<html>
	<head>
		<title>Label tag example.. </title>
	</head>
	<body>
		<form>
			<fieldset>
				<legend>Address</legend>
				<label>Location: </label><input type="text"><br>
				<input type="text" name="emailed"><br>
				<label>Pincode: </label><input type="text">
				<label for="emailed">Email: </label>
				<input type="submit" value="Submit">
			</fieldset>
		</form>
	</body>
</html>

Output -

Address