Can you make a fixed-top navbar scrollable from device with short height

Viewed 161

I am working in Bootstrap 4 and looking to see if it is possible to have a fixed-top navbar that is scrollable. I'm working on a mega menu and portions of it are not visible on a phone depending on what link you select and there is no way to scroll to see the bottom of the menu.

Using a sticky top nav works well but requires you to scroll to the very bottom of the page before the menu scrolls. Also, IE11 doesn't support position: sticky.

Here are some screenshots where scrolling doesn't allow you to see the full menu:

Expanded menu from the top of page

Expanded menu from the bottom of page

https://codepen.io/carboncopylabs/pen/BaQgjzY

1 Answers

It seems the Bootstrap team made the decision that you can have a fixed navbar with a short menu or a long menu, but not with a fixed navbar (based on this and this issue reports).

Fortunately, the discussion did include a solution, which kind of worked (the bottom of the menu when dropped down was right at the edge of the display) – I modified the code.

function setCss() {
    $('.navbar .navbar-collapse').css('max-height', 'calc(100vh - ' + $('.navbar').outerHeight() + 'px' + ')');
}

setCss();

$(window).resize(function() {
    setCss();
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"></script>

<style>
body {
    height: 3000px
}

.navbar .navbar-collapse {
    overflow-y: auto;
}

.container {
    margin-top: 100px
}
</style>

<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
    <a class="navbar-brand" href="#">Fixed navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav mr-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="#">Link 1</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 2</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 3</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 4</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 5</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 6</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 7</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 8</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 9</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link 10</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                    <a class="dropdown-item" href="#">Dropdown link 1</a>
                    <a class="dropdown-item" href="#">Dropdown link 2</a>
                    <a class="dropdown-item" href="#">Dropdown link 3</a>
                    <a class="dropdown-item" href="#">Dropdown link 3</a>
                    <a class="dropdown-item" href="#">Dropdown link 4</a>
                    <a class="dropdown-item" href="#">Dropdown link 5</a>
                </div>
            </li>
        </ul>

    </div>
</nav>

<main role="main" class="container">
    <div class="jumbotron">
        <h1>Navbar example</h1>
        <p class="lead">This example is a quick exercise to illustrate how fixed to top navbar works. As you scroll, it will remain fixed to the top of your browser's viewport.</p>
        <a class="btn btn-lg btn-primary" href="https://getbootstrap.com/docs/4.0/components/navbar/" role="button">View navbar docs &raquo;</a>
    </div>
</main>

Related