Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a form element (search box) inside of Bootstrap navbar?

I'm trying to get the search box to be in the center of my Bootstrap navbar. Removing navbar-left seems to destroy the alignment on the navbar. Im going for something like the linkedin.com page. Whats the easiest way to do this?
Heres a link to a JSFiddle of my navbar https://jsfiddle.net/b6pmy9w4/. Thanks in advance for any help.
Html Code:

<nav class="navbar navbar-default" style="border-radius: 0;">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed burger-button" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand jura-font" href="/#!/">COLIGN</a>
                </div>
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <form class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search">
                        </div>
                        <!-- <button type="submit" class="btn btn-default">Submit</button> -->
                    </form>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#"><span class="glyphicon glyphicon-comment"></span></a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-user"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">View Profile</a></li>
                                <li><a href="#">Manage Account</a></li>
                                <li><a href="#">Invite a friend</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="/auth/signout">Log out</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container-fluid -->
        </nav>
like image 653
Daniel Kobe Avatar asked Jun 28 '15 00:06

Daniel Kobe


2 Answers

Don't know if it's the best way to do it but

  • Remove the navbar-left
  • Move it after the ul
  • Set text-align:center

Updated fiddle

like image 82
Bobby Tables Avatar answered Sep 22 '22 19:09

Bobby Tables


I know this post is old now, but on Bootstrap4 I just added the ml-auto class to the form tag and it worked perfectly:

<form class="form-inline my-2 my-lg-0 ml-auto">

You can also add the col classes to change the size of the form input and the button.

like image 30
Austin not from Boston Avatar answered Sep 21 '22 19:09

Austin not from Boston