Drop-downs are basically work's like a toggle button.
We can create drop-downs by using JavaScript and Bootstrap standard plugins.
Drop-downs are toggle but we have to click hover effect is not enough.
Various kind of third parties use popper.js or popper.min.js libraries, they provide dynamic features to our drop-downs
In Bootstrap we use bootstrap.bundle.min.js or bootstrap.bundle.js libraries
Single Button
Here we are using this drop down in the button element.
<div class ="dropdown" >
<button class ="btn btn-danger dropdown-toggle" type ="button" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false" >
My Single Dropdown
</button>
<div class ="dropdown-menu" aria-labelledby="dropdownMenuButton" >
<a class ="dropdown-item" href="#" >let's </a>
<a class ="dropdown-item" href="#" >keep</a>
<a class ="dropdown-item" href="#" >Coding </a>
</div>
Single Dropdown with <a> tag.
<div class ="dropdown" >
<a class ="btn btn-primary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
Single Dropdown
</a>
<div class ="dropdown-menu" aria-labelledby="dropdownMenuLink" >
<a class ="dropdown-item" href="https://www.codesfamily.com/" >let's </a>
<a class ="dropdown-item" href="https://www.codesfamily.com/" >keep</a>
<a class ="dropdown-item" href="https://www.codesfamily.com/" >Coding </a>
</div>
</div>
Try Yourself
Split Button
In Bootstrap we can also create split button dropdown by using .dropdown-toggle-split class.We can also manage margin and padding according to our requirement.
<div class ="btn-group" >
<button type ="button" class ="btn btn-success" >Action </button>
<button type ="button" class ="btn btn-danger dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
<span class ="sr-only" >My Dropdown </span>
</button>
<div class ="dropdown-menu" >
<a class ="dropdown-item" href="#" >Let 's </a>
<a class ="dropdown-item" href="#" >Keep </a>
<a class ="dropdown-item" href="#" >Coding </a>
<div class ="dropdown-divider" ></div>
<a class ="dropdown-item" href="#" >share link</a>
</div>
</div>
Sizing
In Bootstrap we can customize the size of dropdown according to our requirement by using .drop element.
<div class ="btn-group" >
<button class ="btn btn-secondary btn-lg" type ="button" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false" >
Large Button
</button>
<div class ="dropdown-menu" >
...
</div>
</div>
<div class ="btn-group" >
<button class ="btn btn-primary btn-lg dropdown-toggle" type ="button" >
split button
</button>
<button type ="button" class ="btn btn-sm btn-primary dropdown-toggle-split" dggle="dropdown" aria-haspopup="true" aria-expanded="false" >ata-to
<span class ="sr-only" >Toggled Dropdown </span>
</button>
<div class ="dropdown-menu" >
...
</div>
</div>
Try Yourself
Direction
We can set the direction of the dropdown as well in bootstrap.
Dropup
To set the dropdown as a dropup we have to use .dropup element.
<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 >
<button type ="button" class ="btn btn-success 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 >
Dropright
To set the dropdown as a Dropright we have to use .dropright element.
<div class ="btn-group dropright" >
<button type ="button" class ="btn btn-danger 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-danger" >
Split dropright
</button >
<button type ="button" class ="btn btn-danger 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 >
Dropleft
To set the dropdown direction as a Dropleft we have to use .dropleft element.
<div class ="btn-group dropleft" >
<button type ="button" class ="btn btn-primary dropdown-toggle" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" >
Dropleft Dropdown
</button >
<div class ="dropdown-menu" >
</div >
</div >
<div class ="btn-group" >
<div class ="btn-group dropleft" role ="group" >
<button type ="button" class ="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle ="dropdown" aria-haspopup ="false" aria-expanded ="false" >
<span class ="sr-only" > Toggle Dropleft Dropdown</span >
</button >
<div class ="dropdown-menu" >
</div >
</div >
<button type ="button" class ="btn btn-primary" >
Split dropleft dropdown
</button >
</div >
Try Yourself
Menu Items In Dropdown
In Bootstrap we initially we used only tag inside the dropdowns but now we can also use button element inside the dropdown.
<div class ="dropdown " >
<button class ="btn btn-danger dropdown-toggle" type ="button" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false" >
Dropdown
</button>
<div class ="dropdown-menu" aria-labelledby="dropdownMenu2" >
<button class ="dropdown-item" type ="button" >Let 's </button>
<button class ="dropdown-item" type ="button" >Keep </button>
<button class ="dropdown-item" type ="button" >Coding </button>
</div>
</div>
Active
In Bootstrap we can also add styles by using .active element in dropdowns.
<div class ="dropdown-menu" >
<a class ="dropdown-item" href="#" >Java </a>
<a class ="dropdown-item active" href="#" >Python </a>
<a class ="dropdown-item" href="#" >C ++</a>
</div>
Disable
In Bootstrap , If we don't want to use styles in the dropdown so we can disable as well by using .disable element in dropdown.
<div class ="dropdown-menu" >
<a class ="dropdown-item" href="#" >Common link</a>
<a class ="dropdown-item disabled" href="#" tabindex="-1" aria-disabled="true" >Disabled link Dropdown </a>
<a class ="dropdown-item" href="#" >Next link</a>
</div>
Try Yourself
Menu Alignment
In bootstrap , dropdowns are automatically positioned by default 100%.
We can set custom alignment as well according to our requirement, by using dropdown-menu-right
<div class ="btn-group" >
<button type ="button" class ="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
Right -aligned menu
</button>
<div class ="dropdown-menu dropdown-menu-right" >
<button class ="dropdown-item" type ="button" >java</button>
<button class ="dropdown-item" type ="button" >Python </button>
<button class ="dropdown-item" type ="button" >C ++</button>
</div>
</div>
Try Yourself
Dividers In Bootstrap
In Bootstrap, if we seprate the group of multiple menus known as dividers.
<div class ="dropdown-menu" >
<a class ="dropdown-item" href="#" >Java </a>
<a class ="dropdown-item" href="#" >Python </a>
<a class ="dropdown-item" href="#" >C ++</a>
<div class ="dropdown-divider" ></div>
</div>
Text
We can also add custom text in the dropdown menu according to our requirement and we can set custom height and width as well.
<div class ="dropdown-menu p-4 text-muted" style ="max-width: 200px;" >
<p >
Codes Family.
</p >
<p class ="mb-0" >
Is just like your family
</p >
</div >
Forms
In the bootstrap we can also set form elements inside the dropdown menu according to our requirement.
<div class ="dropdown-menu" >
<form class ="px-4 py-3" >
<div class ="form-group" >
<label for ="UserName" >UserName </label>
<input type ="text" class ="form-control" id="user" placeholder="Enter your name." >
</div>
<div class ="form-group" >
<label for ="exampleDropdownFormEmail1" >Email </label>
<input type ="email" class ="form-control" id="MyEmail1" placeholder="Enter your Email" >
</div>
<div class ="form-group" >
<label for ="exampleDropdownFormPassword1" >Password </label>
<input type ="password" class ="form-control" id="MyPassword" placeholder="Enter yourPassword" >
</div>
<button type ="submit" class ="btn btn-secondary" >Sign in</button>
</form>
<div class ="dropdown-divider" ></div>
<a class ="dropdown-item" href="#" >If you are new here please? Sign up</a>
<a class ="dropdown-item" href="#" >If you Forgot password??</a>
</div>