Jonathan Solorzano
Jonathan Solorzano

Reputation: 7032

Fit image to bootstrap navbar height responsively

I would like to know how to make an image fit bootstrap navbar height (proportionally), here's a descriptive image:

navbar

Any idea?

I'm using this structure:

<nav class="navbar navbar-default">
  <divclass="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">
        <img alt="Brand" src="...">
      </a>
    </div>
  </div>
</nav> 

Upvotes: 3

Views: 20843

Answers (2)

vanburen
vanburen

Reputation: 21663

If you're simply trying to adjust the size of an image to correspond to the height of a default navbar (min-height: 50px), simply use the below CSS as a base depending on how you want the image to fit.

In this example, the image will cover it's part of the navbar completely.

See working Snippet.

body {
  padding-top: 70px;
}
.navbar.navbar-inverse {
  border: none;
}
.navbar .navbar-brand {
  padding-top: 0px;
}
.navbar .navbar-brand img {
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <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="#">
        <img src="http://placehold.it/350x150/f00/fff">
      </a>

    </div>
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a>

        </li>
        <li><a href="#">Link </a>

        </li>
        <li><a href="#">Link </a>

        </li>
        <li><a href="#">Link </a>

        </li>
        <li><a href="#">Link </a>

        </li>
      </ul>
    </div>
  </div>
</nav>
<div class="container text-center">
  <img src="http://placehold.it/350x150/f00/fff" />
  <h4>The Same Image At Full Scale</h4>

</div>

Upvotes: 4

Andrew Adam
Andrew Adam

Reputation: 1582

in a previous project of mine I also encountered some issues with this. Here is how I solved the problem:

HTML:

<nav id="myNavbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <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="index.html" class="smoothScroll"><img src="img/logo.png" ondragstart="return false;" alt="logo"/></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav" >
                <li>
                    <a>Menu Item1</a>
                </li>
            </ul>
        </div>            <!-- /.navbar-collapse -->
    </div>        <!-- /.container -->
</nav>

And the CSS for it:

.navbar-fixed-top {height: 40px;}
.navbar-brand img {height: 40px;}

So setting the navbar and the logo-image to the same height (and not giving it width, thus it will be automatically adjusted) solved the problem for me with good positioning. Two possible cases:

  1. you want a fixed navbar:

    .navbar-brand img {position:fixed;right:0;top:0}

  2. you want a header on top:

    .navbar-brand img {position:absolute;right:0;top:0}

In both cases I set it to top-right corner of the site, but you can adjust that easily. So the key is: same height navbar and image, with the image positioned well. (Then margin-padding or exact position of the image can easily be set in CSS)

Hope it helped, Andrew

EDIT: Regarding fixed navbar please check the Bootstrap documentation (it has built-in class like navbar-fixed-top and such).

Upvotes: 0

Related Questions