Bootstrap Dropdowns

Dropdown menus are toggleable, contextual menus, used for displaying links in a list format. It facilitates users to choose one value from a predefined list. This can be made interactive with the dropdown JavaScript plugin.

You have to wrap dropdown menu within the class .dropdown to create Bootstrap Dropdown.

Bootstrap Dropdown Example

<!DOCTYPE html>  

<html>  

<head>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  

    

</head>  

<body>  

  

<div class="container">  

  <h2>Dropdowns</h2>  

  <p>The .dropdown class is used to indicate a dropdown menu.</p>  

  <p>Use the .dropdown-menu class to actually build the dropdown menu.</p>  

  <p>To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and data-toggle="dropdown".</p>                                            

  <div class="dropdown">  

    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example  

    <span class="caret"></span></button>  

    <ul class="dropdown-menu">  

      <li><a href="#">HTML</a></li>  

      <li><a href="#">CSS</a></li>  

      <li><a href="#">JavaScript</a></li>  

    </ul>  

  </div>  

</div>  

  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

  

</body>  

</html> 

Bootstrap Dropdown Divider

The class .divider is used to separate links inside the dropdown menu:

See this example:

!DOCTYPE html>  

<html>  

<head>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  

   

</head>  

<body>  

  

<div class="container">  

  <h2>Dropdowns</h2>  

  <p>The .divider class is used to separate links inside the dropdown menu:</p>  

  <div class="dropdown">  

    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials  

    <span class="caret"></span></button>  

    <ul class="dropdown-menu">  

      <li><a href="#">HTML</a></li>  

      <li><a href="#">CSS</a></li>  

      <li><a href="#">JavaScript</a></li>  

      <li class="divider"></li>  

      <li><a href="#">About Us</a></li>  

    </ul>  

  </div>  

</div>  

  

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  

 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

  

</body>  

</html> 

Bootstrap Dropdown Header

The class .dropdown-header is used to add headers inside the dropdown menu.

See this example:

<!DOCTYPE html>  

<html>  

<head>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  

   

</head>  

<body>  

  

<div class="container">  

  <h2>Dropdowns</h2>  

  <p>The .dropdown-header class is used to add headers inside the dropdown menu:</p>  

  <div class="dropdown">  

    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials  

    <span class="caret"></span></button>  

    <ul class="dropdown-menu">  

      <li class="dropdown-header">Dropdown header 1</li>  

      <li><a href="#">HTML</a></li>  

      <li><a href="#">CSS</a></li>  

      <li><a href="#">JavaScript</a></li>  

      <li class="divider"></li>  

      <li class="dropdown-header">Dropdown header 2</li>  

      <li><a href="#">About Us</a></li>  

    </ul>  

  </div>  

</div>  

  

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  

 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

  

</body>  

</html> 

Bootstrap Dropdown: Disable an item

Use the class .disabled to disable an item in the dropdown menu.

See this example:

<!DOCTYPE html>  

<html>  

<head>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  

    

</head>  

<body>  

  

<div class="container">  

  <h2>Dropdowns</h2>  

  <p>Here, CSS is disable.</p>  

  <div class="dropdown">  

    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials  

    <span class="caret"></span></button>  

    <ul class="dropdown-menu">  

      <li><a href="#">HTML</a></li>  

      <li class="disabled"><a href="#">CSS</a></li>  

      <li><a href="#">JavaScript</a></li>  

      <li class="divider"></li>  

      <li><a href="#">About Us</a></li>  

    </ul>  

  </div>  

</div>  

  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

  

</body>  

</html>

Bootstrap 4 Disable and Active Items

To make an item active, highlight a specific dropdown item with the .active class (active item is appeared with a blue background color).

To disable an item in the dropdown menu, you have to use the .disabled class (It makes a light-grey text color and a “no-parking-sign” icon on hover).

Example:

<!DOCTYPE html>  

<html>  

<head>  

  <title>Bootstrap Example</title>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">  

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  

  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>  

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>  

</head>  

<body>  

  

<div class="container">  

  <h2>Dropdowns Active and Disable Example</h2>  

  <div class="dropdown">  

    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">  

      Dropdown button  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Normal</a>  

      <a class="dropdown-item active" href="#">Active</a>  

      <a class="dropdown-item disabled" href="#">Disabled</a>  

    </div>  

  </div>  

</div>  

  

</body>  

</html> 

Dropdown Position

It is used to position the dropdown items. By default dropdown is positioned at left. You can right-align the dropdown by adding the .dropdown-menu-right class to the element with .dropdown-menu.

Example:

<!DOCTYPE html>  

<html>  

<head>  

  <title>Bootstrap Example</title>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">  

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  

  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>  

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>  

</head>  

<body>  

  

<div class="container">  

  <h2>Right Aligned Dropdowns</h2>  

  <div class="dropdown">  

    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">  

      Dropdown button  

    </button>  

    <div class="dropdown-menu dropdown-menu-right">  

      <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>  

  </div>  

</div>  

  

</body>  

</html> 

Bootstrap 4 Dropup

If you want the dropdown menu to expand upwards instead of downwards, change the

element with class=”dropdown” to “dropup”:

Example:

<!DOCTYPE html>  

<html>  

<head>  

  <title>Bootstrap Example</title>  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">  

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  

  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>  

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>  

</head>  

<body>  

<br>  

  

<div class="container">  

  <h2>Dropup Example</h2>  

  <p>The .dropup class makes the dropdown menu expand upwards instead of downwards:</p>  

  <div class="dropup">  

    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">  

      Dropup button  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

</div>  

  

</body>  

</html> 

Split Button Dropdowns

It is used to show dropdowns as split buttons. Here we use all contextual classes.

Example:

<!DOCTYPE html>  

<html lang="en">  

<head>  

  <title>Bootstrap Example</title>  

  <meta charset="utf-8">  

  <meta name="viewport" content="width=device-width, initial-scale=1">  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">  

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  

  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>  

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>  

</head>  

<body>  

  

<div class="container">  

  <h2>Dropdown Split Buttons Example</h2>  

  <div class="btn-group">  

    <button type="button" class="btn btn-primary">Primary</button>  

    <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">  

      <span class="caret"></span>  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

    

  <div class="btn-group">  

    <button type="button" class="btn btn-secondary">Secondary</button>  

    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">  

      <span class="caret"></span>  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

  

  <div class="btn-group">  

    <button type="button" class="btn btn-success">Success</button>  

    <button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">  

      <span class="caret"></span>  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

  

  <div class="btn-group">  

    <button type="button" class="btn btn-info">Info</button>  

    <button type="button" class="btn btn-info dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">  

      <span class="caret"></span>  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

  

  <div class="btn-group">  

    <button type="button" class="btn btn-warning">Warning</button>  

    <button type="button" class="btn btn-warning dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">  

      <span class="caret"></span>  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

  

  <div class="btn-group">  

    <button type="button" class="btn btn-danger">Danger</button>  

    <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">  

      <span class="caret"></span>  

    </button>  

    <div class="dropdown-menu">  

      <a class="dropdown-item" href="#">Link 1</a>  

      <a class="dropdown-item" href="#">Link 2</a>  

    </div>  

  </div>  

</div>  

  

</body>  

</html>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *