HTML <label> for Attribute

HTMLWeb DevelopmentFront End Technology

The for attribute of the <label> element is used to set an element with a label.

Syntax

Following is the syntax −

<label for="id">

Above, id is the associated element id.

Example

Let us now see an example to implement the for attribute of <label> element −

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Last Semester Results</h2>
<form>
<fieldset>
<legend>Result</legend>
<label for="stinfo">Student:</label>
<input type="text" id ="stinfo" value="Jack"><br>
<label for="sub">Subject:</label>
<input type="text" value="Maths"><br>
<label for="rank">Rank:</label>
<input type="number" value="8"><br>
</fieldset>
</form>
</body>
</html>

Output

This will produce the following output −

In the above example, we have a form with some fields −

<form>
<fieldset>
<legend>Result</legend>
<label for="stinfo">Student:</label>
<input type="text" id ="stinfo" value="Jack"><br>
<label for="sub">Subject:</label>
<input type="text" value="Maths"><br>
<label for="rank">Rank:</label>
<input type="number" value="8"><br>
</fieldset>
</form>

We have set the label for the field using for attribute of the <label> element, for example −

<label for="stinfo">Student:</label>
<input type="text" id ="stinfo" value="Jack"><br>

Now the input type text with is stinfo will have the label “Student” since we have set it with label for −

<label for="stinfo">Student:</label>
raja
Published on 16-Sep-2019 11:16:41
Advertisements