Button groups let you put several buttons side by side as one unit. This helps keep your page tidy and easy to use.
Button groups in Bootsrap
<div class="btn-group" role="group" aria-label="Button group example"> <button type="button" class="btn btn-primary">Left</button> <button type="button" class="btn btn-primary">Middle</button> <button type="button" class="btn btn-primary">Right</button> </div>
The btn-group class wraps the buttons to group them.
Use role="group" and aria-label for accessibility so screen readers understand the group.
<div class="btn-group" role="group" aria-label="Basic example"> <button type="button" class="btn btn-secondary">One</button> <button type="button" class="btn btn-secondary">Two</button> <button type="button" class="btn btn-secondary">Three</button> </div>
<div class="btn-group btn-group-lg" role="group" aria-label="Large button group"> <button type="button" class="btn btn-success">Left</button> <button type="button" class="btn btn-success">Right</button> </div>
<div class="btn-group-vertical" role="group" aria-label="Vertical button group"> <button type="button" class="btn btn-info">Top</button> <button type="button" class="btn btn-info">Middle</button> <button type="button" class="btn btn-info">Bottom</button> </div>
This page shows a simple horizontal button group with three blue buttons labeled Left, Middle, and Right.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Button Group Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="p-4"> <h1>Button Group Demo</h1> <p>Here is a group of three buttons side by side:</p> <div class="btn-group" role="group" aria-label="Basic example"> <button type="button" class="btn btn-primary">Left</button> <button type="button" class="btn btn-primary">Middle</button> <button type="button" class="btn btn-primary">Right</button> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Button groups keep buttons connected visually and functionally.
Always add aria-label to describe the group for screen readers.
You can make groups vertical by using btn-group-vertical instead of btn-group.
Button groups let you put buttons side by side as one unit.
Use btn-group class and add accessibility roles and labels.
You can change size and orientation with extra classes like btn-group-lg or btn-group-vertical.
Practice
btn-group class in Bootstrap?Solution
Step 1: Understand the role of
Thebtn-groupbtn-groupclass 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, notbtn-group.Final Answer:
To group multiple buttons side by side as a single unit -> Option AQuick Check:
Button groups = group buttons side by side [OK]
- Confusing btn-group with button color classes
- Thinking btn-group creates dropdowns
- Assuming btn-group adds icons automatically
Solution
Step 1: Check the container class and role
The correct class for horizontal grouping isbtn-groupwithrole='group'for accessibility.Step 2: Verify button elements and labels
Buttons inside should havebtnand style classes likebtn-primary. The role must be 'group', not 'button'.Final Answer:
Correct horizontal button group with role='group' -> Option BQuick Check:
btn-group + role='group' = horizontal group [OK]
- Using btn-group-vertical for horizontal groups
- Setting role='button' instead of role='group'
- Missing role attribute for accessibility
<div class='btn-group btn-group-lg' role='group' aria-label='Large button group'> <button type='button' class='btn btn-secondary'>Left</button> <button type='button' class='btn btn-secondary'>Middle</button> <button type='button' class='btn btn-secondary'>Right</button> </div>
Solution
Step 1: Analyze the classes used
Thebtn-groupclass groups buttons horizontally. Thebtn-group-lgclass makes buttons larger in size.Step 2: Understand the layout effect
Buttons inside will appear side by side horizontally, not stacked vertically, and will be large due tobtn-group-lg.Final Answer:
Three large buttons aligned horizontally side by side -> Option DQuick Check:
btn-group + btn-group-lg = large horizontal buttons [OK]
- Confusing vertical stacking with horizontal grouping
- Ignoring size classes like btn-group-lg
- Assuming default size when btn-group-lg is present
<div class='btn-group' aria-label='Button group'> <button class='btn btn-primary'>A</button> <button class='btn btn-primary'>B</button> <button class='btn btn-primary'>C</button> </div>
Solution
Step 1: Check accessibility roles
Bootstrap button groups requirerole='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-primaryis a valid class. Usingdivis correct for button groups.Final Answer:
Missingrole='group'attribute on the container div -> Option CQuick Check:
Button groups need role='group' for accessibility [OK]
- Omitting role='group' on the button group container
- Confusing button type attribute necessity
- Thinking btn-primary is invalid
Solution
Step 1: Identify vertical grouping class
The classbtn-group-verticalmakes the buttons stack vertically.Step 2: Combine vertical grouping with large size
Addingbtn-group-lgalongsidebtn-group-verticalon the container makes the entire group large and vertical.Step 3: Check button classes
Buttons have appropriate color classes and do not need individualbtn-lgif the group is large.Final Answer:
Usebtn-group-vertical btn-group-lgon container with role='group' -> Option AQuick Check:
btn-group-vertical + btn-group-lg = large vertical group [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
