Bootstrap 3 - 如何最大化导航栏中的输入宽度 [英] Bootstrap 3 - How to maximize input width inside navbar

查看:101
本文介绍了Bootstrap 3 - 如何最大化导航栏中的输入宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个如下所示的导航栏:http://bootply.com/78239

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

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

谢谢

<小时>

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

@media (max-width: 767px) {#searchbar >.navbar-form {宽度:100%;}}@media (min-width: 768px) 和 (max-width: 991px) {#searchbar >.navbar-form {宽度:205px;}}@media (min-width: 992px) 和 (max-width: 1199px) {#searchbar >.navbar-form {宽度:425px;}}@media(最小宽度:1200px){#searchbar >.navbar-form {宽度:625px;}}

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

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

解决方案

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

<span style="宽度: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">

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

--

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

我做了什么:

  • 将浮动元素放在表单的左侧(通过删除导航栏左侧的类)
  • 给其他导航栏元素一个正确的浮动(navbar-right 类)
  • 将表单组的显示设置为内联 (style="display:inline")
  • 改变元素的位置(右边先浮动)

相关问题:

html:

<div class="navbar-collapse collapse" id="searchbar"><ul class="nav navbar-nav navbar-right"><li><a href="about.html">关于</a></li><li id="userPage"><a href="#@userpage"><i class="icon-user"></i>我的页面<li><a href="#logout" data-prevent="">注销</a></li><ul class="nav navbar-nav navbar-right"><li><a href="#" title="开始新搜索">清除</a></li><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><!--/.nav-collapse -->

Right now I have a navigation bar that looks like: 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.

Thanks


EDIT:

A partial solution can be to set the width "manually" for each case, like in 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").

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.

解决方案

For Bootstrap version 3.2 and up you should also set the display:table; for the input-group and set the width to 1% for the 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

--

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.

What i did:

Related question:

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>

这篇关于Bootstrap 3 - 如何最大化导航栏中的输入宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