Center button vertically inside Nav boostrap

Asked

Viewed 975 times

1

As I center this button inside a Nav-item, the html is so:

<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

inserir a descrição da imagem aqui

1 answer

2


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

Thus:

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

See here the example working:

.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>
    <script src="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>
    <script src="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>

  • Thank you very much !

  • tmj my young man ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.