Bootstrap4 Dropdowns
The Bootstrap 4 Dropdown is a very common UI component used to display a list of clickable options or menu items.
Bootstrap4 dropdowns are commonly used in navigation bars, forms or other interactive interfaces.
Basic structure
The core of the Bootstrap 4 dropdown is the .dropdown class, which can wrap other elements (such as buttons, links or text) to trigger the menu. The menu itself is identified with the .dropdown-menu class, and the specific options use the .dropdown-item class.
Bootstrap4 dropdowns rely on popper.min.js.
Style Class Descriptions:
| Class Names | Description | Example Usage |
|---|---|---|
.dropdown |
The dropdown container class, wrapping the button and the menu. | |
.dropdown-toggle |
Used to create the button that triggers the dropdown, usually combined with the .btn class. |
|
data-toggle="dropdown" |
Makes a button or link trigger the display and hiding of the dropdown. | |
.dropdown-menu |
The dropdown container class, containing the menu items. | |
.dropdown-item |
A menu item in the dropdown, usually a link. | Item |
aria-haspopup="true" |
Adds accessibility support, indicating that the element has a popup menu. | |
aria-expanded="false" |
Adds accessibility support, indicating the current state of the menu (expanded or collapsed). | |
A dropdown is toggleable, a contextual menu that displays links in a list format.
Example
<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="">Link 1
a
<a class="dropdown-item" href="">Link 2
a
<a class="dropdown-item" href="">Link 3
a
div
div
Try it »
Example Parsing
The .dropdown class is used to specify a dropdown.
We can use a button or link to open the dropdown; the button or link needs the .dropdown-toggle class and the data-toggle="dropdown" attribute.
We can also use it in the tag:
Example
<div class="dropdown">
<a class=">btn btn-secondary dropdown-toggle
"
href
=
"
"
role
=
"
button
"
id
=
"
dropdownMenuLink
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
Dropdown link
a
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<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
Try it »
Dividers in a Dropdown
The .dropdown-divider class is used to create a horizontal divider in the dropdown:
Example
<div class="dropdown-divider" div>Try it »
Headers in Dropdowns
The .dropdown-header class is used to add a header in the dropdown:
Example
<div class="dropdown-header">Dropdown header 1
div
Try it »
Enabled and Disabled Items in Dropdowns
The .active class highlights a dropdown option (adds a blue background).
To disable a dropdown option, you can use the .disabled class.
Example
<a class=">dropdown-item active
"
href
=
"
"
Active
a
<a class=">dropdown-item disabled
"
href
=
"
"
Disabled
a
Try it »
Dropdown Alignment
If we want the dropdown right-aligned, we can add the .dropdown-menu-right class after the .dropdown-menu class on the element.
Example
<div class=">dropdown-menu dropdown-menu-right
"
Try it »
Setting the Dropdown Direction
The dropdown direction defaults to downward, but of course we can set a different direction.
Specifying a Right-aligned Dropdown
If you want the dropdown to pop out to the right, you can add the "dropright" class to the div element:
Example
<div class=">btn-group dropright
"
<button type="button" class=">btn btn-secondary dropdown-toggle
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
Dropright
button
<div class="dropdown-menu" div div>
<div class=">btn-group dropright
"
<button type="button" class=">btn btn-secondary
"
Split dropright
button
<button type="button" class=">btn btn-secondary dropdown-toggle dropdown-toggle-split
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
<span class="sr-only">Toggle Dropright
span
button
<div class="dropdown-menu" div div>Try it »
Specifying an Upward Dropup
If you want the dropup to pop up, you can add the "dropup" class to the div element:
Example
<div class=">btn-group dropup
"
<button type="button" class=">btn btn-secondary dropdown-toggle
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
Dropup
button
<div class="dropdown-menu" div div>
<div class=">btn-group dropup
"
<button type="button" class=">btn btn-secondary
"
Split dropup
button
<button type="button" class=">btn btn-secondary dropdown-toggle dropdown-toggle-split
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
<span class="sr-only">Toggle Dropdown
span
button
<div class="dropdown-menu" div div>Try it »
Specifying a Left-aligned Dropdown
If you want the dropdown to pop out to the left, you can add the "dropleft" class to the div element:
Example
<div class=">btn-group dropleft
"
<button type="button" class=">btn btn-secondary dropdown-toggle
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
Dropleft
button
<div class="dropdown-menu" div div>
<div class="btn-group">
<div class=">btn-group dropleft
"
role
=
"
group
"
<button type="button" class=">btn btn-secondary dropdown-toggle dropdown-toggle-split
"
data-toggle
=
"
dropdown
"
aria-haspopup
=
"
true
"
aria-expanded
=
"
false
"
<span class="sr-only">Toggle Dropleft
span
button
<div class="dropdown-menu" div div>
<button type="button" class=">btn btn-secondary
"
Split dropleft
button
div
Try it »
Dropdown Text
The .dropdown-item-text class can set text items in the dropdown:
Example
<div class="dropdown-menu">
<a class="dropdown-item" href="">Link 1
a
<a class="dropdown-item" href="">Link 2
a
<a class="dropdown-item-text" href="">Text Link
a
<span class="dropdown-item-text">Just text
span
div
Try it »
Dropdowns Inside Buttons
We can add a dropdown inside a button:
Example
<div class="btn-group">
<button type="button" class=">btn btn-primary
"
Sony
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="" Tablet a>
<a class="dropdown-item" href="" Smartphone a div div>Try it »
Vertical Button Group with Dropdown:
Example
<div class="btn-group-vertical">
<button type="button" class=">btn btn-primary
"
Apple
button
<button type="button" class=">btn btn-primary
"
Samsung
button
<div class="btn-group">
<button type="button" class=">btn btn-primary dropdown-toggle
"
data-toggle
=
"
dropdown
"
Sony
button
<div class="dropdown-menu">
<a class="dropdown-item" href="" Tablet a>
<a class="dropdown-item" href="" Smartphone a div div div>Try it »