Cover Image for HTML legend Tag
65 views

HTML legend Tag

The <legend> tag is an HTML element used in conjunction with the <fieldset> element to provide a caption or title for a group of related form controls. It helps to group and label form controls together, making it easier for users to understand and interact with the form.

Here’s an example of how the <legend> tag is used within a <fieldset>:

HTML<span role="button" tabindex="0" data-code="<fieldset> <legend>Shipping Information</legend> <label for="name">Name:</label> <input type="text" id="name" name="name"> <!– Additional form controls — >
<fieldset>
     <legend>Shipping Information</legend>
     <label for="name">Name:</label>
     <input type="text" id="name" name="name">
     <!-- Additional form controls -- >
</fieldset>

In this example, the <fieldset> element creates a container to group related form controls, in this case, for capturing shipping information. The <legend> tag is used as a caption for the fieldset, providing a title or description for the grouped form controls.

The content within the <legend> tag is typically displayed with a slightly larger font and bold styling by default, helping to visually distinguish it from other elements within the fieldset.

The <legend> tag is important for the accessibility and usability of forms, as it helps screen readers and assistive technologies to provide additional context about the purpose of the grouped form controls. It also provides a visual cue for sighted users, making it easier to understand the relationship between the form controls and their associated information.

It’s worth noting that the <fieldset> and <legend> combination can be used for grouping and labeling various types of form controls, such as checkboxes, radio buttons, text inputs, and more.

HTML<span role="button" tabindex="0" data-code="<fieldset> <legend>Choose your preferred payment method:</legend> <input type="radio" id="creditCard" name="payment" value="creditCard"> <label for="creditCard">Credit Card</label> <input type="radio" id="paypal" name="payment" value="paypal"> <label for="paypal">PayPal</label> <!– Additional form controls –>
<fieldset>
     <legend>Choose your preferred payment method:</legend>
     <input type="radio" id="creditCard" name="payment" value="creditCard">
     <label for="creditCard">Credit Card</label>
     <input type="radio" id="paypal" name="payment" value="paypal">
     <label for="paypal">PayPal</label>
     <!-- Additional form controls -->
</fieldset>

In this example, the <legend> tag is used to provide a caption for a group of radio buttons representing different payment methods.

By using the <fieldset> and <legend> tags together, you can create well-structured, accessible forms that are easier for users to understand and interact with.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS