
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML
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 −
<!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>
- Related Articles
- HTML for Attribute
- HTML for Attribute
- Add a label for a form control in HTML?
- HTML DOM Label Object
- HTML pattern Attribute
- HTML novalidate Attribute
- HTML maxlength Attribute
- HTML wrap Attribute
- HTML disabled Attribute
- HTML min Attribute
- HTML disabled Attribute
- HTML disabled Attribute
- HTML min Attribute
- HTML draggable Attribute
- HTML href Attribute

Advertisements