I am not able to align the dropdown to the center of the parent on which the dropdown is opened with hover. I have tried to text-align: center for the entire .nav .navbar li elements, and it doesn't work. I have tried to set margin and left: 50% for the entire ul.dropdown-menu that is the dropdown and it doesn't work. Here is the html code:
<ul class="nav navbar-nav navbar-right" id="headerDropdowns"> <li><div class="btn-toolbar"> <div class="btn-group"> <button class="btnCustom" data-toggle="dropdown" data-hover="dropdown" data-delay="1000">Our Difference</button> <ul class="dropdown-menu"> <li><a tabindex="-1" href="#">Made in the USA</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Human Grade</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Ingredients Fresh</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">USDA Meats</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Our Story</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Campare</a></li> </ul> </div> </li> </ul>
In which class dropdown-menu, or nav navbar-nav, should I do the aligment, and what should I set?
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.
HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element. Use a container element (like <div>) to create the dropdown content and add whatever you want inside of it. Wrap a <div> element around the elements to position the dropdown content correctly with CSS.
Use the CSS text-align Property to Center a Form in HTML We can set the value to center to center the form. For example, apply the text-align property to the form tag in the style attribute, and set the property to center . Next, create input tags with the type text and then submit .
You can use transform to avoid having to use fixed widths:
.dropdown-menu { left: 50%; right: auto; text-align: center; transform: translate(-50%, 0); }
Answer influenced by http://css-tricks.com/centering-percentage-widthheight-elements/
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