bootstrap 4 dropdown menu code example
Example 1: create a dropdown in html bootstrap
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>
Example 2: bootstrap 4 dropdown
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
Example 3: dropdown button in bootstrap 4
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
Example 4: navbar bootstrap 4 with dropdown
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
Example 5: bootstrap dropdown
.dropdown
Add a simple dropdown menu with buttons.
<div class=”dropdown”>
<button class=”btn btn-secondary dropdown-toggle”
type=”button” id=”dropdownMenu1” data-toggle=”dropdown”
aria-haspopup=”true” aria-expanded=”false”>
Dropdown
</button>
<div class=”dropdown-menu” aria-labelledby=”dropdownMenu1”>
<a class=”dropdown-item” href=”#!”>Action</a>
<a class=”dropdown-item” href=”#!”>Another action</a>
</div>
</div>
.dropdown-toggle-split
Create split button dropdowns with proper spacing around the dropdown caret.
<div class=”btn-group”>
<button type=”button” class=”btn btn-secondary”>Dropdown</button>
<button type=”button” class=”btn btn-secondary dropdown-toggle dropdown-togglesplit” data-toggle=”dropdown” aria-haspopup=”true” aria-expanded=”false”>
<span class=”sr-only”>Toggle Dropdown</span>
</button>
<div class=”dropdown-menu”>
<a class=”dropdown-item” href=”#!”>Action</a>
<a class=”dropdown-item” href=”#!”>Another action</a>
</div>
</div>
.dropup
Did you know that you can style a menu coming up rather than down? Now you do!
<!-- Default dropup button -->
<div class=”btn-group dropup”>
<button type=”button” class=”btn btn-secondary dropdown-toggle” datatoggle=”dropdown” aria-haspopup=”true” aria-expanded=”false”>
Dropup
</button>
<div class=”dropdown-menu”>
<!-- Dropdown menu links -->
</div>
</div>
.dropright
Provide the menu to the right of the button.
<!-- Default dropright button -->
<div class=”btn-group dropright”>
<button type=”button” class=”btn btn-secondary dropdown-toggle” datatoggle=”dropdown” aria-haspopup=”true” aria-expanded=”false”>
Dropright
</button>
<div class=”dropdown-menu”>
<!-- Dropdown menu links -->
</div>
</div>
.dropleft
...And you can also display the menu on the left.
<!-- Default dropleft button -->
<div class=”btn-group dropleft”>
<button type=”button” class=”btn btn-secondary dropdown-toggle” datatoggle=”dropdown” aria-haspopup=”true” aria-expanded=”false”>
Dropleft
</button>
<div class=”dropdown-menu”>
<!-- Dropdown menu links -->
</div>
</div>
.dropdown-item-text
Add non-interactive dropdown items to your menu.
<div class=”dropdown-menu”>
<span class=”dropdown-item-text”>Plain text</span>
<a class=”dropdown-item” href=”#”>Clickable action</a>
<a class=”dropdown-item” href=”#”>Another action</a>
<a class=”dropdown-item” href=”#”>Whatever else you need</a>
</div>
.dropdown-item disabled
You can also choose to disable any menu item(s).
<div class=”dropdown-menu”>
<a class=”dropdown-item” href=”#”>Active link</a>
<a class=”dropdown-item disabled” href=”#”>Disabled link</a>
<a class=”dropdown-item” href=”#”>One more link</a>
</div>
.dropdown-divider
Add a simple divider between menu elements to draw extra attention.
<div class=”dropdown-divider”></div>
.dropdown-menu-right
Align the entire menu to the right
<div class=”btn-group”>
<button type=”button” class=”btn btn-secondary dropdown-toggle” datatoggle=”dropdown” aria-haspopup=”true” aria-expanded=”false”>
This dropdown’s menu is right-aligned
</button>
<div class=”dropdown-menu dropdown-menu-right”>
<button class=”dropdown-item” type=”button”>Action</button>
<button class=”dropdown-item” type=”button”>Another action</button>
<button class=”dropdown-item” type=”button”>Something else here</button>
</div>
</div>