In the below example, buttons dropdowns such as HTML, CSS, JavaScript and Bootstrap are wrapped within the button group with the help of dropdown-toggle class.
[html]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Bootstrap 3 Dropdowns within a Navbar</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Default <span class="caret"></span></button>
<!-- Dropdown list -->
<ul class="dropdown-menu" role="menu">
<li><a href="#">Like</a></li>
<li><a href="#">Share</a></li>
<li><a href="#">Retweet</a></li>
<li class="divider"></li>
<li><a href="#">Poke</a></li>
</ul>
</div>
<div class="btn-group"> <!-- btn group 2, HTML -->
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"> HTML <span class="caret"></span></button>
<!-- Dropdown list -->
<ul class="dropdown-menu" role="menu">
<li><a href="#">Head</a></li>
<li><a href="#">Body</a></li>
<li><a href="#">Paragraphs</a></li>
<li><a href="#">Attributes</a></li>
</ul>
</div>
<div class="btn-group"> <!-- btn group 3, CSS -->
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">CSS <span class="caret"></span></button>
<!-- Dropdown list -->
<ul class="dropdown-menu" role="menu">
<li><a href="#">Borders</a></li>
<li><a href="#">Margins</a></li>
<li><a href="#">Padding</a></li>
<li><a href="#">Colors</a></li>
</ul>
</div>
<div class="btn-group"> <!-- btn group 4, JavaScript -->
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">JavaScript <span class="caret"></span></button>
<!-- Dropdown list -->
<ul class="dropdown-menu" role="menu">
<li><a href="#"></a></li>
<li><a href="#">Forms</a></li>
<li><a href="#">Navigation</a></li>
<li><a href="#">Location</a></li>
<li><a href="#">Nodes</a></li>
</ul>
</div>
<div class="btn-group"> <!-- btn group 5, Bootstrap -->
<button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">Bootstrap <span class="caret"></span></button>
<!-- Dropdown list -->
<ul class="dropdown-menu" role="menu">
<li><a href="#"></a></li>
<li><a href="#">Glyphicons</a></li>
<li><a href="#">Jumbotron</a></li>
<li><a href="#">Grids</a></li>
<li><a href="#">Wells</a></li>
</ul>
</div>
</body>
</html>
[/html]
Output:
As shown in the output, when clicked on the carot symbol, the concepts in the respective dropdown will be displayed.
In the below example, a header ELECTRONICS is added to the Bootstrap Dropdowns element product with various electronic devices like tablets, and mobiles and another header FASHION is included with menu items as clothing and sunglasses.
[html]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Adding Headers to Bootstrap 3 Dropdowns</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css">
.bs-example{
margin: 20px;
}
</style>
</head>
<body>
<div class="bs-example">
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">Products <span class="caret"></span></button>
<ul class="dropdown-menu">
<li class="dropdown-header">ELECTRONICS</li>
<li><a href="#">Mobiles</a></li>
<li><a href="#">Tablets</a></li>
<li><a href="#">Laptops</a></li>
<li class="dropdown-header">FASHION</li>
<li><a href="#">Clothing</a></li>
<li><a href="#">Sunglasses</a></li>
</ul>
</div>
</div>
</body>
</html>
[/html]
Output:
To make a menu item disable in a dropdown, the
disabled class has to be used. However, the link may still be clickable. To avoid this problem, remove the
hrefattribute value. Below example shows the dropdown that allows you to view and download but not to edit/delete.
[html]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Disabling Links inside Bootstrap 3 Dropdowns</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css">
.bs-example{
margin: 20px;
}
</style>
</head>
<body>
<div class="bs-example">
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">Report <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">View</a></li>
<li><a href="#">Download</a></li>
<li class="disabled"><a href="#">Edit / Delete</a></li>
</ul>
</div>
</div>
</body>
</html>
[/html]
Output: