Bootstrap 3搜索栏

时间:2013-10-09 21:31:19

标签: html css twitter-bootstrap navbar collapse

您好我正在尝试在标准bootstrap 3模板中包含搜索栏。我试图以这样的方式实现它:当菜单以较小的分辨率折叠时,搜索栏停留在导航栏中,其余菜单按钮崩溃。我得到了完美的桌面分辨率,但当我切换到移动分辨率时,搜索栏显示在我的标题和导航按钮下方。这是我目前正在使用的HTML。

<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>

当菜单崩溃时,如何确保搜索栏不会消失或显示为第二行?非常感谢

1 个答案:

答案 0 :(得分:8)

将表单容器添加到.navbar-header,添加col-xs类来设置其大小,并使用.pull-right代替.navbar-right使其始终与右对齐:

代码 -

&#13;
&#13;
.navbar .navbar-form{
    padding: 0 15px;
    border: 0;
    -webkit-box-shadow: none;
    box-shadow: none;
}
&#13;
<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>
&#13;
&#13;
&#13;