htmlcssmaterialize

How to center align an ICON inside a BUTTON in materialize css navbar?


I read many post on SO about how to center align an ICON inside a BUTTON, but none of them work for my case, since in my case the BUTTON is inside a navbar.

Please see the code in jsfiddle, also pasted in the code block below.

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">


        <div class="navbar-fixed">
          <nav class="teal">
            <div class="container">
              <div class="nav-wrapper">
                <a href="#" class="brand-logo">LOGO </a>
                <ul class="right hide-on-med-and-down">
                  <li>
                    <form class="orange" style="">
                      <div style="display: inline-flex; border: 1px black solid; align-items: center;">
                        <input id="search-bar" type="search" placeholder="text text..." style="border: 1px red solid; margin: 0; height: 100%;">
                        <button class="btn" type="submit" style="background: blue; height:45px;">
                          <i class="material-icons" style="border: 1px red solid; display: inline; margin: auto;">search</i>
                        </button>
                      </div>
                    </form>
                  </li>
                  <li>
                    <a href="#home">Home</a>
                  </li>
                </ul>
              </div>
            </div>
          </nav>
        </div>

The issue is that, the search ICON is not vertically centered inside BUTTON, and I tried many suggested methods, none worked.

Please help!

enter image description here


Solution

  • You have just to add line-height: 45px to your icon , scince you added the height:45px; to your button , just like this :

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    
    
    <div class="navbar-fixed">
      <nav class="teal">
        <div class="container">
          <div class="nav-wrapper">
            <a href="#" class="brand-logo">LOGO </a>
            <ul class="right hide-on-med-and-down">
              <li>
                <form class="orange" style="">
                  <div style="display: inline-flex; border: 1px black solid; align-items: center;">
                    <input id="search-bar" type="search" placeholder="text text..." style="border: 1px red solid; margin: 0; height: 100%;">
                    <button class="btn" type="submit" style="background: blue; height:45px;">
                      <i class="material-icons" style="border: 1px red solid; display: inline; margin: auto;line-height:45px;">search</i>
                    </button>
                  </div>
                </form>
              </li>
              <li>
                <a href="#home">Home</a>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </div>