3

您好我正在尝试在标准引导程序 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>

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

4

1 回答 1

8

将表单容器添加到 中.navbar-header,添加一个col-xs类以设置其大小并使用.pull-right而不是.navbar-right使其始终向右对齐:

代码 -

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

于 2013-10-10T06:46:19.353 回答