Welcome to Episode 22 of the Learn HTML series! In this episode, we will discuss how to use the label, fieldset, and legend elements to enhance the accessibility and structure of your forms.
By the end of this episode, you will understand:
- How to use the
labeltag for form input accessibility. - How to group related form controls with the
fieldsettag. - How to use the
legendtag to define the title for groups of form controls.
The label tag is used to define labels for input elements. It improves accessibility by making it easier for screen readers to associate a label with the corresponding input field.
<label for="username">Username:</label>
<input type="text" id="username" name="username" />- The
forattribute of thelabelmust match theidof the associated input element. - This allows users to click the label to focus on the input field.
The fieldset tag is used to group related form elements. It visually groups related controls, making forms more organized.
<fieldset>
<legend>Personal Information</legend>
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" />
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" />
</fieldset>- The
fieldsetelement can contain multiple form controls. - It is often paired with the
legendtag to provide a description for the group.
The legend tag is used to define a caption for the content inside a fieldset. It provides a title for the group of related elements.
<fieldset>
<legend>Contact Information</legend>
<label for="email">Email:</label>
<input type="email" id="email" name="email" />
</fieldset>- The
legendtag must be the first child of thefieldsetelement. - It helps screen readers and users visually identify the purpose of the grouped fields.
- Use
fieldsetandlegendfor better organization, especially in forms with multiple sections. - Always associate labels with inputs to improve accessibility for all users.
➡️ Episode 23: Meta Tags & SEO Basics
In the next episode, we’ll explore how to use meta tags for better SEO and how to make your website more discoverable by search engines!