简体   繁体   English

修改twitter bootstrap导航栏

[英]Modify twitter bootstrap navbar

Ive been trying to modify the twitter bootstrap navbar, at the moment all the links are aligned to the left, when what i would really like is the have them central. 我一直试图修改twitter bootstrap导航栏,此刻所有链接都对齐到左边,当我真正想要的是将它们放在中心位置时。

In a different post i read that you use this 在另一篇文章中我读到你使用它

.tabs, .pills {
  margin: 0 auto;
  padding: 0;
  width: 100px;
}

But this did not work for me 但这对我不起作用

What do i need to change in the css to make this happen, i understand i put the modified css in the bootstrap and overrides. 我需要在css中更改以实现此目的,我理解我将修改后的CSS放在引导程序中并覆盖。

Any help appreciated 任何帮助赞赏

this is my markup 这是我的标记

layouts/application 布局/应用

<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container-fluid">
      <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      </a>

    <a class="brand">Newbridges</a>  

  <% if user_signed_in? %>
    <div class="nav-collapse">
      <ul class="nav ">
      <%= render "shared/navbarlogin" %>
    </div>

    <% else%>
    <div class="nav-collapse">
      <ul class="nav">

      <%= render "shared/navbar" %>
    </div>
    <% end %>

I've also tried this 我也试过这个

.nav > li {
  float: none;
  display: inline-block;
  *display: inline;
  /* ie7 fix */
  zoom: 1;
  /* hasLayout ie7 trigger */
}
.nav {
  text-align: center;
}

You can center your nav menu by setting your menu items to display:inline-block instead of float:left like so: 您可以通过设置要display:inline-block的菜单项来居中导航菜单display:inline-block而不是float:left如下:

.navbar .nav,
.navbar .nav > li {
    float:none;
    display:inline-block;
    *display:inline; /* ie7 fix */
    *zoom:1; /* hasLayout ie7 trigger */
    vertical-align: top;
}

 .navbar-inner {
    text-align:center;
}

Though i suggest you create your own class to target your navbar menu that you wish to center, this way you won't bother the bootstrap default values and mess with other nav sections you may have in your page. 虽然我建议您创建自己的类来定位您希望居中的导航栏菜单,但这样您就不会打扰引导程序默认值,并且可能会混淆页面中可能包含的其他导航部分。 You can do it like so: 你可以这样做:

Notice the .center class in the navbar container 注意导航栏容器中的.center类

<div class="navbar navbar-fixed-top center">
     <div class="navbar-inner">
        ....
     </div>
</div>

And then you can target the .center class like so: 然后你可以像这样定位.center类:

.center.navbar .nav,
.center.navbar .nav > li {
    float:none;
    display:inline-block;
    *display:inline; /* ie7 fix */
    *zoom:1; /* hasLayout ie7 trigger */
    vertical-align: top;
}

.center .navbar-inner {
    text-align:center;
}

Demo: http://jsfiddle.net/C7LWm/show/ 演示: http//jsfiddle.net/C7LWm/show/

Edit : Forgot to realign the submenu items to the left, this is the fix: 编辑 :忘记重新调整左侧的子菜单项,这是修复:

CSS CSS

.center .dropdown-menu {
    text-align: left;
}

Demo: http://jsfiddle.net/C7LWm/1/show/ 演示: http//jsfiddle.net/C7LWm/1/show/

I know this is an old post but I have also noticed this post a few times in my googling. 我知道这是一个很老的帖子,但我也在google搜索过几次这篇帖子。 I want to actually set the record straight for centering the navbar in twitter bootstrap as the current accepted method is not wrong, but not needed as twitter bootstrap supports this. 我想实际设置记录直接将导航栏置于twitter bootstrap中,因为当前接受的方法没有错,但不需要,因为twitter bootstrap支持这一点。

There is actually a better way of doing this then modifying it manually. 实际上有一种更好的方法可以手动修改它。 This will cut down of code by using what is already in twitter bootstrap. 这将通过使用twitter bootstrap中已有的内容来减少代码。

<div class="navbar navbar-fixed-top navbar-inverse">
    <div class="navbar-inner">
        <div class="container">
            <a class="brand" href="#">RIZEN</a>
            <ul class="nav">
                <li class="active"><a href="/">Home</a></li>
                <li><a href="#">Events</a></li>
                <li><a href="#">Links</a></li>
                <li><a href="#">Gallery</a></li>
                <li><a href="#">Contact</a></li>
                <li><a href="#">Forums</a></li>
            </ul>
        </div>
    </div>
