Bootstrap 4 Multiple fixed-top navbars
Asked Answered
U

3

35

Is it possible to have multiple fixed-top navbars in Bootstrap 4 showing under each other?

The 1 currently overlays the other which is not what I want.

Urias answered 23/2, 2017 at 13:23 Comment(1)
Removed tag for titleWithdrawal
C
28

Yes, it's possible but you have to position the 2nd one accordingly. The height of the Navbar is ~56px.

.fixed-top-2 {
    margin-top: 56px;
}

body {
    padding-top: 105px;
}

<nav class="navbar navbar-toggleable-sm bg-faded navbar-light fixed-top fixed-top-2">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbar1">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a href="/" class="navbar-brand">One</a>
    <div class="navbar-collapse collapse" id="navbar1">
        <ul class="navbar-nav">
            ..
        </ul>
    </div>
</nav>

<nav class="navbar navbar-toggleable-sm bg-inverse navbar-inverse fixed-top">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbar2">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a href="/" class="navbar-brand">Two</a>
    <div class="navbar-collapse collapse" id="navbar2">
        <ul class="navbar-nav">
            ..
        </ul>
    </div>
</nav>

Demo: Bootstrap 4 Multiple fixed-top Navbars

In some scenarios, it may be better to use a single fixed-top DIV to contain both.

Also see:
Bootstrap 4 Navbar sticky after header
Bootstrap 4 collapsing two navbars into one toggle button

Cottingham answered 23/2, 2017 at 14:31 Comment(2)
It loads for me. I posted the code in the question too.Cottingham
That "sticky after header" link you posted is exactly what I was looking for, but I wasn't able to verbalise it. Thanks a million!Morbid
H
10

Both navs can use sticky-top (instead of fixed-top) so you don't have to set the padding on the body.

The second navbar should use top so that the padding will only show when it nears the top.

padding-top will create padding before it hits the top navbar when the padding is necessary.

See here:

https://www.codeply.com/p/sIIXzlAW3V

Hyacinthus answered 3/2, 2020 at 13:10 Comment(2)
Thank you for a good example. But try to look at it in mobile phone mode and click button on the upper menu.Indistinct
@Indistinct Thanks for catching that! Will need to play with it to keep the li's on top.Hyacinthus
S
1

I came up with a pretty simple javascript solution for this. It supports infinite fixed headers with auto heights and resizing.

I use document.getElementsByClassName("fixed-top") to get a list of all fixed <nav> elements (They can be sorted by the data attribute data-header-index). Then I just iterate through each <nav> element adjusting the marginTop to be equivalent to the previous <nav> elements combined height. At the end I adjust paddingTop of the page container so that no <nav> elements overlap the content.

var fixeHeaderElms = document.getElementsByClassName("fixed-top");
function padHeaderElement() {

    // Sort our elements based on the data-header-index
    fixeHeaderElms = Array.from(fixeHeaderElms).sort((a, b) => { return parseInt(a.dataset.headerIndex) - parseInt(b.dataset.headerIndex) });

    var fixedHeaderHeights = 0;
    Array.from(fixeHeaderElms).forEach(
        function (element, index, array) {
            if (fixedHeaderHeights > 0) { element.style.marginTop = fixedHeaderHeights + "px"; }
            fixedHeaderHeights += element.offsetHeight;
        }
    );

    // Padd the page (or the body container) so the menu's don't overlap the body content.
    if (fixedHeaderHeights > 0) { document.getElementsByClassName("page")[0].style.paddingTop = fixedHeaderHeights + "px"; }
}

// Throw an observer on something controlling the size change of the menu.
if (fixeHeaderElms.length > 0) { new ResizeObserver(padHeaderElement).observe(fixeHeaderElms[0]);}

You can control the display index by setting the attribute data-header-index on the nav element.

<nav data-header-index="1" class="fixed-top . . .">
<nav data-header-index="2" class="fixed-top . . .">
<nav data-header-index="3" class="fixed-top . . .">

Note: <nav> element is not required. Just fixed-top class.

Staffan answered 11/7, 2022 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.