twitter-bootstrap Bootstrap 4 多个固定顶部导航栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42417018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:37:59  来源:igfitidea点击:

Bootstrap 4 Multiple fixed-top navbars

twitter-bootstrapbootstrap-4twitter-bootstrap-4

提问by Byron Daries

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

是否可以在 Bootstrap 4 中显示多个固定顶部导航栏?

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

1 目前覆盖了另一个,这不是我想要的。

采纳答案by Zim

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

是的,这是可能的,但您必须相应地定位第二个。导航栏的高度约为 56 像素。

.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

演示:Bootstrap 4 多个固定顶部导航栏

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

在某些情况下,最好使用单个fixed-topDIV 来包含两个.

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

另请参阅:
Bootstrap 4 Navbar sticky after header
Bootstrap 4 将两个导航栏折叠成一个切换按钮

回答by madav

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 topso that the padding will only show when it nears the top.

应使用第二个导航栏,top以便填充仅在接近顶部时显示。

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

padding-top当需要填充时,将在它到达顶部导航栏之前创建填充。

See here:

看这里:

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

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