Is anybody can show me how to add space between each and every button in this Navigation Bar.
I want to separate them to look like This one I'm using bootstrap 5.
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm bg-body">
<div class="container">
<a class="navbar-brand" href="">
<h3>Brand</h3>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="btn btn-primary shadow-sm" aria-current="page"
href="{% url 'About' %}">About</a></li>
<li class="nav-item"><a class="btn btn-primary shadow-sm"
href="{% url 'login' %}">Log in</a></li>
<li class="nav-item"><a class="btn btn-primary shadow-sm"
href="{% url 'register' %}">Sign Up</a></li>
</ul>
</div>
</div>
To space elements we add margin between them. We'll do it only when navbar is horizontal so to not affect mobile menu. (We also add some bottom margin to space them for mobile)
#media (min-width: 992px) {
.navbar-nav li {
margin-left: 5px;
}
}
.navbar-nav li {
margin-bottom: 5px;
}
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm bg-body">
<div class="container">
<a class="navbar-brand" href="">
<h3>Brand</h3>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="btn btn-primary shadow-sm" aria-current="page" href="{% url 'About' %}">About</a></li>
<li class="nav-item"><a class="btn btn-primary shadow-sm" href="{% url 'login' %}">Log in</a></li>
<li class="nav-item"><a class="btn btn-primary shadow-sm" href="{% url 'register' %}">Sign Up</a></li>
</ul>
</div>
</div>
Related
I want to change this bootstrap5 navbar buttons from left side to a right side, but I don't know how to do that using css or bootstrap5, guys how can I do this?
My Code:
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.3.0-alpha1/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">
<h1>Pondadb</h1>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">About</a>
</li>
<li class="nav-item">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">Login</a>
</li>
<li class="nav-item">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">Singn-Up</a>
</li>
</ul>
</div>
</div>
</nav>
You have .mr-auto which is Bootstrap 4's version of margin-right: auto;. This does not work in B5 and the correct class would be .me-auto.
However, you don't want margin-right: auto;, you want margin-left: auto; to push the elements to the right. In B5, you should use ms-auto.
Spacing · Bootstrap v5.3
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">
<h1>Pondadb</h1>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto">
<li class="nav-item active">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">About</a>
</li>
<li class="nav-item">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">Login</a>
</li>
<li class="nav-item">
<a class="btn btn-primary mx-lg-2 mx-0 my-lg-0 my-2" href="#">Singn-Up</a>
</li>
</ul>
</div>
</div>
</nav>
I think changing of "mr-auto" in into "ml-auto" should help. It is bootstrap form of margin-right: auto, so it means that it is positioned at right. If I undertand you right, you want it to be on right side, so change it to margin-left: auto = ml-auto.
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 10 months ago.
I'm pulling my hair out trying to center my nav items and have my button at the end. I managed to get the button to the end but cannot figure out how to center the items+brand. Any help would be appreciated!
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>DOC</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">The team</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Contact Us</a>
</li>
</ul>
<div class="d-flex">
<button class="btn btn-outline-success" type="submit">Login</button>
</div>
</div>
</div>
</nav>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
Well you can't do such thing inside of a single flex (it might be possible using margin auto but I'm not sure about it) but using two nested flex boxes can help you
here in this code I used the div.collapse as first flexbox and ul.navbar-nav as the second one
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse justify-content-between navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav w-100 justify-content-center mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="#">The team</a>
</li>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="#">Contact Us</a>
</li>
</ul>
<div class="d-flex">
<button class="btn btn-outline-success" type="submit">Login</button>
</div>
</div>
</div>
Here is a live demonstration of how it works, let me know if you meant something else
https://jsfiddle.net/mahdiar_mansouri/fohgr7ew/7/
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="">DELIVER</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">PANDA</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
{{ request.user.username }}
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">個人檔案</a></li>
<li><a class="dropdown-item" href="#">我的訂單</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">登出</a></li>
</ul>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search items" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
For the above code, I would like to put the navbar items on the right. However I had already tried me-auto and mr-auto but all the items are still put on the left. Even though I followed the documentation of Bootstrap it still didn't work. Can anyone tell me where is the problem?
To align navbar (place it on right), just add ".justify-content-end" to ".collapse" block, see Code snipper comment.
To check it in Code Snippet example you might need to open it in full screen.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/jquery#3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="">DELIVER</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- add "justify-content-end" here -->
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<!-- remove unnecessary "me-auto" -->
<ul class="navbar-nav mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">PANDA</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">{{ request.user.username }}</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">個人檔案</a></li>
<li><a class="dropdown-item" href="#">我的訂單</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">登出</a></li>
</ul>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search items" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
This is happen because the navbar-collapse are given display flex. And flexing default properties are pulling the items left and right.
To pull the navbar-items to the right just remove the "me-auto" class from unordered-list. and add the flex properties class "justify-content-end" with the "navbar-collapse" class.
Currently, my navbar in bootstrap looks fine when in full screen. It looks like this.
However, bootstrap automatically condense the items in to a stack when below a certain width. That is when it looks bad as the Register and Login are on the same line. Looks like this.
I want the navbar to have the Register and Login routes on the right while containing the routes as a stack when condensed.
Current HTML for the nav.
<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">
Logout
</span>
{% else%}
<span class="navbar-text mb-2 mb-lg-0">
Login
</span>
<span class="navbar-text mb-2 mb-lg-0">
Register
</span>
{% endif %}
</div>
</div>
</nav>
Well, I am not sure if I understand the question pretty well,
but from what I understand I would move the login, register, and logoff into a div and apply a media query in the CSS to do what you ask for.
<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">
Logout
</span>
{% else%}
<span class="navbar-text mb-2 mb-lg-0">
Login
</span>
<span class="navbar-text mb-2 mb-lg-0">
Register
</span>
{% endif %}
</div>
</div>
</div>
</nav>
Navbar should appear as a stack only in mobile view. You can also consider checking if you have added xs- classes properly to get the desired view. I can only see lg classes in your snippet. Pelase try to share the codepen if it still doesn't work.
As stated in the title, due to some internal issue of data-bs-toggle in a newer version of bootstrap, it only collapses and does not hide it afterward, and after reading a similar post about this issue https://github.com/twbs/bootstrap/issues/32643 where they addressed it, they didn't include any alternative in meantime, and as I am a beginner in web development, I am unsure what would be the best approach to do the same feature?
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<nav class="navbar fixed-top navbar-expand-lg navbar-dark p-md-3">
<div class="container">
<a class="navbar-brand logoText" href="#">Power<span style="color:#0ac1ce;" class="logoText">In</span>Button</a>
<!--Only collapses and does not hide (problem of V5 engine data-bs-toggle)-->
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<div class="mx-auto"></div>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-white" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="/blog">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="/cv">CV</a>
</li>
<li class="nav-item me-4">
<a class="nav-link text-white" href="/contact">Contact</a>
</li>
I don't see any problem. Please check if you included both css and js links properly and I see you are using text-white which makes the text like invisible if you are not having a dark theme or any contrast background by default.
<link
href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.5.0/font/bootstrap-icons.css"
/>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj"
crossorigin="anonymous"
></script>
<div class="container">
<a class="navbar-brand logoText" href="#"
>Power<span style="color: #0ac1ce;" class="logoText">In</span>Button</a
>
<!--Only collapses and does not hide (problem of V5 engine data-bs-toggle)-->
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<i class="navbar-toggler-icon">menu</i>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-black" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-black" href="/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link text-black" href="/blog">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link text-black" href="/cv">CV</a>
</li>
<li class="nav-item me-4">
<a class="nav-link text-black" href="/contact">Contact</a>
</li>
</ul>
</div>
</div>
Here your issue is with the Text-color being white There is a class in bootstrap bg-dark Giving that to nav will change the background color so you can see the text ...
And the issue u mentioned is for modal collapse, not navbar
Here is the updated code
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark p-md-3">
<div class="container">
<a class="navbar-brand logoText" href="#">Power<span style="color:#0ac1ce;" class="logoText">In</span>Button</a>
<!--Only collapses and does not hide (problem of V5 engine data-bs-toggle)-->
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<div class="mx-auto"></div>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-white" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="/blog">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="/cv">CV</a>
</li>
<li class="nav-item me-4">
<a class="nav-link text-white" href="/contact">Contact</a>
</li>
I have this issue with bootstrap 5 and vue3, and made a function onclick, that looks like this:
toggleMenu() {
try {
let el = document.getElementById('navbarCollapse');
if (el) {
el.classList.toggle('show');
}
} catch (error) {
console.error(error);
}
},
which is simply called from the button click.
I removed the data-bs-... properties from the button, naturally.
<button class="navbar-toggler" #click="toggleMenu()" type="button" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
The idea is to just toggle the 'show' class on the collapsible part of the menu.