Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 : Vertically Align Navigation Links when Logo Increases the Navbar Height

I'm new to the Bootstrap 4 framework, but even after reading all the documentation and all the existing B4 questions on StackOverFlow, I'm still a bit stuck.

In my navigation bar, I inserted a SVG logo that has a height and width of 50px. This automatically makes the navbar taller. I have not adjusted any CSS for this, it is simply the logo that is forcing the navbar height to increased.

Here is my HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
    <title>@yield('title')</title>
    <link rel="import" href="elements.html">
    <link rel="stylesheet" href="/styles/app.css">
</head>
<body>
    <header class="navbar navbar-light navbar-fixed-top bd-faded">
        <div class="clearfix hidden-sm-up">
            <button class="navbar-toggler pull-right" data-toggle="collapse" data-target="#navbar">
                    &#9776;
                </button>
            <a href="/" class="navbar-brand">
                <img src="/images/brand.svg" alt="Logo">
                <span>Bootstrap</span>
            </a>
        </div>
        <div class="collapse navbar-toggleable-xs" id="navbar">
            <a href="/" class="navbar-brand">
                <img src="/images/brand.svg" alt="Logo">
                <span>Bootstrap</span>
            </a>
            <nav class="nav navbar-nav">
                <div class="nav-item nav-link"><a href="#">Link 1</a></div>
                <div class="nav-item nav-link"><a href="#">Link 2</a></div>
                <div class="nav-item nav-link"><a href="#">Link 3</a></div>
            </nav>
        </div>
    </header>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="/scripts/vendor.js"></script>
    <script src="/scripts/bootstrap.js"></script>
    <script src="/scripts/app.js"></script>
    <script src="/components/webcomponentsjs/webcomponents.js"></script>
</body>
</html>

Here is my Custom CSS:

.navbar-brand {
    font-size: 22px;
    img {
        display: inline-block;
        margin-right: 8px;
        width: 50px;
        height: 50px;
    }
}

I'd like the navigation links which is link1, link2, & link3 to be vertically align to the title which is Bootstrap.

Thanks You in Advance

like image 780
Paper9oll Avatar asked Feb 16 '16 13:02

Paper9oll


People also ask

How do I align the navigation bar vertically?

Add text-align:center to <li> or <a> to center the links. Add the border property to <ul> add a border around the navbar. If you also want borders inside the navbar, add a border-bottom to all <li> elements, except for the last one: Home.

How do I align navbar links to the right in Bootstrap 4?

ml-auto class in Bootstrap can be used to align navbar items to the right. The . ml-auto class automatically aligns elements to the right.

How do I vertically align an image in Bootstrap 4?

One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.


2 Answers

I ran into a similar situation where I'd like to vertically align my nav-items.

Step 1: Add the classes d-lg-flex align-items-center to your .navbar-nav parent element. This ensures that you can align child items using flex-utilities

Step 2: Add the h-100 class to each child nav-item. Nav-item has no defined height that I'm aware of. With nav-item's defined height, it will follow the flex rules set above by align-items-center.

Step 3: Please check my external links in my codepen because the navbar requires additional js.

Here is a working example of vertically centered nav-items https://codepen.io/b3bold/pen/rvMwzN

<nav class="navbar navbar-expand-md p-3 mb-2 navbar-light bg-white text-dark">   
  <a class="navbar-brand" href="#">
    <img src="https://i.stack.imgur.com/snRuy.jpg" />
  </a>

  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav ml-auto text-center w-100 justify-content-between ml-lg-0 d-lg-flex align-items-center">
      <div class="nav-item h-100"><a class="nav-link" href="#">RandomLink</a></div>
      <div class="nav-item h-100"><a class="nav-link"href="#">Some Really Long Link as an Example of Vertical </a></div>
      <div class="nav-item h-100"><a class="nav-link" href="#">RandomLink</a></div>
      <div class="nav-item h-100"><a class="nav-link" href="#">RandomLink</a></div>
      <div class="nav-item h-100"><a class="nav-link" href="#">RandomLink</a></div>
    </ul>
  </div>
</nav>
like image 193
Alexander Romero Avatar answered Oct 16 '22 14:10

Alexander Romero


I think you can remove top and bottom padding from links and add height and line-height to it. Also remove padding from .navbar-brand.

Just add this CSS:

.navbar-brand {
  padding: 0;
}
.navbar-nav .nav-link {
  padding-top: 0;
  padding-bottom: 0;
  height: 50px;
  line-height: 50px;
}

CODEPEN

like image 13
max Avatar answered Oct 16 '22 15:10

max