简体   繁体   English

如何在Bootstrap导航栏中居中放置项目

[英]How to center items in Bootstrap navbar

I've tried my best but unfortunately I am not able to position the Text Center 1 and 2 elements in the middle of the navbar. 我已经尽力了,但是很遗憾,我无法将文本中心1和2元素放置在导航栏的中间。 Hope you can help me out. 希望你能帮助我。 Thanks. 谢谢。

 <div class="navbar navbar-default navbar-bottom" role="navigation"> <div class="container"> <div class="navbar-text pull-left"> <p>Left</p> </div> <div class="navbar-text ???"> <a href="#"><i>Text Center 1</i></a> <a href="#"><i>Text Center 2</i></a> </div> <div class="navbar-text pull-right"> <a href="#"><i class="fa fa-facebook-square fa-2x"></i></a> <a href="#"><i class="fa fa-twitter fa-2x"></i></a> </div> </div> </div> 

Bootstrap has a predefined centered text class text-center that you can add to any element/div. Bootstrap具有预定义的居中文本类text-center ,您可以将其添加到任何元素/ div。

example: <div class="navbar-text text-center"> 示例: <div class="navbar-text text-center">

View all bootstrap alignment classes here: http://getbootstrap.com/css/#type-alignment 在此处查看所有引导程序对齐类: http : //getbootstrap.com/css/#type-alignment

Just give the div that you want to center the text-align:CENTER; 只需将要居中的div赋予text-align:CENTER; property. 属性。

Sometimes it is just easier to use plain CSS instead of Bootstrap. 有时,使用纯CSS而不是Bootstrap会更容易。

 div.center{ text-align:CENTER; } 
 <div class="navbar navbar-default navbar-bottom" role="navigation"> <div class="container"> <div class="navbar-text pull-left"> <p>Left</p> </div> <div class="navbar-text center"> <a href="#"><i>Text Center 1</i></a> <a href="#"><i>Text Center 2</i></a> </div> <div class="navbar-text pull-right"> <a href="#"><i class="fa fa-facebook-square fa-2x"></i></a> <a href="#"><i class="fa fa-twitter fa-2x"></i></a> </div> </div> </div> 

Class text-center helps you to center Text Center links only when screen-size is less then 768px (but in this case social links fall down!), if screen-size is more 768px, bootstrap's class navbar-text has property float: left and your links will be left side of navbar. text-center类可帮助您仅在屏幕大小小于768px时使Text Center链接居中(但在这种情况下社交链接会掉落!),如果屏幕大小大于768px,则引导程序的类navbar-text具有float: left属性float: left并且您的链接将在导航栏的左侧。 To prevent its behavior, you need to apply float: none and display: inline-block properties for middle navbar: 为防止其行为,您需要对中间导航栏应用float: nonedisplay: inline-block属性:

<div class="navbar navbar-default navbar-bottom" role="navigation">
  <div class="container text-center">
    <div class="navbar-text pull-left">
      <p>Left</p>
    </div>
    <div class="navbar-text myClass">
      <a href="#"><i>Text Center 1</i></a>
      <a href="#"><i>Text Center 2</i></a>
    </div>
    <div class="navbar-text pull-right">
      <a href="#"><i class="fa fa-facebook-square fa-2x"></i></a>
      <a href="#"><i class="fa fa-twitter fa-2x"></i></a>
    </div>
  </div>
</div>
.myClass {
  float: none;
  display: inline-block;
}

class text-center in this case is used for div.container 在这种情况下,类text-center用于div.container

JSFiddle-example 的jsfiddle,例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM