A dropdown is a user interface element that allows users to select one option from a list of options. When a dropdown is clicked, it expands to show the available options, allowing the user to select their desired option. Dropdowns are commonly used in forms, navigation menus, and anywhere else that requires a selection from a set of options.
There are various types of dropdowns, each serving different purposes:
When implementing dropdowns, it’s essential to consider accessibility:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap dropdowns Example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h2>Dropdown Examples</h2>
<h4>Basic Dropdown</h4>
<label for="options">Choose an option:</label>
<select id="options" name="options" class="form-control mb-4">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<h4>Dropdown with a Button</h4>
<div class="custom-dropdown mb-4">
<button class="dropbtn">Select an option</button>
<div class="dropdown-content">
<a href="#">Option 1</a>
<a href="#">Option 2</a>
<a href="#">Option 3</a>
</div>
</div>
<h4>Multi-Select Dropdown</h4>
<label for="multi-select">Select options:</label>
<select id="multi-select" name="options" multiple class="form-control mb-4">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<h4>Disabled Dropdown</h4>
<label for="disabled-select">Disabled option:</label>
<select id="disabled-select" name="options" class="form-control mb-4" disabled>
<option value="option1">Option 1 (Disabled)</option>
<option value="option2">Option 2 (Disabled)</option>
</select>
<h4>Searchable Dropdown</h4>
<div class="searchable-dropdown mb-4">
<input type="text" class="search-input" placeholder="Search options..." onkeyup="filterOptions()">
<div class="search-dropdown-content" id="searchOptions">
<div>Option 1</div>
<div>Option 2</div>
<div>Option 3</div>
<div>Option 4</div>
<div>Option 5</div>
</div>
</div>
<h4>Cascading Dropdown</h4>
<label for="main-select">Choose a category:</label>
<select id="main-select" class="form-control mb-4" onchange="updateSubOptions()">
<option value="">Select a category</option>
<option value="fruits">Fruits</option>
<option value="vegetables">Vegetables</option>
</select>
<label for="sub-select">Choose an item:</label>
<select id="sub-select" class="form-control mb-4" disabled>
<option value="">Select an item</option>
</select>
<h4>Bootstrap Dropdown</h4>
<div class="dropdown mb-4">
<button class="btn btn-primary 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>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>