Twitter Bootstrap - centering brand logo in navbar

2019-04-01 15:06发布

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;

}

3条回答
太酷不给撩
2楼-- · 2019-04-01 15:26
.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

查看更多
相关推荐>>
3楼-- · 2019-04-01 15:27

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;
    }

查看更多
迷人小祖宗
4楼-- · 2019-04-01 15:42

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.

查看更多
登录 后发表回答