Bootstrap 3表单填写父级

时间:2013-07-29 09:02:35

标签: html css twitter-bootstrap twitter-bootstrap-3

我试图在Bootstrap 3中编写一个导航栏,有一个导航栏和一些品牌和其他东西,包括其中的搜索表单 我希望搜索表单填充导航栏中的额外空间,我该怎么办?

<div class="navbar navbar-fixed-top">
  <div class="container">
    <a class="navbar-brand" href="#">Brand</a>
    <form class="navbar-form pull-left">
      <input type="search" class="form-control search-box">
      <button type="submit" class="btn btn-default glyphicon glyphicon-search"></button>
    </form>
    <div class="navbar-form btn-group pull-right">
      <button type="button" class="btn btn-default">User</button>
      <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
        <li><a href="#">My Account</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li class="divider"></li>
        <li><a href="#">Logout</a></li>
      </ul>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:4)

Bootstrap 3改变了很多。您需要为输入指定宽度。你可以试试像......

<div class="navbar navbar-fixed-top">
      <div class="container">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">Brand</a>
        <div class="nav-collapse collapse">
          <ul class="nav navbar-nav pull-right">
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">User <b class="caret"></b></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 class="divider"></li>
                <li class="nav-header">Nav header</li>
                <li><a href="#">Separated link</a></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
          <form class="navbar-form">
            <button type="submit" class="btn btn-default glyphicon glyphicon-search"></button> 
            <input type="search" class="form-control pull-left" style="width:70%;"> 
          </form>
        </div><!--/.nav-collapse -->
      </div>
    </div>

Bootply上的演示:http://bootply.com/70142

答案 1 :(得分:1)

因为您的菜单固定在顶部,所以您可以使用以下内容:

<form class="navbar-form navbar-left" 
       style="display:inline; 
              position:absolute; 
              margin-right: 140px;">
    <div class="input-group">
        <input type="text" class="form-control">
        <span class="input-group-btn">
            <button class="btn btn-default" type="button">Search</button>
        </span>
    </div>
</form>

显示:内联用于绝对位置直接位于左侧导航栏品牌旁边,或者可能是导航栏杆导航。 bootstrap的类表单控件将使用100%宽度来填充输入字段的其余导航栏。因为您在表单的右侧有一个下拉列表,右侧会定义一个匹配的边距,因此该元素不会重叠。

示例:on plunker

适用于IE和Chrome,但不幸的是不适用于Firefox和Safari 。想法如何解决这个问题是值得欢迎的。

答案 2 :(得分:0)

试试这个

<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
    <div class="container">
        <ul class="nav">
            <li><a class="brand" href="#">Brand</a></li>
            <li><a href="#">Nav 2</a></li>
            <li><a href="#">Nav 3</a></li>
        </ul>

        <form class="navbar-search pull-left">
            <input type="text" class="search-query" placeholder="Search">
        </form>
        <ul class="nav pull-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">User  <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#">My Account</a></li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                    <li class="divider"></li>
                    <li><a href="#">Logout</a></li>
                </ul>
            </li>
        </ul>
    </div>
</div>