Button groups can play a significant role if you desire to wrap interrelated buttons together that represent multiple and connected actions together. Moreover, these button groups can be nested to generate dropdown menus which prove helpful in situations when you want to provide your user with multiple choices.

Before we jump on how to generate dropdown menus by nesting button groups let’s quickly understand how buttons can be grouped together.

How to create Button Groups using Bootstrap 5

For the purpose of grouping buttons you have to assign a .btn-group class to the div container wrapping the buttons. Moreover, if you wish to style these button groups then use the .btn class along with contextual color classes. The .btn class gives a basic style to the groups, while the contextual color classes are used to provide a background color to the button groups.

HTML

<div class=“btn-group”>


  <button type=“button” class=“btn btn-success text-white”>Button 1</button>


  <button type=“button” class=“btn btn-success text-white”>Button 2</button>


  <button type=“button” class=“btn btn-success text-white”>Button 3</button>


  <button type=“button” class=“btn btn-success text-white”>Button 4</button>

</div>

The code snippet will generate four buttons grouped together, each button having a definite padding, a green background and white text color.

Output

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-52.png" data-lazy- height="86" src="data:image/svg xml,” width=”972″>

This is how you can group buttons together using Bootstrap 5.

Now we will move on to the main topic under discussion as indicated by the title of the article which is how button groups can be nested to generate dropdown menus.

How to nest Grouped Buttons to generate Dropdown Menus

You would want to nest button groups to make a dropdown menu in various scenarios such as when you wish to provide multiple options to the user to choose from. The example demonstrated below shows how you can perform this task.

HTML

<div class=“btn-group”>


  <button type=“button” class=“btn btn-success dropdown-toggle” data-bs-toggle=“dropdown”>TV</button>


  <ul class=“dropdown-menu”>


    <li><a class=“dropdown-item” href=“#”>Samsung</a></li>


    <li><a class=“dropdown-item” href=“#”>LG</a></li>


  </ul>

  <div class=“btn-group”>


    <button type=“button” class=“btn btn-success dropdown-toggle” data-bs-toggle=“dropdown”>Mobile Phones</button>


    <ul class=“dropdown-menu”>


      <li><a class=“dropdown-item” href=“#”>Android</a></li>


      <li><a class=“dropdown-item” href=“#”>Iphone</a></li>


    </ul>

  </div>


</div>

Example Explained

The above code generates two button groups each consisting of a single button and each button has a dropdown menu associated with it.

The first button group has a dropdown menu associated with it which was created by assigning the .dropdown class to an unordered list and was made toggleable by assigning the .dropdown-toggle class and to the

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/B49B6FDCE1F74B3598C4C9BF564AEBEC-150×150.jpg625e31efa36ff.jpg" height="112" src="data:image/svg xml,” width=”112″>

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.