Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 - How to separate 2 different dropdown menus?

I am following this tutorial (W3Schools bootstrap 4 tutorial) and I was wondering about how dropdown menus work

   <div class="btn-group">
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
       Sony
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Tablet</a>
      <a class="dropdown-item" href="#">Smartphone</a>
    </div>
  </div>

Why are the dropdown menu and button separated? Shouldn't the menu be a part of the button?

I've tried making 2 buttons in this button group, both dropdown menus but when I tried to make another menu, it chose the first one always

example:

<div class="btn-group">
  <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
    Sony
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Tablet</a>
    <a class="dropdown-item" href="#">Smartphone</a>
  </div>
  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
    Microsoft
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Programs</a>
    <a class="dropdown-item" href="#">Store</a>
  </div>
</div>

but the microsoft button still uses Tablets and Smartphones, so how can I separate the menu items so I can have different items in different buttons?

like image 494
a new one Avatar asked Oct 31 '25 12:10

a new one


1 Answers

You need another surrounding btn-group as so:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>


<div class="btn-group" role="group" aria-label="Button group with nested dropdown">
<div class="btn-group">
  <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
    Sony
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Tablet</a>
    <a class="dropdown-item" href="#">Smartphone</a>
  </div>
</div>
<div class="btn-group">
  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
    Microsoft
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Programs</a>
    <a class="dropdown-item" href="#">Store</a>
  </div>
</div>
</div>
like image 120
Rafael Herscovici Avatar answered Nov 03 '25 02:11

Rafael Herscovici