The <legend> tag in HTML is used to set the caption for the <fieldset> element. The role of fieldset is used to group elements like button, input, radio, etc.
Syntax
Following is the syntax −
<fieldset> <legend> Set caption </legend> </fieldset>
Example
Let us now see an example to implement the <legend> tag −
<!DOCTYPE html> <html> <body> <h2>Subject-wise rank</h2> <form> <fieldset> <legend>Ranking (caption)</legend> <label for="stinfo">Student:</label> <input type="text" id ="stinfo" value="Steve"><br> <label for="sub">Subject:</label> <input type="text" value="Science"><br> <label for="rank">Rank:</label> <input type="number" value="1"><br> <label for="marks">Marks:</label> <input type="number" value="98"><br> </fieldset> </form> </body> </html>
Output
This will produce the following output −
In the above example, we have set the caption using the <legend> element inside the <fieldset> −
<fieldset> <legend>Ranking (caption)</legend> …form elements… </fieldset>
The <fieldset> group form elements inside the <form> −
<form> <fieldset> <legend>Ranking (caption)</legend> …form elements… </form?