Materialize framework: responsive logo in navbar

Filip Perz picture Filip Perz · Oct 30, 2015 · Viewed 36.2k times · Source

I'm creating a website in materialize framework. I have a problem with logo in navbar. So my logo is very big (6202x3800 px) and I don't have any other version. I want to put this logo in navbar but it always exceeds the frame of nav-wrapper. I tried to add class responsive-img but it didn't work at all.

Here is my code:

#allcontent {
  background-color: white;
  margin: 2%;
}
body {
  background: #d26335;
}
nav {
  color: black;
}
<div class="row" id="allcontent">
  <div class="navbar-default" id="navbar">
    <nav class="white">
      <div class="nav-wrapper">
        <a href="index.html" class="brand-logo left"><img class="responsive-img" id="logo" src="<!-- my image -->"/></a>
        <ul id="nav-mobile" class="right hide-on-med-and-down">
          <li><a href="sass.html"><i class="material-icons">search</i></a>
          </li>
          <li>colors</li>
        </ul>
      </div>

    </nav>
  </div>
</div>

Answer

vanburen picture vanburen · Oct 31, 2015

All you have to do is control the size of the image with CSS to whatever dimensions you want:

.navbar-material .nav-wrapper .brand-logo img {
    height: 64px;
}

Or use a image editor to resize the image to something more reasonable for a logo to start with. See Photoshop, GIMP or Sketch to name a few.

(*And definitely compress images; if you're not using a build system to do this there's always something like TINYpng)

See working example Snippet.

(**note all classes used are default MaterialCSS minus the .navbar-material class and it looks like you're mixing Bootstrap3 classes with MaterialCSS classes)

$(".button-collapse").sideNav();
body {
  background: #d26335;
}
nav.navbar-material {
  background: #d26335;
  border: none;
  box-shadow: none;
}
.navbar-material .nav-wrapper {
  background: white;
  margin: 20px;
}
.navbar-material .nav-wrapper > ul > li > a {
  color: black;
}
.navbar-material .nav-wrapper .brand-logo img {
  height: 64px;
}
.navbar-material .nav-wrapper .button-collapse i {
  color: black;
}
@media (max-width: 600px) {
  .navbar-material .nav-wrapper .brand-logo img {
    height: 56px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/js/materialize.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/css/materialize.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<nav class="navbar-material">
  <div class="nav-wrapper">
    <a href="#!" class="brand-logo">
      <img src="http://placehold.it/6202x3800/FF001E/fff">
    </a>
    <a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>

    <ul class="right hide-on-med-and-down">
      <li><a href="sass.html">Sass</a>

      </li>
      <li><a href="badges.html">Components</a>

      </li>
      <li><a href="collapsible.html">Javascript</a>

      </li>
      <li><a href="mobile.html">Mobile</a>

      </li>
    </ul>
    <ul class="side-nav" id="mobile-demo">
      <li><a href="sass.html">Sass</a>

      </li>
      <li><a href="badges.html">Components</a>

      </li>
      <li><a href="collapsible.html">Javascript</a>

      </li>
      <li><a href="mobile.html">Mobile</a>

      </li>
    </ul>
  </div>
</nav>