skip to Main Content

I have a nabvar that contains a search field ‘navbarSearchQuery’. When the navbar collapses when the screen shrinks to a phone size I want ‘navbarSearchQuery’ to remain visible in the navbar. Is there something I can do with @media queries to keep it visible or do I have to move it outside the collapsable section in the navbar?

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <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 hidden-xs" href="/Home">Yoga</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Browse <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Popular</a>
            </li>
            <li><a href="#">Friends</a>
            </li>
            <li><a href="#">Groups</a>
            </li>
            <li><a href="#">Neighborhoods</a>
            </li>
            <li><a href="#">About <span class="sr-only">(current)</span></a>
            </li>
            <li><a href="#">Contact</a>
            </li>
          </ul>
        </li>
        <li>
          <div class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search" id="navbarSearchQuery" name="location">
            </div>
          </div>
        </li>
      </ul>
      @Html.Partial("_LoginPartial")
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

2

Answers


  1. Chosen as BEST ANSWER

    I can put the navbar in the header part but when I shrink the alignment is slightly off. Can this be fixed?

    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <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 hidden-xs" href="/Home">Yoga</a>
      <div class="navbar-form navbar-left navbar-search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search" id="navbarSearchQuery" name="location">
        </div>
      </div>
    </div>

    before and after pics. you can see the button isn't aligned correctly

    enter image description here

    enter image description here


  2. You can create a form inside the navbar-header.

    /**Custom**/
    
    .navbar.navbar-custom {
      height: 50px;
      border: none;
    }
    .navbar-custom .formSearch {
      width: 300px;
      float: left;
      display: block;
      position: absolute;
      padding-top: 7px;
      margin-left: 160px;
    }
    .navbar-custom .inner-addon {
      position: relative;
    }
    .navbar-custom .inner-addon .glyphicon {
      position: absolute;
      padding: 10px;
      pointer-events: none;
      color: black;
    }
    .navbar-custom .left-addon .glyphicon {
      left: 0px;
    }
    .navbar-custom .left-addon input {
      padding-left: 30px;
    }
    @media (max-width: 767px) {
      .navbar-custom .formSearch {
        padding-left: 15px;
        width: 70%;
        margin-left: 0;
      }
      .navbar-custom .navbar-collapse {
        background: #f5f5f5;
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <nav class="navbar navbar-default navbar-custom">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" 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 hidden-xs" href="/Home">Yoga</a>
    
          <form class="formSearch" role="search">
            <div class="inner-addon left-addon"> <span class="glyphicon glyphicon-search"></span>
    
              <input type="text" class="form-control" placeholder="Search" id="navbarSearchQuery" name="location">
            </div>
          </form>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Browse <span class="caret"></span></a>
    
              <ul class="dropdown-menu" role="menu">
                <li><a href="#">Popular</a>
    
                </li>
                <li><a href="#">Friends</a>
    
                </li>
                <li><a href="#">Groups</a>
    
                </li>
                <li><a href="#">Neighborhoods</a>
    
                </li>
                <li><a href="#">About <span class="sr-only">(current)</span></a>
    
                </li>
                <li><a href="#">Contact</a>
    
                </li>
              </ul>
            </li>
          </ul>
        </div>
        <!-- /.navbar-collapse -->
      </div>
      <!-- /.container-fluid -->
    </nav>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search