CSS Bootstrap导航栏中心搜索栏,在崩溃时占据整个宽度

时间:2017-01-14 07:03:12

标签: html css twitter-bootstrap navbar

我有一个带有3个元素的bootstrap v.3导航栏。我想均匀对齐元素,以便搜索栏在导航栏中居中。当它崩溃时,它需要完整的widht。但努力做到这一点。

这是它的html:

      <nav class="navbar navbar-transparent navbar-fixed-top" role="navigation">

        <div class="container">
          <!-- Brand and toggle get grouped for better mobile display -->
          <div class="col-lg-4 navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <img src="assets/img/logo.png" alt="logo">
          </div>

          <!-- Collect the nav links, forms, and other content for toggling -->
          <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <form class="navbar-form navbar-left navbar-search-form active" role="search">
               <div class="form-group">
                  <input type="text" value="" class="form-control" placeholder="Search...">
               </div>
            </form>
                    <ul class="nav navbar-nav">
              <li class="active">
                  <a href="javascript:void(0);"><i class="fa fa-search"></i></a>
              </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
              <li><button href="#gsdk" class="btn btn-round btn-default">Sign in</button></li>
            </ul>

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

我试图为它制作一个fiddle,但结果并不好,但希望那里的代码可以给出一些想法。

3 个答案:

答案 0 :(得分:0)

引导程序3中的“text-center”类用于将子元素与中心对齐。

答案 1 :(得分:0)

最简单的方法是使用flex但浏览器兼容性较差,因此您可以将media queriesposition:absolute;用于.navbar-form

position-relative提供给.container内的div nav。我给了它一些边框,所以你可以看到中心。您的sign in按钮位于右上角,因为您已将其navbar-right分类。

&#13;
&#13;
.navbar-header img{
  width:70px;
  margin:0;
}
.navbar-container{
  position:relative;
  border:1px solid red;
}
.navbar-header button span{
  color:black;
  background-color:green;
}
@media screen and (min-width:768px){
  form.navbar-form{
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    margin:0;
  }
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-transparent navbar-fixed-top" role="navigation">
  <div class="container navbar-container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <img src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg" alt="logo">
    </div>
   <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <form class="navbar-form navbar-search-form active" role="search">
        <div class="form-group">
          <input type="text" value="" class="form-control" placeholder="Search...">
          <a href="javascript:void(0);"><i class="fa fa-search"></i></a>
        </div>
      </form>
      <ul class="nav navbar-nav navbar-right">
         <li><button href="#gsdk" class="btn btn-round btn-default">Sign in</button></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

通过一些简单的调整,你可以在没有flex和CSS3属性的情况下实现相同的效果。

检查演示 HERE

试试这个

HTML:

<nav class="navbar navbar-transparent navbar-fixed-top" role="navigation">

  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

      <img class="logo" src="//placehold.it/50x50" alt="logo">
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <form class="navbar-form navbar-search-form active" role="search">
        <div class="form-group">
          <div class="input-group">
            <input type="text" class="form-control" placeholder="Search for...">
            <span class="input-group-btn">
        <button class="btn btn-default" type="button"><i class="fa fa-search"></i></button>
      </span>
          </div>
        </div>
      </form>

      <ul class="nav navbar-nav navbar-right">
        <li><button href="#gsdk" class="btn btn-round btn-default">Sign in</button></li>
      </ul>

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

CSS:

body {
  background-color: #ddd;
}

.navbar {
  border: 1px solid #ccc;
}

.navbar-toggle .icon-bar {
  background-color: #333;
}

.navbar-nav > li {
  line-height: 50px;
  position: relative;
}

.logo, .navbar-nav > li > .btn {
  margin: 0 15px;
}

@media (min-width: 767px) {
  .navbar-search-form {
    position: absolute;
    left: 0;
    right: 0;
    margin: auto;
    width: 100%;
    float: none;
    text-align: center;
    padding: 8px 0;
  }
}