使用bootstrap 3.3

时间:2015-07-10 14:41:41

标签: html css twitter-bootstrap

我希望我的搜索框出现在桌面和移动设备的导航栏中。目前,当我减小宽度时,搜索栏会下降到下一行。我无法弄清楚如何让它与导航栏齐平。

以下是代码:http://plnkr.co/edit/Unr6MEiIkF6WpXwCCgkF?p=preview

我可以通过在导航栏标题中插入第二个搜索框来实现所需的效果。部分,但由先行代码引起的疯狂。

代码

这是我的HTML的header部分:

  <header>
    <div class="navbar navbar-default navbar-static-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button class="navbar-toggle collapsed" data-target=".navbar-collapse" data-toggle="collapse" type="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <div class="hidden-xs">
            <a class="navbar-brand" href="/">Some Project</a>
          </div>
          <div class="visible-xs">
            <a class="navbar-brand" href="/">P</a>
          </div>
        </div>
        <div class="navigation">
          <div class="pull-right" id="navbar-search-area">
            <form class="navbar-form site-search" id="new_search" action="/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
              <div class="form-group">
                <div class="icon-addon addon-sm">
                  <input id="search" placeholder="search" input-html="col-xs-5 col-sm-3 col-md-4" type="text" name="search[term]" />
                  <label class="glyphicon glyphicon-search" rel="tooltip" title="search"></label>
                </div>
              </div>
            </form>
          </div>
          <div class="collapse navbar-collapse header-collapse" id="navbar-section-1">
            <ul class="nav navbar-nav">
              <li>
                <a href="/things">Pick Me!</a>
              </li>
              <li>
                <a href="/thing/2">me me me me!</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </header>

任何指导都将不胜感激。

参考

其他人也遇到过类似的问题,但他们看起来有点不同,很大程度上是因为没有超级标准的方式来做这件事,而且因为人们希望他们的网页以不同的方式行事。

Aligning search bar for mobile with bootstrap 3

[update]更新了plunkr中的css,以显示导航栏以不同的背景颜色在顶部延伸。

1 个答案:

答案 0 :(得分:3)

将搜索和折叠的导航栏拉出标题并右键对齐它们似乎可以解决问题

http://plnkr.co/edit/B4gIfAkNiwkBId9fbLVi?p=preview

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <title>AngularJS Plunker</title>
    <!-- your app.js file -->
    <script src="app.js"></script>
    <!-- bootstrap css -->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
    <!-- your style.css file -->
    <link href="style.css" rel="stylesheet" />
  </head>

  <body class="pages welcome">
      <style>
        body { background-color: #F9F7F5; }
      </style>
      <header>
        <div class="navbar navbar-default navbar-static-top" role="navigation">

          <button class="pull-right navbar-toggle collapsed" data-target=".navbar-collapse" data-toggle="collapse" type="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>

          <div class="pull-right" id="navbar-search-area">
            <form class="navbar-form site-search" id="new_search" action="/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
              <div class="form-group">
                <div class="icon-addon addon-sm">
                  <input id="search" placeholder="search" input-html="col-xs-5 col-sm-3 col-md-4" type="text" name="search[term]" />
                  <label class="glyphicon glyphicon-search" rel="tooltip" title="search"></label>
                </div>
              </div>
            </form>
          </div>

          <div class="container">
            <div class="navbar-header">
              <div class="hidden-xs">
                <a class="navbar-brand" href="/">Some Project</a>
              </div>
              <div class="visible-xs">
                <a class="navbar-brand" href="/">P</a>
              </div>
            </div>
            <div class="navigation">
              <div class="collapse navbar-collapse header-collapse" id="navbar-section-1">
                <ul class="nav navbar-nav">
                  <li>
                    <a href="/things">Pick Me!</a>
                  </li>
                  <li>
                    <a href="/thing/2">me me me me!</a>
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </header>
      <div class="container">
        <div class="row">
          <div class="col-md-8 col-md-push-2">
            <h1 class="page-title">Welcome to the Project</h1>
            <p>This is the body.</p>
          </div>
        </div>
      </div>
  </body>
</html>