Open In App

Bootstrap 5 Dropdowns Usage

Bootstrap 5 dropdowns allow users to display a list of options that can be selected by clicking on a button or link. They are often used in navigation menus, form selectors, and other user interfaces. To use a dropdown in Bootstrap 5, you will need to include the Bootstrap CSS and JavaScript files in your project.

Approach:



Bootstrap 5 dropdown Usages: Bootstrap 5 also includes options for making the dropdown menu appear on the right side of the toggle element, as well as creating split dropdowns where the toggle element is split into two parts with one part functioning as the dropdown toggle and the other as a separate action.

Syntax:



<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="#">...</a>
         ...
      </div>
</div>

Example 1: This shows a basic dropdown. In this example, the toggle element is a button with the “dropdown-toggle” class and the “data-toggle” attribute set to “dropdown”. The dropdown menu is a div with the “dropdown-menu” class and the “aria-labelledby” attribute set to the ID of the toggle element. The dropdown items are a series of links with the “dropdown-item” class.




<!doctype html>
<html lang="en">
  
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
  
    <!-- Bootstrap CSS -->
    <link href=
        rel="stylesheet" integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
        crossorigin="anonymous">
     <!-- Option : Bootstrap Bundle with Popper -->
    <script src=
            integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous">
    </script>    
</head>
  
<body class="m-3">
    <h1 class="text-success">
        GeeksforGeeks
    </h1>
  
    <div class="dropdown" >
        <button class="btn btn-secondary dropdown-toggle" 
                type="button" id="dropdownMenuButton2"
            data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown button
        </button>
        <ul class="dropdown-menu dropdown-menu-dark" 
            aria-labelledby="dropdownMenuButton2">
            <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>            
        </ul>
    </div>   
</body>
</html>

Output:

Bootstrap 5  Dropdowns Usage

Example 2: This example shows a right-aligned dropdown. To display a dropdown menu to the right of an element, add the “dropend” class to the parent element.




<!doctype html>
<html lang="en">
  
<head>
   <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
  
    <!-- Bootstrap CSS -->
    <link href=
        rel="stylesheet" integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
        crossorigin="anonymous">
     <!-- Option : Bootstrap Bundle with Popper -->
    <script src=
            integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous">
    </script
</head>
  
<body class="m-3">
    <h1 class="text-success">
        GeeksforGeeks
    </h1>
  
    <!-- Container element for the dropdown -->
    <div class="dropend">
        <button class="btn btn-secondary dropdown-toggle" 
            type="button" id="dropdownMenuButton2"
            data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown button
        </button>
        <ul class="dropdown-menu dropdown-menu-dark" 
            aria-labelledby="dropdownMenuButton2">
            <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>
</body>
</html>

Output:

Bootstrap 5  Dropdowns Usage

Example 3: This shows a left-aligned dropdown. To display a dropdown menu to the left of an element, add the “dropstart” class to the parent element.




<!doctype html>
<html lang="en">
  
<head>
   <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
  
    <!-- Bootstrap CSS -->
    <link href=
        rel="stylesheet" integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
        crossorigin="anonymous">
     <!-- Option : Bootstrap Bundle with Popper -->
    <script src=
            integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous">
    </script
</head>
  
<body class="m-2" style="text-align:center">
    <h1 class="text-success">
        GeeksforGeeks
    </h1>
  
    <!-- Container element for the dropdown -->
    <div class="dropstart">
        <button class="btn btn-secondary dropdown-toggle" 
                type="button" id="dropdownMenuButton2"
            data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown button
        </button>
        <ul class="dropdown-menu dropdown-menu-dark" 
            aria-labelledby="dropdownMenuButton2">
            <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>    
</body>
</html>

Output:

Bootstrap 5  Dropdowns Usage

Example 4: To display a dropdown menu above an element, add the “dropup” class to the parent element.




<!doctype html>
<html lang="en">
  
<head>
   <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
  
    <!-- Bootstrap CSS -->
    <link href=
        rel="stylesheet" integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
        crossorigin="anonymous">
     <!-- Option : Bootstrap Bundle with Popper -->
    <script src=
            integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous">
    </script
</head>
  
<body class="m-2" 
    style="text-align:center;padding-top:120px;">
    <h1 class="text-success">
        GeeksforGeeks
    </h1>
  
    <!-- Container element for the dropdown -->
   <div class="dropup">
        <button class="btn btn-secondary dropdown-toggle" 
            type="button" id="dropdownMenuButton2"
            data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown button
        </button>
        <ul class="dropdown-menu dropdown-menu-dark" 
            aria-labelledby="dropdownMenuButton2">
            <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
</body>
</html>

Output:

Bootstrap 5  Dropdowns Usage

Example 5: This shows a Dropdown in Navbar. A navbar dropdown menu is displayed within a navigation bar. To create a dropdown menu in a navbar using Bootstrap, you can use the .nav-item and .dropdown classes.




<!doctype html>
<html lang="en">
  
<head>
   <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
  
    <!-- Bootstrap CSS -->
    <link href=
        rel="stylesheet" integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
        crossorigin="anonymous">
     <!-- Option : Bootstrap Bundle with Popper -->
    <script src=
            integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous">
    </script
</head>
  
<body class="m-2">
  
    <!-- Container element for the dropdown -->
    <nav class="navbar navbar-expand-lg navbar-dark 
        bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">
                <h1 class="text-success">
                    GeeksforGeeks
                </h1>
            </a>
            <button class="navbar-toggler" type="button" 
                data-bs-toggle="collapse"
                data-bs-target="#navbarNavDarkDropdown" 
                aria-controls="navbarNavDarkDropdown" 
                aria-expanded="false"
                aria-label="Toggle navigation">
                <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" role="button"
                            data-bs-toggle="dropdown" aria-expanded="false">
                            Dropdown
                        </a>
                        <ul class="dropdown-menu dropdown-menu-dark" 
                            aria-labelledby="navbarDarkDropdownMenuLink">
                            <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>
                    </li>
                </ul>
            </div>
        </div>
    </nav>     
</body>
</html>

Output:

Bootstrap 5  Dropdowns Usage

Reference: https://getbootstrap.com/docs/5.0/components/dropdowns/


Article Tags :