Performance: Button groups
Button groups affect the rendering speed and interaction responsiveness of grouped buttons on a page.
<div class="btn-group" role="group" aria-label="Basic example"> <button type="button" class="btn btn-primary">One</button> <button type="button" class="btn btn-primary">Two</button> <button type="button" class="btn btn-primary">Three</button> <button type="button" class="btn btn-primary">Four</button> <button type="button" class="btn btn-primary">Five</button> </div>
<div class="btn-group"> <button type="button" class="btn btn-primary">One</button> <button type="button" class="btn btn-primary">Two</button> <button type="button" class="btn btn-primary">Three</button> </div> <div class="btn-group"> <button type="button" class="btn btn-primary">Four</button> <button type="button" class="btn btn-primary">Five</button> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple separate btn-groups | High (many nodes) | Multiple reflows per group | High paint cost | [X] Bad |
| Single btn-group with all buttons | Low (fewer nodes) | Single reflow | Lower paint cost | [OK] Good |
btn-group class in Bootstrap?btn-groupbtn-group class is designed to place buttons next to each other horizontally, making them appear as one group.btn-group.btn-group with role='group' for accessibility.btn and style classes like btn-primary. The role must be 'group', not 'button'.<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>
btn-group class groups buttons horizontally. The btn-group-lg class makes buttons larger in size.btn-group-lg.<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>
role='group' on the container for screen readers to understand the grouping.btn-primary is a valid class. Using div is correct for button groups.role='group' attribute on the container div -> Option Cbtn-group-vertical makes the buttons stack vertically.btn-group-lg alongside btn-group-vertical on the container makes the entire group large and vertical.btn-lg if the group is large.btn-group-vertical btn-group-lg on container with role='group' -> Option APass the test and this pattern moves unknown