Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap center align a dropdown menu

I have a dropdown button in the center of my page, but when I click the dropdown, the actual dropdown part is still on the left side of the page. What's the issue?

HTML

<div class="row">
    <div class="col-md-12 school-options-dropdown">
            <div class="dropdown">
              <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Choose a School
              <span class="caret"></span></button>
              <ul class="dropdown-menu">
                  <li><a href="#">Add your school</a></li>
                  <li><a href="#">Hello</a></li>
              </ul>
            </div>
    </div>
</div>

CSS

div.school-options-dropdown {
    text-align: center;
}

.dropdown {
    text-align:center;
}

.dropdown-menu {
    text-align: center;
}
like image 295
James Mitchell Avatar asked Jan 29 '17 03:01

James Mitchell


2 Answers

I've found this when trying to solve similar problem. Credit to Vladimir Rodkin

.dropdown-menu-center {
right: auto;
left: 50%;
-webkit-transform: translate(-50%, 0);
-o-transform: translate(-50%, 0);
transform: translate(-50%, 0);}

https://jsfiddle.net/VovanR/2ao5b22h/

like image 38
Filip Avatar answered Sep 16 '22 14:09

Filip


You need to put the dropdown menu and the toggle button inside a .btn-group.

Also, Bootstrap provides the .text-center property to align the contents. This can be used on .school-options-dropdown, instead of manually adding CSS.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<div class="row">
  <div class="col-md-12 school-options-dropdown text-center">
    <div class="dropdown btn-group">

      <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Choose a School
        <span class="caret"></span>
      </button>

      <ul class="dropdown-menu">
        <li><a href="#">Add your school</a></li>
        <li><a href="#">Hello</a></li>
      </ul>

    </div>
  </div>
</div>
like image 69
John Bupit Avatar answered Sep 20 '22 14:09

John Bupit