Grouping Form Elements
Grouping form elements in HTML helps to organize related fields, making forms more accessible and easier to navigate. This guide provides an overview of how to group form elements using the <fieldset>
and <legend>
tags, along with examples to illustrate their usage.
Fieldset (<fieldset>
)
The <fieldset>
element is used to group related form elements within a form. It creates a box around the grouped elements, providing a visual structure.
<form>
<fieldset>
<legend>Personal Information</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</fieldset>
</form>
Legend (<legend>
)
The <legend>
element is used to provide a caption for the <fieldset>
. It represents the title for the grouped elements, improving accessibility and readability.
<form>
<fieldset>
<legend>Account Details</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</fieldset>
</form>
Conclusion
Grouping form elements using <fieldset>
and <legend>
tags helps in creating well-structured and accessible forms. These elements not only enhance the visual organization of form fields but also improve the user experience by clearly defining related groups of information.
Comments
Post a Comment