Bootstrap 5 dropdown menu is used to display the predefined links in a list format and it allows the user to select one value from the predefined list.
Steps to create dropdown:
.dropdown
class to the dropdown container's base class.
<button>
inside the dropdown container and assign the .dropdown-toggle
class & data attribute data-bs-toggle="dropdown"
to the <button>
element.
.dropdown-menu
class.
.dropdown-item
class to the unordered list item's <li>
base class.
General Syntax
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" >Dropdown button</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Dropdown item</a></li>
</ul>
</div>
Source Code
<div class="container my-5">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">Dropdown button</button>
<ul class="dropdown-menu" >
<li><a class="dropdown-item" href="..">Action</a></li>
<li><a class="dropdown-item" href="..">Another action</a></li>
<li><a class="dropdown-item" href="...">Something else here</a></li>
</ul>
</div>
</div>
Source Code : Output
Code Explanation
Please keep in mind that , assign .dropdown-menu
class to the dropdown container and assign .dropdown-item
class to every dropdown item base class.
Steps to create dropdown menu using hyperlink:
General Syntax
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#" data-bs-toggle="dropdown">Dropdown link</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Dropdown Link </a></li>
</ul>
</div>
Source Code
<div class="dropdown mt-5 text-center">
<a class="btn btn-secondary dropdown-toggle" href=".." data-bs-toggle="dropdown">Dropdown link</a>
<ul class="dropdown-menu" >
<li><a class="dropdown-item" href="..">Dropdown Link </a></li>
<li><a class="dropdown-item" href="..>Dropdown Link 2</a></li>
<li><a class="dropdown-item" href="..">Dropdown Link 3</a></li>
</ul>
</div>
Source Code : Output
Code Explanation
Please keep in mind that assign .dropdown-menu
to the dropdown item container element base class and assign .dropdown-item
to every dropdown child item base class.
To make dropdown item active and disabled, use .active
and .disbaled
to the dropdown list item <a>
base class.
General Syntax
<ul class="dropdown-menu" >
<a class="btn btn-secondary dropdown-toggle" href="#" data-bs-toggle="dropdown" >Dropdown</a>
<li>
<a class="dropdown-item active" href="#">Active Link</a>
</li>
<li>
<a class="dropdown-item disabled" href="#">Disabled Link</a>
</li>
</ul>
Source Code
<div class="dropdown my-5 text-center">
<a class="btn btn-secondary dropdown-toggle" href=".." data-bs-toggle="dropdown" >Dropdown Link</a>
<ul class="dropdown-menu" >
<li><a class="dropdown-item active" href="...">Active Link</a></li>
<li><a class="dropdown-item disabled" href="..">Disabled Link</a></li>
</ul>
</div>
Source Code : Output
Code Explanation
To make dropdown item active and disabled assign .active
& .disabled
class to the dropdown child element base class correspondingly.
To make large dropdown button size add .btn-lg
& .dropdown-toggle-split
to the <button>
having .dropdown-toggle
while as to make small <button>
add .btn-sm
& .dropdown-toggle-split
to the <button>
having .dropdown-toggle
General Syntax
<button type="button"
class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" >
<span class="visually-hidden">Toggle Dropdown</span>
</button>
Source Code
<div class="btn-group text-center my-5">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-bs-toggle="dropdown">
Large button
</button>
<ul class="dropdown-menu">
<a class="dropdown-item" href="..">Link 1</a>
<a class="dropdown-item" href="..">Link 2</a>
</ul>
</div>
Source Code : Output
Code Explanation
To make a dark dropdown, add .dropdown-menu-dark
class to the dropdown item's container having .dropdown-menu
class.
Source Code
<div class="dropdown m-5">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" >Dropdown button</button>
<ul class="dropdown-menu dropdown-menu-dark">
<li><a class="dropdown-item active" href="..">Action</a></li>
<li><a class="dropdown-item" href="..">Another action</a></li>
<li><a class="dropdown-item" href="..">Something else here</a></li>
<li><hr class="dropdown-divider" /></li>
<li><a class="dropdown-item" href="..">Separated link</a></li>
</ul>
</div>
Source Code : Output
Code Explanation
Please keep in mind that, you have to assign the .dropdown-menu-dark
class to the element having .dropdown-menu
class.
Use navbar and dropdown concept to make a navbar with the dropdown feature.
Source Code
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="..">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDarkDropdown">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href=".." id="navbarDarkDropdownMenuLink" data-bs-toggle="dropdown">Dropdown</a>
<ul class="dropdown-menu dropdown-menu-dark">
<li><a class="dropdown-item" href="..">Action</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
Source Code : Output
The .dropdown-divider
class is used inside dropdown menu list to separate the dropdown item with a horizontal border.
General Syntax
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Link 1</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Another link</a>
</div>
Source Code
<div class="container my-5 text-center">
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">Dropdown button</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="..">Link 1</a>
<a class="dropdown-item" href="..">Link 2</a>
<a class="dropdown-item" href="..">Link 3</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="...">Another link</a>
</div>
</div>
</div>
Source Code : Output
Code Explanation
To create dropdown divider inside dropdown menu, assign the .dropdown-divider
class to the <div>
element's base class.