Bootstrap 3 search bar

Issue

Hi I’m trying to include a search bar in a standard bootstrap 3 template. I’m trying to implement it in such a way that when the menu collapses at a smaller resolution the search bar stays in the navigation bar and the rest of the menu buttons collapse. I got it working perfectly on desktop resolution but when I switch to the mobile resolution the search bar shows up underneath my title and nav button. This is the HTML I’m using at the moment.

<div class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">My Project</a>
    </div>
    <div class="col-sm-3 col-md-3 navbar-right">
      <form class="navbar-form" role="search">
        <div class="input-group">
          <input type="text" class="form-control" placeholder="Search" name="srch-term" id="srch-term">
          <div class="input-group-btn">
            <button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
          </div>
        </div>
      </form>
    </div>
    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">Browse Courses</a></li>
        <li><a href="#contact">Active Courses</a></li>
        <li><a href="#contact">Shopping Cart</a></li>
        <li><a href="#contact">Recently Viewed</a></li>
      </ul>
    </div><!--/.navbar-collapse -->
  </div>
</div>

How can I make sure the searchbar doesn’t disappear or show up as a second row when the menu collapses? Many thanks

Solution

Add the form container into the .navbar-header, add a col-xs class to set its size and use .pull-right instead of .navbar-right to make it always align to the right:

Code –

.navbar .navbar-form{
    padding: 0 15px;
    border: 0;
    -webkit-box-shadow: none;
    box-shadow: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>


<div class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">My Project</a>
      <div class="col-xs-5 col-sm-3 pull-right">
        <form class="navbar-form" role="search">
          <div class="input-group">
            <input type="text" class="form-control" placeholder="Search" name="srch-term" id="srch-term">
            <div class="input-group-btn">
              <button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
            </div>
          </div>
        </form>
      </div>
    </div>
    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">Browse Courses</a></li>
        <li><a href="#contact">Active Courses</a></li>
        <li><a href="#contact">Shopping Cart</a></li>
        <li><a href="#contact">Recently Viewed</a></li>
      </ul>
    </div><!--/.navbar-collapse -->
  </div>
</div>

Answered By – omma2289

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published