</div>

Breakdown 分解
First line is pretty simple as we are creating the navbar, setting it to a fixed position on the viewport to the top and then swapping the white to black by inverting the color theme. 第一行非常简单,因为我们正在创建导航栏,将其设置为视口上的固定位置到顶部,然后通过反转颜色主题将白色交换为黑色。

Next we are going to assign the navbar-inner which more or less says, if at all possible, set a minimum hight and this is where the actual color stylings come from, not the line above. 接下来我们将分配navbar-inner,或多或少说,如果可能的话,设置最小高度,这是实际颜色样式的来源,而不是上面的行。

Here is the IMPORTANT line as we are dropping the fluid layout for the navbar and going with just a container. 这是重要的一行,因为我们正在删除导航栏的流体布局并且仅使用容器。 This sets a fixed with with margins that center aligns the inner content to the screen. 这设置了一个固定的边距,中心将内部内容与屏幕对齐。 This is done through having auto margins, and setting a width. 这是通过自动边距和设置宽度来完成的。

This method will do what you want as you actually save kb in header requests by not adding extra code and using the scaffolding properly with twitter bootstrap and not having extra code bloat. 通过不添加额外的代码并使用twitter引导程序正确使用脚手架并且没有额外的代码膨胀,此方法将执行您想要的实际保存kb的头请求。 I have used this method in my own projects and continue to use it in my current project. 我在自己的项目中使用了这种方法,并继续在我当前的项目中使用它。 The hazard with modifying TBS(twitter bootstrap) is that you lose code consistency and if in the future you want to change things you have to make a mental note of what you have changed otherwise things may not work as intended. 修改TBS(Twitter引导程序)的危险在于您丢失了代码一致性,如果将来您想要更改内容,则必须记下您已更改的内容,否则事情可能无法按预期工作。

Hope this helps. 希望这可以帮助。 Also if you want a working example just look at the homepage of twitter bootstrap as it has just that. 此外,如果你想要一个工作的例子,只需查看twitter bootstrap的主页即可。

Ah and also, if you wish for spaces on both sides (before "Project Name" and after "Dropdown") to be symmetrical, add the following: 啊,而且,如果您希望两侧的空间(在“项目名称”之前和“下拉”之后)是对称的,请添加以下内容:

.navbar .nav, .navbar .nav > ul
{
    float: right
}

Also, it seems that using the following (as of Bootstrap v2.0.4) doesn't center the navbar still: 此外,似乎使用以下(从Bootstrap v2.0.4开始)不会使导航栏居中:

<div class="navbar navbar-fixed-top center">
      <div class="navbar-inner">
        <div class="container-fluid">
        .
        .
        .
        </div>
      </div>
</div>

You need to change to 你需要改为

<div class="navbar navbar-fixed-top center">
      <div class="navbar-inner">
        <div class="container">
        .
        .
        .
        </div>
      </div>
</div>

(Extracted from the sample provided by Andres llich; just that he missed out the change) (摘自Andres llich提供的样本;只是他错过了更改)

Now it works for me... 现在它对我有用......

The centering css from above works well on un-collapsed navbars only. 上面的定心css仅适用于未折叠的导航栏。

It's not ideal when the navbar is collapsed, though. 但是,当导航栏折叠时,它并不理想。 By default, each collapsed navbar item is on its own row, like a drop down menu. 默认情况下,每个折叠的导航栏项都在其自己的行上,如下拉菜单。 But the css above tries to shove all collapsed items onto the same row, which is less than ideal. 但是上面的css试图将所有折叠的项目推到同一行上,这不太理想。

I used this small fix of wrapping the centering css in a media query, so it gets triggered on non-collapsed navbars only: 我在媒体查询中使用了这个包含居中css的小修复程序,因此它仅在非折叠导航栏上触发:

@media (min-width: 768px) {
    .center.navbar .nav,
    .center.navbar .nav > li {
        float:none;
        display:inline-block;
        *display:inline; /* ie7 fix */
        *zoom:1; /* hasLayout ie7 trigger */
        vertical-align: top;
    }
    .center .navbar-inner {
        text-align:center;
    }
}

(tested with bootstrap 3.1.1) (使用bootstrap 3.1.1测试)

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

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