gpt4 book ai didi

How can I put two navbar elements to the end in Bootstrap 5?(如何在Bootstrap 5中将两个导航栏元素放在末尾?)

转载 作者:bug小助手 更新时间:2023-10-24 21:47:57 24 4
gpt4 key购买 nike



I am trying to put two navbar elements to the end of the navbar in Bootstrap 5 but it's not working

我正在尝试将两个导航栏元素放到Bootstrap 5中导航栏的末尾,但它不起作用


I've tried ms-auto with a div encapsulated around the log in and sign up elements but that didn't work. I've also tried float and text but that didn't work either.

我试过在登录和注册元素周围封装一个div的ms-auto,但这不起作用。我也尝试过浮动和文本,但也不起作用。




<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<title>Hello, world!</title>
</head>

<body>
<nav class="navbar navbar-dark bg-dark navbar-expand-sm">
<a class="navbar-brand" href="">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarPractice" aria-controls="navbarPractice" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarPractice">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="">Home</a>
<a class="nav-link" href="">About</a>
<a class="nav-link" href="">Support</a>
<a class="nav-link" href="">Log In</a>
<a class="nav-link" href="">Sign Up</a>
</div>
</div>
</nav>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>




更多回答
优秀答案推荐

Use the w-100 or flex-fill class to stretch the navbar-nav element. Without that you have no space to implement ms-auto, which then works as expected. You'd use ms-sm-auto to only apply margin above the mobile breakpoint.

使用w-100或FLEX-FILL类来拉伸Navbar-NAV元素。否则,您就没有空间来实现MS-AUTO,然后它就会按预期工作。您可以使用ms-sm-auto来仅应用移动断点上方的边距。




<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<nav class="navbar navbar-dark bg-dark navbar-expand-sm">
<a class="navbar-brand" href="">Navbar</a>

<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarPractice" aria-controls="navbarPractice" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarPractice">
<div class="navbar-nav w-100">
<a class="nav-link active" aria-current="page" href="">Home</a>
<a class="nav-link" href="">About</a>
<a class="nav-link" href="">Support</a>
<a class="nav-link ms-sm-auto" href="">Log In</a>
<a class="nav-link" href="">Sign Up</a>
</div>
</div>
</nav>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>





edit: use bootstrap flex utilities like @isherwood suggested.

编辑:使用像@Isherwood建议的bootstrap flex实用程序。


Here is a bad practice option of setting the css yourself :

下面是一个自己设置css的糟糕实践选项:




@media (min-width: 576px) {
.navbar-nav {
width: 100%;
}
#navbarPractice>div a.login {
margin-left: auto;
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.1/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-dark bg-dark navbar-expand-sm">
<a class="navbar-brand" href="">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarPractice" aria-controls="navbarPractice" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarPractice">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="">Home</a>
<a class="nav-link" href="">About</a>
<a class="nav-link" href="">Support</a>
<a class="nav-link login" href="">Log In</a>
<a class="nav-link" href="">Sign Up</a>
</div>
</div>
</nav>
</div>
</div>
</nav>




更多回答

You rarely need custom CSS for things like this when using Bootstrap. Always make yourself familiar enough to avoid duplicating functionality this way.

在使用Bootstrap时,您很少需要为这样的事情定制CSS。总是让自己足够熟悉,以避免以这种方式重复功能。

whoops, sorry that was just the documentation that showed up first when I googled. sorry for the confusion. Yes, you're right, you should use what bootstrap offers and avoid customizing it yourself when possible.

哎呀,对不起,那只是我在谷歌上搜索时最先出现的文档。很抱歉给您带来了混乱。是的,你是对的,你应该使用Bootstrap提供的东西,尽可能避免自己定制它。

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