for

The for attribute in HTML is used with the <label> tag to connect it to a specific form input. This enhances accessibility and user experience by associating labels with their corresponding input fields.

for attribute

The for attribute in HTML is used with <label> and <output> elements to link them to a specific form control, such as <input> or <textarea>. This connection is made by matching the for attribute's value to the id of the related element.

Supported Tags

Syntax

index.html
<tagname for="value">

Example

index.html
<form>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
  <label for="password">Password:</label>
  <input type="password" id="password" name="password">
</form>

Definition and Usage

When used with the <label> element, the for attribute links the label to a specific form element.

When used with the <output> element, the for attribute defines the connection between the calculation result and the elements involved in the calculation.

Value

  • value
    • The value specifies the unique identifier for the attribute, matching the id of the form control (such as <input>, <select>, <textarea>, etc.). It is always enclosed in quotes.

Applies to

The for attribute can be applied to the following elements:

ValueDescription
<label>for
<output>for

Conclusion

The for attribute in HTML associates a <label> or <output> element with a specific form control, improving accessibility. It links the label to an input by matching the for value with the element's id. This enables users to quickly focus on form inputs by clicking on the labels.