Twitter Bootstrap - centering brand logo in navbar

无人久伴 提交于 2019-12-05 03:10:28

问题


I tryed centering the .brand logo with:

.brand { margin-left: auto; margin-right: auto; }

this is my top navbar layout:

http://jsfiddle.net/J4zkJ/3/

but it seems it doesn't works.

I searched on stack before to post this answer and the code i used above, is taken from an answered question.

Any idea how to center .brand ?

NB: i can't use position:absolute and fixed, and i would like to do this responsively (responsive)

also i tryed this:

.brand { 
  margin:0 auto !important;
  padding:0 auto !important;
  position:absolute !important;
  right:0px;
  left:0px;
   z-index:9999999999 !important;

}

回答1:


.navbar .brand {
  margin-left: auto;
  margin-right: auto;
  width: 60px;
  float: none;
}

I gave it an arbitrary width, set it to whatever you'd like.

jsFiddle




回答2:


using bootstrap 3

I added three classes to my global.less file to augment the bootstrap navbar.less classes.

This approach works through all the responsive @media rule sizes, but you will need to adjust font-size and menu-collapse points depending on the width of your menu items (this approach positions the navbar-brand absolutely - it "floats" over the other menu items).

style:

.navbar-brand-centered {
    position: absolute;
    top: 0;
    left: 50%;
    height: 50px;
    width: 200px;  /* bootstrap has max-width: 200px rule */
    margin-left: -100px;
    margin-top: 8px;
    text-align: center;
  }

  .navbar-brand-centered {
    color: @navbar-inverse-brand-color;
    &:hover,
    &:focus {
      color: @navbar-inverse-brand-hover-color;
      background-color: @navbar-inverse-brand-hover-bg;
    }
  }

  .navbar-brand-centered {
    display: block;
    max-width: 200px;
    margin-left: auto;
    margin-right: auto;
    padding: @navbar-padding-vertical @navbar-padding-horizontal;
    font-size: @font-size-large;
    font-weight: 500;
    line-height: @line-height-computed;
    color: @navbar-brand-color;
    text-align: center;
    &:hover,
    &:focus {
      color: @navbar-brand-hover-color;
      text-decoration: none;
      background-color: @navbar-brand-hover-bg;
    }
  }

usage:

<div class="navbar navbar-fixed-top">
    <!-- .navbar-toggle is used as the toggle for collapsed navbar content -->
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>

    <!-- Be sure to leave the brand out here if you want it shown -->
    <a class="navbar-brand-centered" href="/"><span style="font-size: 2em">mess<span style="color: red;">2</span>dress</span></a>

    <!-- Place everything within .navbar-collapse to hide it until above 768px -->
    <div class="nav-collapse collapse navbar-responsive-collapse">
        <!-- YOUR MENU ITEMS HERE -->

This worked for me, but I'm sure there's a better way to integrate with the float-based approach that bootstrap implements. Open to feedback.




回答3:


I just used those in my style.css and it worked for me:


    /* The header of the navbar */
    .navbar-header {
        text-align: center;
        margin: 10px;
        float: none !important;
    }

    /* The website name */
    .navbar-brand {
        float: none !important;
    }



来源:https://stackoverflow.com/questions/14486261/twitter-bootstrap-centering-brand-logo-in-navbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!