使用bootstrap如何将一组导航项目保持在一起

时间:2015-08-08 05:26:35

标签: twitter-bootstrap

我是bootstrap的新手,并且一直试图组建一个基本的导航栏。我遇到的问题是如何在不使用表单元素的情况下将元素保持在一起。

搜索按钮浮动在搜索输入元素下,如jsfiddle this pro-tip

所示

我的预期结果是将输入和按钮并置,如下所示,但同时保持与其他菜单项的距离: here

实现此效果的引导配置(或类)是什么?

我的HTML是:

<nav class="navbar navbar-default" style="margin-top:3px">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <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>
            <div style="width:50px; padding-top:4px; padding-bottom:4px">
                <span>Brand</span>
                <!--<img src="content/images/MaxForms_2.png" />-->
            </div>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">My Action <span class="caret"></span></a>
                <ul class="dropdown-menu">
                    <li><a href="#">Action</a></li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">Separated link</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">One more separated link</a></li>
                </ul>
            </li>

            <li>
                <ul class="">
                    <li>
                        <input type="text" class="form-control" placeholder="Search item">
                        <button type="submit" class="btn btn-default">Search</button>
                    </li>
                </ul>
            </li>

            <li class="input-lg">
                <button type="button" class="btn btn-default" data-bind="click: $root.saveForm, event:{blur: $root.removeStatus}">
                    <span class="glyphicon glyphicon-upload" aria-hidden="true"></span>  Submit Form
                </button>
            </li>
        </ul>

    </div><!-- /.navbar-collapse -->
</nav>

2 个答案:

答案 0 :(得分:2)

我假设您忘记查看此页面。 http://getbootstrap.com/components/#navbar-default

然而,我在我的小提琴中创造了一个新的条目供你跟随。

<nav class="navbar navbar-default" style="margin-top:3px;">
<div class="container-fluid">

  <!-- start your brand -->
  <div class="navbar-header">
    <button type="button" class="hide"> <!-- alt icon for mobile here --> </button>
    <div class="navbar-brand site-brand">
      <span>Brand Name</span>
    </div>
  </div>

  <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <!-- start your menu -->

    <ul class="nav navbar-nav navbar-left">
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Action</a></li>
          <li><a href="#">Another action</a></li>
          <li><a href="#">Something else here</a></li>
          <li role="separator" class="divider"></li>
          <li><a href="#">Separated link</a></li>
          <li role="separator" class="divider"></li>
          <li><a href="#">One more separated link</a></li>
        </ul>
      </li>
    </ul>

    <!-- start your form -->
    <form class="navbar-form navbar-right" role="search">
      <div class="form-group">
        <input type="text" class="form-control" placeholder="Search">
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
      <button type="submit" class="btn btn-default">Another Submit</button>
    </form>
  </div>

</div>
</nav>

JSFiddle Version JSFiddle Version 2

注意:我确实在徽标部分添加了一个类,您可以在这里应用或覆盖您想要的样式...如果此处的所有内容都清楚,请告诉我。

答案 1 :(得分:0)

知道了:输入/按钮块应定义为:

<li>
                    <ul class="nav navbar-form" >
                        <li>
                            <input type="text" class="form-control" placeholder="Search item">
                            <button type="submit" class="btn btn-default">Search</button>
                        </li>
                    </ul>
                </li>

最终的jsfiddle here

我很高兴!