How to Add Multiple Radio Button Groups in HTML
- Understanding Radio Buttons in HTML
- Styling Multiple Radio Button Groups
- Accessibility Considerations for Radio Button Groups
- Conclusion
- FAQ

Adding multiple radio button groups in HTML is a common requirement when designing forms for user input. Radio buttons allow users to select a single option from a set, making them ideal for questions that require a single answer.
In this tutorial, we will walk you through the steps to effectively add multiple radio button groups in HTML. Whether you’re building a survey, a quiz, or a registration form, understanding how to implement radio buttons will enhance your web development skills. Let’s dive in and explore how to create multiple radio button groups that not only look good but also function seamlessly.
Understanding Radio Buttons in HTML
Radio buttons are an essential part of HTML forms. They enable users to select one option from a predefined list. The <input>
element with the type attribute set to “radio” is used to create radio buttons. When you have multiple groups of radio buttons, it’s crucial to ensure that each group is independent of the others. This means that selecting an option in one group should not affect the selections in another group.
To create multiple radio button groups, you need to use the same name attribute for the buttons within each group while ensuring different name attributes for the groups themselves. This way, the browser knows which options belong together.
Here’s a simple example of how to create multiple radio button groups in HTML:
<form>
<fieldset>
<legend>Choose your favorite color:</legend>
<input type="radio" id="colorRed" name="color" value="red">
<label for="colorRed">Red</label><br>
<input type="radio" id="colorGreen" name="color" value="green">
<label for="colorGreen">Green</label><br>
<input type="radio" id="colorBlue" name="color" value="blue">
<label for="colorBlue">Blue</label>
</fieldset>
<fieldset>
<legend>Choose your favorite animal:</legend>
<input type="radio" id="animalDog" name="animal" value="dog">
<label for="animalDog">Dog</label><br>
<input type="radio" id="animalCat" name="animal" value="cat">
<label for="animalCat">Cat</label><br>
<input type="radio" id="animalBird" name="animal" value="bird">
<label for="animalBird">Bird</label>
</fieldset>
</form>
In this example, we have two groups of radio buttons: one for colors and another for animals. Each group has its own <fieldset>
and <legend>
for better accessibility and organization.
Output:
HTML form with multiple radio button groups
The use of <fieldset>
and <legend>
enhances the user experience by clearly defining the groups. Each radio button has a unique id
and shares the same name
attribute with others in the same group, ensuring that only one option can be selected at a time.
Styling Multiple Radio Button Groups
While the basic structure of radio buttons is functional, styling can greatly improve the user experience. By using CSS, you can enhance the appearance of your radio buttons, making them more visually appealing and easier to interact with.
Here’s an example of how to style multiple radio button groups:
<style>
fieldset {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 20px;
}
legend {
font-weight: bold;
}
input[type="radio"] {
margin-right: 10px;
}
</style>
<form>
<fieldset>
<legend>Choose your favorite color:</legend>
<input type="radio" id="colorRed" name="color" value="red">
<label for="colorRed">Red</label><br>
<input type="radio" id="colorGreen" name="color" value="green">
<label for="colorGreen">Green</label><br>
<input type="radio" id="colorBlue" name="color" value="blue">
<label for="colorBlue">Blue</label>
</fieldset>
<fieldset>
<legend>Choose your favorite animal:</legend>
<input type="radio" id="animalDog" name="animal" value="dog">
<label for="animalDog">Dog</label><br>
<input type="radio" id="animalCat" name="animal" value="cat">
<label for="animalCat">Cat</label><br>
<input type="radio" id="animalBird" name="animal" value="bird">
<label for="animalBird">Bird</label>
</fieldset>
</form>
In this example, we added styles to the <fieldset>
, <legend>
, and the radio buttons themselves. The margin-right
property on the radio buttons provides a bit of space between the button and its label, enhancing readability.
Output:
Styled HTML form with multiple radio button groups
Styling your radio buttons not only makes your forms more visually appealing but also improves usability. Users are more likely to engage with well-designed forms, leading to better data collection and user satisfaction.
Accessibility Considerations for Radio Button Groups
When creating forms with radio buttons, accessibility should always be a priority. Ensuring that all users, including those with disabilities, can easily interact with your forms is essential. Here are some best practices for making your radio button groups accessible:
-
Use
<fieldset>
and<legend>
: As shown in previous examples, wrapping radio buttons in a<fieldset>
with a<legend>
provides context for screen readers, helping users understand the purpose of the group. -
Label Elements: Always use
<label>
elements for your radio buttons. This not only makes it easier for users to click on the options but also improves accessibility for screen readers. -
Keyboard Navigation: Ensure that users can navigate through your radio buttons using the keyboard. This is typically handled automatically by browsers, but it’s good to test your forms to ensure smooth navigation.
Here’s how you can ensure accessibility in your radio button groups:
<form>
<fieldset>
<legend>Choose your favorite color:</legend>
<input type="radio" id="colorRed" name="color" value="red">
<label for="colorRed">Red</label><br>
<input type="radio" id="colorGreen" name="color" value="green">
<label for="colorGreen">Green</label><br>
<input type="radio" id="colorBlue" name="color" value="blue">
<label for="colorBlue">Blue</label>
</fieldset>
<fieldset>
<legend>Choose your favorite animal:</legend>
<input type="radio" id="animalDog" name="animal" value="dog">
<label for="animalDog">Dog</label><br>
<input type="radio" id="animalCat" name="animal" value="cat">
<label for="animalCat">Cat</label><br>
<input type="radio" id="animalBird" name="animal" value="bird">
<label for="animalBird">Bird</label>
</fieldset>
</form>
Output:
Accessible HTML form with multiple radio button groups
By following these best practices, you can create forms that are inclusive and user-friendly. Accessibility is not just a legal requirement; it enhances the overall user experience and ensures that everyone can interact with your content.
Conclusion
In this tutorial, we explored how to add multiple radio button groups in HTML. By understanding the structure of radio buttons, applying styles for better aesthetics, and considering accessibility, you can create forms that are not only functional but also user-friendly. Remember, the key to effective web development is to prioritize user experience while ensuring that your forms are easy to navigate and understand. With these skills, you’re well on your way to becoming a proficient web developer.
FAQ
-
How do I group radio buttons in HTML?
To group radio buttons, use the samename
attribute for each group of buttons while ensuring different names for different groups. -
Can I style radio buttons in HTML?
Yes, you can use CSS to style radio buttons, including their labels and surrounding elements for a more visually appealing form. -
What is the purpose of the
<fieldset>
and<legend>
elements?
The<fieldset>
groups related elements in a form, while the<legend>
provides a caption for that group, improving accessibility. -
Are radio buttons accessible for users with disabilities?
Yes, when properly implemented with<label>
elements and<fieldset>
, radio buttons can be made accessible to users with disabilities. -
Can I use JavaScript with radio buttons?
Yes, JavaScript can be used to enhance the functionality of radio buttons, such as dynamically changing the form based on user selections.
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn