gpt4 book ai didi

html - Bootstrap 4 - 如何防止折叠菜单折叠导航中的所有元素

转载 作者:行者123 更新时间:2023-12-04 10:43:52 25 4
gpt4 key购买 nike

我正在使用 bootstrap 4 导航栏,我想防止 Logo 和语言切换在单击汉堡菜单时随菜单一起移动和折叠。这是我所有的代码。另外,这是我的代码笔的链接。这可能吗?

https://codepen.io/asizemore/pen/LYEgvOy?editors=1100

CSS

.logo {
max-width: 130px;
}

.btn {
font-family: 'Muli', sans-serif;
font-weight: 900;
}

.btn {
background: rgb(142, 63, 41);
color: rgb(202, 171, 159);
}

.active {
background: rgb(166, 73, 47);
color: white;
}

HTML

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>

!-- NAV BAR -->
<nav class="navbar navbar-light fixed-top navbar-expand-lg bg-white">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<a class="navbar-brand mt-lg-0" href="#"><img src="https://via.placeholder.com/200x50" class="logo img-fluid"></a>

<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mx-auto mt-2 mt-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">PURPOSE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">FAMILY</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">AGRICULTURE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">ART</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">CONTACT</a>
</li>

</ul>

</div>

<!-- LANGUAGE SWITCH -->

<div class="btn-group btn-toggle lang mr-1">
<input type="button" name="lang" class="btn" value="ENG">

<input type="button" name="lang" class="btn active" value="SPA">
</div>
</nav>

最佳答案

将您的语言切换放在 .navbar-item 中,紧跟在 .navbar-brand 之后,并给它一个 order: 1(大于 0 的默认值 - 因此它将在 .navbar 未折叠的时间间隔内显示为其父项的最后一项)。由于您使用的是 .navbar-expand-lg,因此它将是 @media (min-width: 992px) 但如果您使用 .navbar-expand -md 它应该是 min-width: 768px - 或者 576px for -sm。更多关于 responsive breakpoints here .

当导航折叠时,您不需要改变顺序,所以语言切换器显示在菜单之前(这就是我们首先将它放在 Logo 之后的原因)。看到它工作:

.logo {
max-width: 130px;
}

.btn {
font-family: 'Muli', sans-serif;
font-weight: 900;
}

.btn {
background: rgb(142, 63, 41);
color: rgb(202, 171, 159);
}

.active {
background: rgb(166, 73, 47);
color: white;
}

@media(min-width: 992px) {
.language-switch {
order: 1;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

<nav class="navbar navbar-light fixed-top navbar-expand-lg bg-white">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mt-lg-0" href="#"><img src="https://via.placeholder.com/200x50" class="logo img-fluid"></a>
<div class="nav-item language-switch">
<div class="btn-group btn-toggle lang mr-1">
<input type="button" name="lang" class="btn" value="ENG">
<input type="button" name="lang" class="btn active" value="SPA">
</div>
</div>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mx-auto mt-2 mt-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">PURPOSE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">FAMILY</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">AGRICULTURE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">ART</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">CONTACT</a>
</li>
</ul>
</div>
</nav>

哦,当它们实际上不适合一行(设备窄于 ~450px).但这基本上是您提出的问题。

注意:如果您希望在折叠导航栏时首先显示 Logo ,您可以简单地将其放在切换器之前,并为切换器提供 ml-auto mr-3ml -3 mr-auto 使其粘在右侧或左侧(恕我直言,让它居中有点奇怪)。示例:

.logo {
max-width: 130px;
}

.btn {
font-family: 'Muli', sans-serif;
font-weight: 900;
}

.btn {
background: rgb(142, 63, 41);
color: rgb(202, 171, 159);
}

.active {
background: rgb(166, 73, 47);
color: white;
}

@media(min-width: 992px) {
.language-switch {
order: 1;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

!-- NAV BAR -->
<nav class="navbar navbar-light fixed-top navbar-expand-lg bg-white">
<a class="navbar-brand mt-lg-0" href="#"><img src="https://via.placeholder.com/200x50" class="logo img-fluid"></a>
<button class="navbar-toggler mr-auto" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="nav-item language-switch">
<div class="btn-group btn-toggle lang mr-1">
<input type="button" name="lang" class="btn" value="ENG">
<input type="button" name="lang" class="btn active" value="SPA">
</div>
</div>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mx-auto mt-2 mt-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">PURPOSE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">FAMILY</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">AGRICULTURE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">ART</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">CONTACT</a>
</li>
</ul>
</div>
</nav>

在适当的响应间隔(即低于 991px 在你的情况下)。

关于html - Bootstrap 4 - 如何防止折叠菜单折叠导航中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59807203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com