csstwitter-bootstrapbootstrap-5bootstrap-icons

How to vertically align navbar link when used with icon that is larger than default?


Using Bootstrap 5 with Bootstrap Icons, I also enlarged the icon using font-size:1.5rem;, but that broke the vertical alignment for the links (If you remove the styling you can see it's working again, need to be in Full Page mode)

I could not find the correct way to vertically align it again:

<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.2/font/bootstrap-icons.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>


<!-- Navigation-->
<nav class="navbar navbar-expand-lg navbar-light fixed-top shadow-sm" id="mainNav">
    <div class="container">
        <a class="navbar-brand fw-bold" href="/">Navbar</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            Menu
            <i class="bi-list"></i>
        </button>

        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ms-auto me-4 my-3 my-lg-0 text-center d-flex align-items-center">
                <li class="nav-item"><a class="nav-link me-lg-3" href="#">Menu Item #1</a></li>
                <li class="nav-item"><a role="button" class="nav-link me-lg-3"><i class="bi bi-stack-overflow" style="font-size:1.5rem;"></i>Menu Item With Icon</a></li>
                <li class="nav-item"><a class="nav-link me-lg-3" href="#">Menu Item #3</a></li>
            </ul>
        </div>
    </div>
</nav>


Solution

  • Try adding the "d-flex" and "align-items-center" classes to that nav-link:

    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.2/font/bootstrap-icons.css" rel="stylesheet"/>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
    
    
    <!-- Navigation-->
    <nav class="navbar navbar-expand-lg navbar-light fixed-top shadow-sm" id="mainNav">
        <div class="container">
            <a class="navbar-brand fw-bold" href="/">Navbar</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                Menu
                <i class="bi-list"></i>
            </button>
    
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ms-auto me-4 my-3 my-lg-0 text-center d-flex align-items-center">
                    <li class="nav-item"><a class="nav-link me-lg-3" href="#">Menu Item #1</a></li>
                    <li class="nav-item"><a role="button" class="d-flex align-items-center nav-link me-lg-3"><i class="bi bi-stack-overflow" style="font-size:1.5rem;"></i>Menu Item With Icon</a></li>
                    <li class="nav-item"><a class="nav-link me-lg-3" href="#">Menu Item #3</a></li>
                </ul>
            </div>
        </div>
    </nav>