Bootstrap 导航栏在全屏时有效,但在压缩时无效
Bootstrap navbar does works when in full screen but not when condensed
目前,我在 bootstrap 中的导航栏在全屏模式下看起来不错。看起来 like this.
但是,bootstrap 低于特定宽度时会自动将项目压缩到堆栈中。那时候看起来很糟糕,因为注册和登录在同一行。看起来 like this.
我希望导航栏在右侧显示注册和登录路由,同时在压缩时将路由作为堆栈包含。
导航的当前 HTML。
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{{ url_for('home')}}">Test</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Pricing</a>
</li>
</ul>
{% if current_user.is_authenticated %}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('logout')}}" class="me-4">Logout</a>
</span>
{% else%}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('login')}}" class="me-4">Login</a>
</span>
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('register')}}" class="me-4">Register</a>
</span>
{% endif %}
</div>
</div>
</nav>
导航栏应仅在移动视图中显示为堆栈。您还可以考虑检查是否已正确添加 xs- 类 以获得所需的视图。我只能在您的代码段中看到 lg 类 。如果还是不行,请尝试分享codepen。
嗯,我不确定我是否理解这个问题,
但据我所知,我会将登录、注册和注销移动到 div 中,并在 CSS 中应用媒体查询来执行您的要求。
<style>
@media only screen and (max-width: 990px) {
.chnageMeWhenSmall {
position: absolute;
right: 60px;
}
}
</style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{{ url_for('home')}}">Test</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Pricing</a>
</li>
</ul>
<div class="chnageMeWhenSmall">
{% if current_user.is_authenticated %}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('logout')}}" class="me-4">Logout</a>
</span>
{% else%}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('login')}}" class="me-4">Login</a>
</span>
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('register')}}" class="me-4">Register</a>
</span>
{% endif %}
</div>
</div>
</div>
</nav>
目前,我在 bootstrap 中的导航栏在全屏模式下看起来不错。看起来 like this.
但是,bootstrap 低于特定宽度时会自动将项目压缩到堆栈中。那时候看起来很糟糕,因为注册和登录在同一行。看起来 like this.
我希望导航栏在右侧显示注册和登录路由,同时在压缩时将路由作为堆栈包含。
导航的当前 HTML。
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{{ url_for('home')}}">Test</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Pricing</a>
</li>
</ul>
{% if current_user.is_authenticated %}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('logout')}}" class="me-4">Logout</a>
</span>
{% else%}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('login')}}" class="me-4">Login</a>
</span>
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('register')}}" class="me-4">Register</a>
</span>
{% endif %}
</div>
</div>
</nav>
导航栏应仅在移动视图中显示为堆栈。您还可以考虑检查是否已正确添加 xs- 类 以获得所需的视图。我只能在您的代码段中看到 lg 类 。如果还是不行,请尝试分享codepen。
嗯,我不确定我是否理解这个问题, 但据我所知,我会将登录、注册和注销移动到 div 中,并在 CSS 中应用媒体查询来执行您的要求。
<style>
@media only screen and (max-width: 990px) {
.chnageMeWhenSmall {
position: absolute;
right: 60px;
}
}
</style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{{ url_for('home')}}">Test</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Pricing</a>
</li>
</ul>
<div class="chnageMeWhenSmall">
{% if current_user.is_authenticated %}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('logout')}}" class="me-4">Logout</a>
</span>
{% else%}
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('login')}}" class="me-4">Login</a>
</span>
<span class="navbar-text mb-2 mb-lg-0">
<a href="{{ url_for('register')}}" class="me-4">Register</a>
</span>
{% endif %}
</div>
</div>
</div>
</nav>