HTML Tutorial / Explain Forms with fieldset, legend, and autocomplete
Use fieldset to group related form controls and legend to state the group's question. For fields such as a person's name or email address, a suitable autocomplete token can tell the browser what information the field requests.
Group one question with fieldset and legend
Individual radio labels describe the answers, but users also need the question those answers belong to. legend provides that group name. Compare radio and checkbox entries, including unselected controls, in Choose Input Types for Email, Numbers, Dates, and Choices.
<fieldset>
<legend>How should we contact you?</legend>
<label><input type="radio" name="contact" value="email"> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
Open the example in a browser: a border and legend may appear by default. CSS can change that appearance without removing the relationship. The two radio inputs share name="contact" so choosing one clears the other. Use a separate fieldset for a separate question, and a different radio-group name; otherwise selecting an answer in one question could clear an answer in another.
Describe an input's purpose with autocomplete
<form action="/contact" method="post">
<fieldset>
<legend>Contact details</legend>
<label for="full-name">Full name</label>
<input id="full-name" name="full_name" autocomplete="name">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">
</fieldset>
<button type="submit">Send</button>
</form>
autocomplete="name" and autocomplete="email" describe the fields' purposes. They do not set a fixed value or submit information on behalf of the user. Whether a suggestion appears depends on browser settings and stored information. The name attribute is the server-side key; the autocomplete token is an input-purpose hint. /contact is an illustrative path and needs a server handler. An autofill purpose does not replace the label and ID association described in Connect Labels and Inputs Correctly.
Test independent groups and input purposes
Save as groups.html and select Email and Morning. The two selections should coexist because the groups use different name values.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Group practice</title>
</head>
<body>
<h1>Answer two independent questions</h1>
<form id="groups-form" method="get">
<fieldset>
<legend>Contact method</legend>
<label><input type="radio" name="contact" value="email" required> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
<fieldset>
<legend>Visit time</legend>
<label><input type="radio" name="visit" value="morning" required> Morning</label>
<label><input type="radio" name="visit" value="afternoon"> Afternoon</label>
</fieldset>
<button type="submit">Inspect both answers</button>
</form>
</body>
</html>
Temporarily change both visit-group names to contact. Selecting an answer then clears the other question's answer, demonstrating how the name establishes a radio group. Restore visit after the comparison.
| autocomplete token | Input purpose | Example field |
|---|---|---|
| name | Full name | Name |
| given-name | Given name | A separate given-name field |
| family-name | Family name | A separate family-name field |
| Email address | type="email" |
|
| tel | Telephone number | type="tel" |
| postal-code | Postal code | type="text" |
Choose a token that matches the requested information. autocomplete="postal-code" does not validate a postal-code format, and a browser with no saved information may show no suggestion.
Check grouping and autofill
Do not put unrelated questions inside one fieldset, and do not scatter one radio question without a group name. Use defined autocomplete tokens rather than arbitrary words. Test two independent radio groups: if choosing an answer in the second clears the first, their name values probably match by mistake. Autofill does not validate the user's information, and forms collecting personal data still need suitable security and privacy practices. See the MDN autocomplete reference and fieldset reference.




