具有徽标的Bootstrap 3导航栏

css CSS

小小村村

2020-03-20

我想将Bootstrap 3默认导航栏与图像徽标一起使用,而不要使用文字商标。在不引起任何不同屏幕尺寸问题的情况下,这样做的正确方法是什么?我认为这是一个普遍的要求,但是我还没有看到好的代码示例。除了在所有屏幕尺寸上都可以接受的显示外,关键要求是在较小屏幕上的菜单可折叠性。

我尝试仅将IMG标签放在具有navbar-brand类的A标签内,但这导致菜单在我的android手机上无法正常运行。我还尝试增加导航栏类的高度,但这使事情变得更糟。

顺便说一句,我的徽标图像大于导航栏的高度。

第2432篇《具有徽标的Bootstrap 3导航栏》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

13个回答
LEY小卤蛋 2020.03.20

Add the following to the .navbar-brand class

.navbar-brand
{
  padding: 0px; // this allows the image to occupy all the padding space of the navbar--brand
}

.navbar-brand > img
{
   height: 100%; // set height to occupy full height space on the navbar-brand
   width: auto; // width should be auto to allow img to scale accordingly
   max-height: 100%; // optional
   mrgin: 0 auto; // optional
}
前端古一 2020.03.20

The easiest and best answer for this question is to set a max width and height depending on your logo, the image will be a max of 50px high but no longer than 200px.

<a href="index.html">
<img src="imgs/brand-w.png" style="max-height:50px;max-width:200px;">
</a>

This will vary depending on your logo size and the nav bar items you have.

猪猪路易 2020.03.20

This code works perfect if you need to see the brand centered and with auto width in toolbar. It contains the Wordpress function to get the image file from right path:

<a class="navbar-brand page-scroll" rel="home" 
    href="#page-top"  title="Title">
    <img style="height: 100%; width: auto;" 
          src="<?php echo get_template_directory_uri();?>/img/logo.png">

Green猿 2020.03.20

You might try using @media query such as below.

Add a logo class first, then use @media to specify your logo height and width per device size. In the example below, I am targeting devices which have a max width of 320px (iPhone) You can play around with this until you find the best fit. Easy way to test: Use chrome or Firefox with inspect element. Shrink your browser as far as possible. Observe the logo size change based on the @media max width you specify. Test on iPhone, android devices, iPad etc to get the desired results.

.logo {
  height: 42px;
  width: 140px;
}

@media (max-width:320px) { 
.logo {
  height: 33px;
  width: 110px;
}
}
GO西门LEY 2020.03.20

Please try following code:

<style>
   .navbar a.navbar-brand {padding: 9px 15px 8px; }
</style>
<a class="navbar-brand" href="#">
   <img src="http://placehold.it/140x34/000000/ffffff/&amp;text=LOGO" alt="">
</a>
村村L 2020.03.20

Maybe this is too simple, but I just copied the navbar-brand line so there are two of them, the image and then the text.

<a class="navbar-brand" href="index.php"><img class="img-responsive" src="images/my-icon.png" width="30" height="25" alt=" "></a>
<a class="navbar-brand" href="index.php">My Brand</a>

And I created an image that fits inside the navbar (roughly 1/2 the height).

GO西门 2020.03.20

I also implement it via css background property. It works healty in any width value.

.navbar-brand{
    float:left;
    height:50px;
    padding:15px 15px;
    font-size:18px;
    line-height:20px;
    background-image: url("../images/logo.png");
    background-repeat: no-repeat;
    background-position: left center
}
老丝乐Mandy 2020.03.20

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;
}
Sam小哥 2020.03.20

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.

Gil前端 2020.03.20

I use the img-responsive class and then set a max-width on the a element. Like this:

<nav class="navbar">
    <div class="navbar-header">
        <a class="navbar-brand" href="#">
            <img class="img-responsive" src="mylogo.png">
        </a>
    </div>
</nav>

and the CSS:

.navbar-brand {
    max-width: 50%;
}
西里村村 2020.03.20

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

猿阳光 2020.03.20

My solution is add css "display: inline-block" to img tag.

<img style="display: inline-block; height: 30px; margin-top: -5px">

DEMO: jsfiddle

神奇古一 2020.03.20
<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="navbar-brand" rel="home" href="#" title="Buy Sell Rent Everyting">
        <img style="max-width:100px; margin-top: -7px;"
             src="/img/transparent-white-logo.png">
    </a>
</div>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android