I have reviewed this answer which will help me to accomplish my ultimate goal (by tweaking CSS) but my question is regarding component and styling that bootstrap has provided us.
Problem: The dropdown button is aligned just as required but the dropdown menu is misaligned. Try resizing the window to verify small devices, tablet sizes.
Changing number of columns by modifying col-sm-12
also helps a bit but the misalignment still persists on small devices.
Here's the Minimal, Complete and Verifiable: JsFiddle Example
<div class="container-fluid">
<div class="row justify-content-center text-center">
<div class="col-sm-12 align-self-center">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select Category
</button>
<div class="dropdown-menu"
aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Category1</a>
<a class="dropdown-item" href="#">Category2</a>
<a class="dropdown-item" href="#">Category3</a>
</div>
</div>
</div>
</div>
</div>
The two classes which allow us to center align -
justify-content-center
:
https://v4-alpha.getbootstrap.com/layout/grid/#horizontal-alignment
text-center
: https://v4-alpha.getbootstrap.com/utilities/typography/#text-alignment
Question:
Dropdown button can be positioned in the center of the page by setting the “text-align” property of dropdown div to center. The following example contains a simple Bootstrap dropdown menu with an added class “my-menu”. The property “text-align: center” is added to the class.
Why is my drop down menu not working in bootstrap? Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.
To center div both vertically and horizontally use flexbox utilities. See the examples. Add d-flex align-items-center justify-content-center classes to the parent element to center its content vertically and horizontally.
Just add the class . text-center to the parent element of the text to center content horizontally.
It's because your dropdown menu is positioned absolutely - you need to overwrite it's styles and position it in the center rather than on the left:
.justify-content-center .dropdown-menu {
left: 50%;
transform: translateX(-50%);
}
Updated fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With