HTML & CSS · Beginner · 15 min
Group radio buttons in a signup form
Give related choices a shared question and make them act as a single-choice group.
Give related choices a shared question
Wrap related choices in <fieldset>. Put <legend> first inside it and use it to ask the shared question. Give each choice its own <label> so readers can tell the options apart.
<fieldset>
<legend>Which workshop session suits you?</legend>
<label><input type="radio" name="session" value="morning"> Morning</label>
<label><input type="radio" name="session" value="afternoon"> Afternoon</label>
</fieldset>Expected appearance
Which workshop session suits you? ○ Morning ○ Afternoon
Share a name and give each option its own value
In the same form, radio buttons with the same name belong to one group. Use name="session" on both choices. Give each one a different value so the form can distinguish them. Use Tab to reach the group and the arrow keys to change the selected option.
<label><input type="radio" name="session" value="morning"> Morning</label>
<label><input type="radio" name="session" value="afternoon"> Afternoon</label>Expected appearance
○ Morning ● Afternoon
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
In the form inside .signup, wrap the two labeled choices in one <fieldset>. Put a <legend> first and write a short question in it. Keep each <input type="radio"> inside its own <label>. Set name="session" on both inputs. Keep value="morning" and value="afternoon" for the two options.
Put <legend> before the two <label> elements in <fieldset>. Change both name attributes to name="session".
Show worked solution
<style>
.signup { max-width: 32rem; margin: 1rem auto; font-family: sans-serif; line-height: 1.5; }
.signup fieldset { border: 1px solid #64748b; padding: 0.75rem; }
.signup legend { font-weight: 700; }
.signup label { display: block; margin-block: 0.5rem; }
.signup input:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 3px; }
</style>
<main class="signup">
<h1>Library workshop signup</h1>
<form>
<fieldset>
<legend>Which workshop session suits you?</legend>
<label><input type="radio" name="session" value="morning"> Morning</label>
<label><input type="radio" name="session" value="afternoon"> Afternoon</label>
</fieldset>
</form>
</main>The <legend> gives the group its question and each <label> identifies one option. Both radios share name="session", so selecting one clears the other.
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Preview the form and select Morning, then Afternoon. Check that only one choice stays selected.
- Use Tab to reach the choices. Use the arrow keys to switch the selection and check that the focus outline stays visible.