Html Bootstrap 3 - 如何最大化导航栏中的输入宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18552714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:53:24  来源:igfitidea点击:

Bootstrap 3 - How to maximize input width inside navbar

htmlcssnavbartwitter-bootstrap-3html-input

提问by Oren

Right now I have a navigation bar that looks like: http://bootply.com/78239

现在我有一个导航栏,看起来像:http: //bootply.com/78239

I want to maximize the width of the "search" text-input, without creating line-breaks (i.e., that the "Clear" link will be tight with the "About" link).

我想最大化“搜索”文本输入的宽度,而不创建换行符(即“清除”链接将与“关于”链接紧密相连)。

I only have basic experience with css and web-design. I read something about "overflow: hidden" tricks, but I don't understand how to use it when there are more elements to the right of the targeted element.

我只有 css 和网页设计的基本经验。我读了一些关于“ overflow: hidden”的技巧,但是当目标元素右侧有更多元素时,我不明白如何使用它。

Thanks

谢谢



EDIT:

编辑:

A partial solution can be to set the width "manually" for each case, like in http://bootply.com/78247:

部分解决方案可以是为每种情况“手动”设置宽度,例如http://bootply.com/78247

@media (max-width: 767px) {
  #searchbar > .navbar-form {
    width: 100%;
  }
}

@media (min-width: 768px) and (max-width: 991px) {
  #searchbar > .navbar-form {
    width: 205px;
  }
}

@media (min-width: 992px) and (max-width: 1199px) {
  #searchbar > .navbar-form {
    width: 425px;
  }
}

@media (min-width: 1200px) {
  #searchbar > .navbar-form {
    width: 625px;
  }
}

but this solution will not work when the menu's texts are "dynamic" (for example, contain "Hello username").

但是当菜单的文本是“动态的”(例如,包含“Hello username”)时,此解决方案将不起作用。

I suppose it is not a big issue if you assume that there is a limit on the menu's texts' widths - it's just annoying to calculate/test those widths manually. I'm just curious to know if there's a neat way to do it automatically.

如果您认为菜单文本的宽度有限制,我想这不是什么大问题 - 手动计算/测试这些宽度很烦人。我只是想知道是否有一种巧妙的方法可以自动执行此操作。

回答by Bass Jobsen

For Bootstrap version 3.2 and up you should also set the display:table;for the input-groupand set the width to 1%for the input-group-addon.

对于 Bootstrap 3.2 及更高版本,您还应该设置display:table;forinput-group并将宽度设置1%input-group-addon

<div style="display:table;" class="input-group">
            <span style="width: 1%;" class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
            <input type="text" autofocus="autofocus" autocomplete="off" placeholder="Search Here" name="search" style="" class="form-control">
          </div>

Demo Bootstrap version 3.2+: http://www.bootply.com/t7O3HSGlbc

演示 Bootstrap 3.2+ 版:http: //www.bootply.com/t7O3HSGlbc

--

——

If you allow changing the position of your navbar elements? I don't understand "without creating line-breaks (i.e., that the "Clear" link will be tight with the "About" link)." Try this: http://bootply.com/78374.

如果您允许更改导航栏元素的位置?我不明白“不创建换行符(即“清除”链接将与“关于”链接紧密相连)。试试这个:http: //bootply.com/78374

What i did:

我做了什么:

  • Drop the float left of the form (by dropping the navbar-left class)
  • Give other navbar elements a right float (the navbar-right class)
  • Set display of the form-group to inline (style="display:inline")
  • Change the position of the elements (the right floated first)
  • 删除表单左侧的浮动(通过删除导航栏左侧的类)
  • 给其他导航栏元素一个正确的浮动(navbar-right 类)
  • 将表单组的显示设置为内联 ( style="display:inline")
  • 改变元素的位置(右边先浮动)

Related question:

相关问题:

html:

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="navbar-collapse collapse" id="searchbar">

      <ul class="nav navbar-nav navbar-right">
        <li><a href="about.html">About</a></li>
        <li id="userPage">
          <a href="#@userpage"><i class="icon-user"></i> My Page</a>
        </li>
        <li><a href="#logout" data-prevent="">Logout</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#" title="Start a new search">Clear</a></li>
      </ul>



     <form class="navbar-form">
        <div class="form-group" style="display:inline;">
          <div class="input-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
            <input class="form-control" name="search" placeholder="Search Here" autocomplete="off" autofocus="autofocus" type="text">
          </div>
        </div>
      </form>

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

回答by thouliha

Two things worked for me here. Adding Orens media queries, and also adding a display: inlineto that searchbar form group:

有两件事对我有用。添加 Orens 媒体查询,display: inline并向该搜索栏表单组添加一个:

<li id="searchbar">
          <form id="search_form" class="navbar-form navbar-left navbar-input-group " role="search">
            <div class="input-group">
              <div class="input-group-btn">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">All <span class="caret"></span>
                </button>
              </div>
              <div class="form-group">
                <input name="search_input" type="text" class="form-control typeahead" placeholder="Search for items or shops" autofocus="autofocus">
              </div>
              <span class="input-group-btn">
          <button type="submit" class="btn btn-default"><i class="fa fa-search"></i>
          </button>
          </span>
            </div>
          </form>
        </li>

With the css:

使用CSS:

#search_form > .input-group > .form-group {
  display: inline;
}