使div垂直居中

时间:2014-07-24 17:15:29

标签: css twitter-bootstrap twitter-bootstrap-3 vertical-alignment

我试图在bootstrap <header>中垂直对齐div中心,但它不起作用(请在投票之前访问JSFiddle链接!):

<div style="display: table-cell; height: 50px; vertical-align: middle;">
    <button type="button" class="btn btn-default navbtn">
        <span class="glyphicon glyphicon-leaf"></span>
    </button>
    <button type="button" class="btn btn-default navbtn" >
        <span class="glyphicon glyphicon-cog"></span>
    </button>
    <div id="navbar-search">
        <button type="button" class="btn btn-default navbar-search-btn" >
            <span class="fa fa-search"></span>
        </button>
        <input type="search" class="navsearch" />
    </div>
</div>

当我删除navbar-search div按钮没问题。 enter image description here

JSFiddle:http://jsfiddle.net/maysamsh/Rp94k/1/

注意:搜索容器有display:inline-block;并使其贴在顶部,我无法将其删除。

2 个答案:

答案 0 :(得分:1)

您需要将.navbar-btn添加到按钮以垂直居中。有关详细信息,请参阅Bootstrap Docs

语义上如果它是导航,它应该在nav HTML5元素中。无论如何,你的列太宽了,正在按下图标(占据宽度100%的列不会留下任何空间)。

使用相当多的基础Bootstrap

查看Fiddle
  <nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <div class="row"> 
        <div class="col-sm-6">
            <div class="row">
            <div class="col-sm-5">1</div>
            <div class="col-sm-2">2</div>
            <div class="col-sm-5">3</div>
            </div>
        </div>
        <div class="col-sm-6 navbar-right">
            <div class="row">
      <div class="col-sm-8">
          <form class="navbar-form navbar-right" role="search">
              <div class="input-group">
                  <input type="text" class="form-control" placeholder="Search" name="q">
                      <div class="input-group-btn">
                          <button class="btn btn-default" type="submit">
                              <i class="glyphicon glyphicon-search">
                              </i>
                          </button>
                      </div>
                  </div>
              </form><!--/form-->
          </div><!--/form column-->
          <div class="col-sm-4">
              <button type="button" class="btn btn-default navbtn navbar-btn">
                  <span class="glyphicon glyphicon-leaf"></span>
              </button> 
              <button type="button" class="btn btn-default navbtn  navbar-btn" >
                  <span class="glyphicon glyphicon-cog"></span>
              </button>
            </div><!--/buttons column-->
            </div><!--/row column-->
        </div><!--/right column-->
      </div><!--/row-->
   </div><!--/container-->
</nav> <!--/nav-->

答案 1 :(得分:1)

HTML

<header class="navbar">
        <div class="container-fluid expanded-panel">
            <div class="row" dir="rtl">
                <div class="col-sm-5">
                    3

                </div>
                <div class="col-sm-2">
                    2
                </div>
                <div class="col-sm-5">
                    <div style="display: table;">

                        <button type="button" class="btn btn-default navbtn">
                            <span class="glyphicon glyphicon-leaf"></span>
                        </button>
                        <button type="button" class="btn btn-default navbtn" >
                            <span class="glyphicon glyphicon-cog"></span>
                        </button>
                        <div id="navbar-search" style="display: table-cell;">
                            <button type="button" class="btn btn-default navbar-search-btn" >
                                <span class="fa fa-search"></span>
                            </button>
                            <input type="search" class="navsearch" />
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </header>

CSS

.navbar{
    background-color:#fff;
    border-bottom:#eee solid 1px;
}
.navbtn {
    font-size:large;
    margin-left: 5px;
}
.navsearch{
    text-align: right;
    -webkit-border-radius: 15px 0 0 15px;
    border-radius: 15px 0 0 15px;
    outline: none;
    background: #eee;
    cursor: inherit;
    display:inline-block;
    border:none;
    padding:5px;
    height:35px;
}
#navbar-search{
    display:inline-block;
    float:none;
    margin-top:10px;
}
.navbar-search-btn{
    -webkit-border-radius: 0 15px 15px 0;
    border-radius: 0 15px 15px 0;
    display:inline;
    float:right;
    border:none;
    background: #eee;
    height:35px;

}

小提琴:http://jsfiddle.net/monzoor/TC9UH/1/