gpt4 book ai didi

css - Bootstrap 将导航栏元素右对齐

转载 作者:数据小太阳 更新时间:2023-10-29 09:04:57 29 4
gpt4 key购买 nike

如何将导航栏元素右对齐?

我想在右侧进行登录和注册。但我尝试的一切似乎都不起作用。

Picture of Navbar

这是我到目前为止尝试过的:

  • <div> <ul>周围具有属性:style="float: right"
  • <div> <ul>周围具有属性:style="text-align: right"
  • <li> 上尝试了这两件事标签
  • !important 再次尝试了所有这些事情添加到最后
  • 已更改 nav-itemnav-right<li>
  • 添加了 pull-sm-right类到 <li>
  • 添加了 align-content-end类到 <li>

这是我的代码:

<div id="app" class="container">
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<ul class="navbar-nav">
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricingg</a>
</li>
</ul>
<ul class="navbar-nav " >
<li class="nav-item">
<a class="nav-link" href="{{ url('/login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url('/register') }}">Register</a>
</li>
</ul>
</nav>
@yield('content')
</div>

最佳答案

Bootstrap 5(2021 年更新)

在 Bootstrap 5 ( see this question ) 中,ml-auto 已替换为 ms-auto 以表示 start 而不是 。由于导航栏仍然基于 flexboxauto margins OR flexbox utility classes仍用于对齐导航栏内容。

例如,使用me-auto...

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Menu </a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>

Bootstrap 5 right align Navbar content


Bootstrap 4(原始答案)

Bootstrap 有 many different ways to align navbar items . float-right 将不起作用,因为导航栏现在是 flexbox

您可以使用 mr-auto1st(左)navbar-nav 上自动设置右边距。或者ml-auto 可以用在2nd(右)navbar-nav 上,或者如果你只是有一个navbar-nav

<nav class="navbar navbar-expand-md navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Register</a>
</li>
</ul>
</div>
</nav>

https://codeply.com/go/P0G393rzfm

还有 flexbox 实用程序。例如,在折叠菜单上使用 justify-content-end:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Download</a>
</li>
</ul>
</div>
</div>
</nav>

或者当你有 2 个 navbar-nav 时,在 navbar-collapse 中使用 justify-content-between 可以使空间均匀在导航栏导航之间:

 <div class="navbar-collapse collapse justify-content-between">
<ul class="navbar-nav mr-auto">
..
</ul>
<ul class="navbar-nav">
..
</ul>
</div>

Bootstrap 4.0 及更新版本的更新

从 Bootstrap 4 beta 开始,ml-auto 仍然可以将元素推到右边。请注意 navbar-toggleable- 类已更改为 navbar-expand-*

已更新 navbar right for Bootstrap 4


另一个常见的 Bootstrap 4 导航栏右对齐场景包括右侧的按钮,该按钮保留在移动折叠导航之外,因此它始终以所有宽度显示。

Right align button that is always visible

enter image description here

enter image description here

相关:Bootstrap NavBar with left, center or right aligned items

关于css - Bootstrap 将导航栏元素右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41513463/

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