Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center aligning Dropdown component in Bootstrap 4 not working properly

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:

  1. Why don't the above classes apply to the dropdown menu but only to the dropdown button?
  2. Are there are any helpful Bootstrap 4 styling classes that I'm missing that would fix that misalignment?
like image 900
Niraj Pandkar Avatar asked Jun 05 '17 12:06

Niraj Pandkar


People also ask

How Align drop down menu in bootstrap Center?

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 bootstrap dropdown not working?

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.

How do I center align a div in bootstrap 4?

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.

How do I center a component in Bootstrap?

Just add the class . text-center to the parent element of the text to center content horizontally.


1 Answers

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

like image 198
Pete Avatar answered Oct 16 '22 11:10

Pete