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.
I had the same problem. I solved it like this:
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.
my working code - bootstrap 3.0.3. when navbar-toggle, hidden-xs original logo image.
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:
Compute the height and (padding top + padding bottom) of the logo. Here 120px (100px height + padding top (10px) + padding bottom (10px))
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.
In this example I use this navbar. In navbar-brand:
add a class, for example myLogo, and an img (your logo)
<a class="navbar-brand myLogo" href="#"><img src="yourLogo.jpg" /></a>
.Add CSS
.myLogo { padding: 10px; }
Adequate to other sizes.
Example
You must use code as this:
Class of A tag must be "logo" not navbar-brand.
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 fitAnother option is to use a background image. Use an image of any size and then just set the desired width:
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.
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.
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.
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.So to complete this I put them both together and it seems to work perfectly in all browsers.
DEMO http://codepen.io/bootstrapped/pen/KwYGwq
Quick Fix : Create a class for your
logo
and set theheight
to28px
. This works well with the navbar on all devices. Notice I said works "WELL" .