Bootstrap 3 Navbar with Logo

2019-01-02 18:57发布

I want to use the Bootstrap 3 default navbar with an image logo instead of text branding. What's the proper way of doing this without causing any issues with different screen sizes? I assume this a common requirement, but I haven't yet seen a good code sample. A key requirement other than having acceptable display on all screen sizes is the menu collapsibility on smaller screens.

I tried just putting an IMG tag inside the A tag that has the navbar-brand class, but that caused the menu not to function properly on my android phone. I also tried increasing the height of the navbar class, but that screwed things up even more.

By the way, my logo image is larger than the height of the navbar.

28条回答
浮光初槿花落
2楼-- · 2019-01-02 19:28

I had the same problem. I solved it like this:

<a href="#" class="btn btn-link navbar-btn">
  <img class="img-responsive" src="#">
</a>

There is no navbar-brand class. The result looks like logo picture that fits navbar and works like a link. Also I recommend to use navbar-right class for the menu items so they won't go below the logo.

<div class="collapse navbar-collapse navbar-right">
  <ul class="nav navbar-nav" role="navigation">
    <li><a href="#">Item1</a></li>
    <li><a href="#">Item2</a></li>
  </ul>
</div>
查看更多
骚的不知所云
3楼-- · 2019-01-02 19:30

my working code - bootstrap 3.0.3. when navbar-toggle, hidden-xs original logo image.

    <a class="navbar-brand hidden-xs" href="<?=$g4['path']?>/">
    <img src="<?=$g4[path]?>/images/logo_opencode.gif" align=absmiddle alt="brand logo">
    </a>

    <a class="navbar-brand navbar-toggle" href="<?=$g4['path']?>/" style="border:0;margin-bottom:0;">
    <img src="<?=$g4[path]?>/images/logo_opencode.gif" alt="brand logo" style="width:120px;">
    </a>
查看更多
泛滥B
4楼-- · 2019-01-02 19:31

Instruction how to create a bootstrap navbar with a logo which is larger than the default height (50px):

Example with a logo 100px x 100px, standard CSS:

  1. Compute the height and (padding top + padding bottom) of the logo. Here 120px (100px height + padding top (10px) + padding bottom (10px))

  2. Goto bootstrap / customize. Set instead of navbar height 50px > 120px (50 + 70) and navbar-collapse-max-height from 340px to 410px (340 + 70). Download css.

  3. In this example I use this navbar. In navbar-brand:

      <div class="navbar-header">
        ...
        </button>
        <a class="navbar-brand myLogo" href="#">
          <img src="yourLogo.jpg" />
        </a>
      </div>...
    

    add a class, for example myLogo, and an img (your logo)

    <a class="navbar-brand myLogo" href="#"><img src="yourLogo.jpg" /></a>.

  4. Add CSS

    .myLogo { padding: 10px; }

  5. Adequate to other sizes.

Example

查看更多
十年一品温如言
5楼-- · 2019-01-02 19:31

You must use code as this:

<div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" 
            data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <a class="logo" rel="home" href="#" title="Buy Sell Rent Everyting">
        <img style=""
             src="/img/transparent-white-logo.png">
    </a>
</div>

Class of A tag must be "logo" not navbar-brand.

查看更多
长期被迫恋爱
6楼-- · 2019-01-02 19:32

Bootstrap 3 navbar logo resizing

COMPLETE DEMO WITH MANY EXAMPLES

IMPORTANT UPDATE: 12/21/15

There is currently a bug in Mozilla I discovered that breaks the navbar on certain browser widths with MANY DEMOS ON THIS PAGE. Basically the mozilla bug is including the left and right padding on the navbar brand link as part of the image width. However, this can be fixed easily and I've tested this out and I fairly sure it's the most stable working example on this page. It will resize automatically and works on all browsers.

Just add this to your css and use navbar-brand the same way you would .img-responsive. Your logo will automatically fit

.navbar-brand {
  padding: 0px; /* firefox bug fix */
}
.navbar-brand>img {
  height: 100%;
  padding: 15px; /* firefox bug fix */
  width: auto;
}

Another option is to use a background image. Use an image of any size and then just set the desired width:

.navbar-brand {
  background: url(http://disputebills.com/site/uploads/2015/10/dispute.png) center / contain no-repeat;
  width: 200px;
}


ORIGINAL ANSWER BELOW (for reference only)

People seem to forget about object-fit a lot. Personally I think it's the easiest one to work with because the image automatically adjusts to the menu size. If you just use object fit on the image it will auto resize to the menus height.

    .navbar-brand > img {
      max-height: 100%;
      height: 100%;
      -o-object-fit: contain;
      object-fit: contain;
    }

It was pointed out that this does not work in IE "yet". There is a polyfill, but that might be excessive if you don't plan on using it for anything else. It does look like object-fit is being planned for a future release of IE so once that happens this will work in all browsers.

However, if you notice the .img-responsive class in bootstrap the max-width is assuming you are putting these images in columns or something that has an explicit with set. This would mean that 100% specifically means 100% width of the parent element.

    .img-responsive
        max-width: 100%;
        height: auto;
    }

The reason we can't use that with the navbar is because the parent (.navbar-brand) has a fixed height of 50px, but the width is not set.

If we take that logic and reverse it to be responsive based on the height we can have a responsive image that scales to the .navbar-brand height and by adding and auto set width it will adjust to proportion.

max-height: 100%;
width: auto;

Usually we would have to add display:block; to the scenario, but since navbar-brand already has float:left; applied to it, it automatically acts as a block element.


You may run into the rare situation where your logo is too small. The img-responsive approach does not take this into account, but we will. By also adding the "height" attribute to the .navbar-brand > img you can be assured that it will scale up as well as down.

max-height: 100%;
height: 100%;
width: auto;

So to complete this I put them both together and it seems to work perfectly in all browsers.

<style type="text/css">
.navbar-brand>img {
   max-height: 100%;
   height: 100%;
   width: auto;
   margin: 0 auto;


   /* probably not needed anymore, but doesn't hurt */
   -o-object-fit: contain;
   object-fit: contain; 

}
</style>

<nav class="navbar navbar-default">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="http://disputebills.com"><img src="http://disputebills.com/site/uploads/2015/10/dispute.png" alt="Dispute Bills"></a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div>
      </div>
</nav>

DEMO http://codepen.io/bootstrapped/pen/KwYGwq

查看更多
长期被迫恋爱
7楼-- · 2019-01-02 19:35

Quick Fix : Create a class for your logo and set the height to 28px. This works well with the navbar on all devices. Notice I said works "WELL" .

.logo {
  display:block;
  height:28px;
}
查看更多
登录 后发表回答