How to use dropdown in Bootstrap 4
Introduction
A dropdown is a menu that allows a user to choose from a list of predefined options. It is toggleable.
Bootstrap 4 comes with predefined classes that can be used to set up a dropdown menu. These classes make it easy to create a dropdown with bootstrap.
How to create a dropdown in Bootstrap 4
Creating a dropdown with Bootstrap 4 is not a very tiring task. Still, a beginner may get stuck since the dropdown may not work if the code isn’t written well. Below is a step-by-step guide to creating a functioning Bootstrap 4 dropdown.
-
Step 1: Create a
divwith class.dropdown. Thisdivis the container that will hold the menu list and the button to access the list. -
Step 2: Create a
buttonwith the class.dropdown-toggleanddata-toggle = "dropdown". Thisbuttonis used to access the dropdown once it is set up. -
Step 3: Create a
divwith class.dropdown-menu. This container will hold our menu list. Each item in the list will have the class.dropdown-item.
<div>
<button type="button" data-toggle="dropdown"> Button </button>
<div>
<a href="#"> List 1</a>
</div>
</div>
We can easily set up a dropdown with the steps given above.
Note: Dropdown-divider is a thin horizontal border used to separate links inside the menu. It is set up by creating a
divwith class.dropdown-divider.
Explanation
- Line 10: We set up a
divwith class.dropdown. This container holds the dropdown. - Line 11: We set up a
button. It has the classesdropdown-toggleanddata-toggle="dropdown", which enable it to control the dropdown menu that follows. - Line 13: We set up a
divwith class.dropdown-menu. It holds the menu lists. - Lines 14-18: We set up the items of the dropdown, with each item having a class
.dropdown-item.
- Line 17: We set up a
dropdown-dividerthat separates the last item.