Center button vertically inside nav boostrap

1

As I center this button inside a nav-item, the html looks like this:

<nav class="navbar navbar-expand-sm bg-light navbar-light sticky-top">
  <div class="container">
    <a class="navbar-brand" href="#">
      <img src="imgs/logocompleto.png">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home
            <span class="sr-only">(current)</span>
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Services</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Contact</a>
        </li>
        <li class="nav-item">
          <button type="button" class="align-middle" data-toggle="modal" data-target="#myModal">
            Começe Agora
          </button>
        </li>
      </ul>
    </div>
  </div>
</nav>

I'm using bootstrap 4

    
asked by anonymous 26.02.2018 / 17:25

1 answer

1

You can only reference the Button that is inside the navbar and put a transform: translateY with that regardless of the height of the navbar the Button will always be centralized vertically

This way:

.nav-item button {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

See the working example here:

.nav-item button {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.bundle.min.js"></script>
<nav class="navbar navbar-expand-sm bg-light navbar-light sticky-top">
    <div class="container">
        <a class="navbar-brand" href="#">
        <img src="imgs/logocompleto.png">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
            <li class="nav-item active">
            <a class="nav-link" href="#">Home
                <span class="sr-only">(current)</span>
            </a>
            </li>
            <li class="nav-item">
            <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
            <a class="nav-link" href="#">Services</a>
            </li>
            <li class="nav-item">
            <a class="nav-link" href="#">Contact</a>
            </li>
            <li class="nav-item">
            <button type="button" class="align-middle" data-toggle="modal" data-target="#myModal">
                Começe Agora
            </button>
            </li>
        </ul>
        </div>
    </div>
</nav>
    
26.02.2018 / 18:28