📖 Scenario: You are building a simple web page that needs a group of buttons aligned side by side. This is common in toolbars or forms where users choose one option from a set.
🎯 Goal: Build a horizontal button group using Bootstrap that contains three buttons labeled Left, Middle, and Right.
📋 What You'll Learn
Use Bootstrap 5 classes to create a button group
Include exactly three buttons with the text: Left, Middle, Right
Buttons should be grouped horizontally with no line breaks
Use semantic HTML and accessible button elements
💡 Why This Matters
🌍 Real World
Button groups are common in toolbars, forms, and navigation bars where users select one option from a set of related actions.
💼 Career
Knowing how to use Bootstrap button groups helps you build professional, consistent user interfaces quickly, a valuable skill for front-end web development jobs.
Progress0 / 4 steps
1
Set up the basic HTML structure
Create a basic HTML5 page with lang="en", meta charset="UTF-8", and a title of "Button Group Example". Add a div with the class container inside the body.
Bootsrap
Hint
Remember to include the lang attribute in the html tag and the meta charset tag inside head.
2
Add Bootstrap CSS link
Inside the head section, add the Bootstrap 5 CSS CDN link: <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">.
Bootsrap
Hint
Use the official Bootstrap 5 CSS CDN link inside the head tag.
3
Create the button group container
Inside the div with class container, add a div with the class btn-group and the attribute role="group".
Bootsrap
Hint
The btn-group class groups buttons horizontally. The role="group" helps screen readers understand this is a group of buttons.
4
Add the three buttons inside the group
Inside the div with class btn-group, add three button elements with type button and class btn btn-primary. The buttons should have the text Left, Middle, and Right respectively.
Bootsrap
Hint
Each button needs the classes btn and btn-primary for Bootstrap styling. Use type="button" for accessibility.
Practice
(1/5)
1. What is the main purpose of using the btn-group class in Bootstrap?
easy
A. To group multiple buttons side by side as a single unit
B. To change the color of a button
C. To add icons inside buttons
D. To create dropdown menus
Solution
Step 1: Understand the role of btn-group
The btn-group class is designed to place buttons next to each other horizontally, making them appear as one group.
Step 2: Compare with other options
Changing colors, adding icons, or dropdowns require different classes or components, not btn-group.
Final Answer:
To group multiple buttons side by side as a single unit -> Option A
Quick Check:
Button groups = group buttons side by side [OK]
Hint: Remember: btn-group means buttons together horizontally [OK]
Common Mistakes:
Confusing btn-group with button color classes
Thinking btn-group creates dropdowns
Assuming btn-group adds icons automatically
2. Which of the following is the correct syntax to create a horizontal button group with three buttons labeled 'One', 'Two', and 'Three'?
easy
A.
OneTwoThree
B.
OneTwoThree
C.
OneTwoThree
D.
OneTwoThree
Solution
Step 1: Check the container class and role
The correct class for horizontal grouping is btn-group with role='group' for accessibility.
Step 2: Verify button elements and labels
Buttons inside should have btn and style classes like btn-primary. The role must be 'group', not 'button'.
Final Answer:
Correct horizontal button group with role='group' -> Option B
Quick Check:
btn-group + role='group' = horizontal group [OK]
Hint: Use role='group' with btn-group for horizontal groups [OK]
Common Mistakes:
Using btn-group-vertical for horizontal groups
Setting role='button' instead of role='group'
Missing role attribute for accessibility
3. What will be the visual result of this code snippet?
A. Buttons should use type='submit' instead of default
B. Class btn-primary is invalid for buttons
C. Missing role='group' attribute on the container div
D. The container should be a nav element instead of div
Solution
Step 1: Check accessibility roles
Bootstrap button groups require role='group' on the container for screen readers to understand the grouping.
Step 2: Validate other attributes and tags
Button type defaults to 'button' which is fine. btn-primary is a valid class. Using div is correct for button groups.
Final Answer:
Missing role='group' attribute on the container div -> Option C
Quick Check:
Button groups need role='group' for accessibility [OK]
Hint: Always add role='group' on btn-group container for accessibility [OK]
Common Mistakes:
Omitting role='group' on the button group container
Confusing button type attribute necessity
Thinking btn-primary is invalid
5. You want to create a vertical button group with three buttons labeled 'Yes', 'No', and 'Maybe' that is also large in size. Which is the correct Bootstrap code snippet?
hard
A.
\n Yes\n No\n Maybe\n
B.
\n Yes\n No\n Maybe\n
C.
\n Yes\n No\n Maybe\n
D.
\n Yes\n No\n Maybe\n
Solution
Step 1: Identify vertical grouping class
The class btn-group-vertical makes the buttons stack vertically.
Step 2: Combine vertical grouping with large size
Adding btn-group-lg alongside btn-group-vertical on the container makes the entire group large and vertical.
Step 3: Check button classes
Buttons have appropriate color classes and do not need individual btn-lg if the group is large.
Final Answer:
Use btn-group-vertical btn-group-lg on container with role='group' -> Option A
Quick Check:
btn-group-vertical + btn-group-lg = large vertical group [OK]
Hint: Combine btn-group-vertical and btn-group-lg on container for large vertical group [OK]
Common Mistakes:
Using btn-group-lg without btn-group-vertical for vertical layout
Adding btn-lg to buttons instead of group container
Mixing btn-group and btn-group-vertical classes incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.