I try to use bootstrap navbar and write below code :
<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="#navbarGeneral" aria-controls="navbarGeneral" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarGeneral">
<ul class="nav navbar-nav">
<li class="nav-item ">
<a href="http://localhost:8080/" class="nav-link">Home
</a>
</li>
<li class="nav-item active">
<a href="http://localhost:8080/bootstrap5" class="nav-link">bootstrap5
</a>
</li>
<li class="nav-item">
<a href="http://localhost:8080/home4" class="nav-link">home4
</a>
</li>
<li class="nav-item dropdown">
<a href="http://localhost:8080/LoginPage" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">LoginPage
</a>
<ul class="dropdown-menu">
<li class="nav-item ">
<a href="http://localhost:8080/LoginPage/Bootstrap-34" class="dropdown-item">Bootstrap 3.4
</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
when clicking on the login page get an error that
Uncaught DOMException: Failed to execute 'querySelector' on
'Document': 'http://localhost:8080/LoginPage' is not a valid selector.
What is problem?
Jsfiddle
It's not working to toggle the dropdown because the anchor tag is attempting to navigate. Remove the url from the href...
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle"
data-bs-toggle="dropdown"
role="button" aria-expanded="false">LoginPage</a>
<ul class="dropdown-menu">
<li class="nav-item ">
Bootstrap
</li>
</ul>
</li>
Note to future readers: Most Bootstrap 5 "dropdown not working" questions are caused by failing to use the new data-bs- attributes
Use this and Let me know if it works
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/js/bootstrap.bundle.min.js?cdv=40" type="text/javascript"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css?cdv=40" type="text/css" rel="stylesheet"/>
</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="#navbarGeneral" aria-controls="navbarGeneral" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarGeneral">
<ul class="nav navbar-nav">
<li class="nav-item ">
<a href="http://localhost:8080/" class="nav-link">Home
</a>
</li>
<li class="nav-item active">
<a href="http://localhost:8080/bootstrap5" class="nav-link">bootstrap5
</a>
</li>
<li class="nav-item">
<a href="http://localhost:8080/home4" class="nav-link">home4
</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">
LoginPage
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</body>
</html>
Related
I copied everything from Bootstrap, but my navbar stays collapsed on mobile and tablet. When i click the icon, there is a movement, like they try to go up and down.
I attached the code below :
<nav class="navbar navbar-expand-lg navbar-dark ">
<div class="container-fluid ">
<!-- <a class="navbar-brand" href="#">Navbar</a> -->
<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 d-flex justify-content-center" id="navbarNav">
<ul class="navbar-nav my-5 fs-5">
<li class="nav-item me-5">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item me-5">
<a class="nav-link" href="#">Tour List</a>
</li>
<li class="nav-item me-5">
<a class="nav-link" href="#">Tour Search</a>
</li>
<!-- LOGO -->
<img src="/images/Landing-logo.png" width="125px" alt="">
<!-- LOGO -->
<li class="nav-item ms-5">
<a class="nav-link" href="#">Desinations</a>
</li>
<li class="nav-item ms-5">
<a class="nav-link" href="#">Pages</a>
</li>
<li class="nav-item ms-5">
<a class="nav-link" href="#">Blog</a>
</li>
</ul>
</div>
</div>
</nav>
this is code snippet for responsive navbar you can adjust it accordingly.
<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="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</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">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>```
I followed the w3schools documentation page for bootstrap navbars, which said to add justify-content-center to my navbar to center links. In addition, I followed a YouTube video which said to do the same thing, with the caveat of adding that Bootstrap class to the div class which has <div class="collapse navbar-collapse" however, when I add it to my code, it does not center any of the items. I only want to center the nav links and not the brand logo.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<header class="header">
<nav class="navbar navbar-colour navbar-expand-lg navbar-dark_disabled">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">BRAND</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 justify-content-center" 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="#">Home</a>
</li>
<li class=" nav-item">
<a class="navolour nav-link" href="#">About</a>
</li>
<li class=" nav-item dropdown">
<a class="navolour nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Drop Down
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Nav Item 1</a></li>
<li><a class="dropdown-item" href="#">Nav Item 2</a></li>
<li><a class="dropdown-item" href="#">Nav Item 3</a></li>
</ul>
</li>
<li class=" nav-item">
<a class="navolour nav-link" href="search.php">Nav Link</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<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>
jsfiddle: https://jsfiddle.net/eagucmh0/
The bootstrap documentation indicates the justify-content-center should be on the <ul> element - <ul class="nav justify-content-center">
https://getbootstrap.com/docs/5.1/components/navs-tabs/
Update
Try this...
<!-- ======= Header ======= -->
<header class="header">
<nav class="navbar navbar-colour navbar-expand-lg navbar-dark ">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">BRAND</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 mb-2 mb-lg-0 justify-content-center collapse navbar-collapse" id="navbarSupportedContent">
<li class=" nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class=" nav-item">
<a class="navolour nav-link" href="#">About</a>
</li>
<li class=" nav-item dropdown">
<a class="navolour nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
Drop Down
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Nav Item 1</a></li>
<li><a class="dropdown-item" href="#">Nav Item 2</a></li>
<li><a class="dropdown-item" href="#">Nav Item 3</a></li>
</ul>
</li>
<li class=" nav-item">
<a class="navolour nav-link" href="search.php">Nav Link</a>
</li>
</ul>
<!-- </div> -->
</div>
</nav>
</header><!-- End Header -->
Commented out the inner <div> and moved its classes and the id to the <ul> element.
jsfiddle: https://jsfiddle.net/0vm29bah/
You need to take the me-auto class off the list. That pushes it over even if you use centering classes on the parent element.
See the full page demo.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<header class="header">
<nav class="navbar navbar-colour navbar-expand-lg navbar-dark_disabled">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">BRAND</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 justify-content-center" id="navbarSupportedContent">
<ul class="navbar-nav 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="navolour nav-link" href="#">About</a>
</li>
<li class=" nav-item dropdown">
<a class="navolour nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Drop Down
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Nav Item 1</a></li>
<li><a class="dropdown-item" href="#">Nav Item 2</a></li>
<li><a class="dropdown-item" href="#">Nav Item 3</a></li>
</ul>
</li>
<li class=" nav-item">
<a class="navolour nav-link" href="search.php">Nav Link</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<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>
I am a CSS beginner and I want to center Bootstrap navbar horizontally on the page (that is, the navbar itself extends over the width of the page, but the elements in it should be center-aligned with respect to the navbar). How can I do this?
My HTML code is as below:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Test page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous">
<link rel="stylesheet" href="styles.css">
</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="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</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">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-
disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-
label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
</body>
</html>
Whereas in my CSS file styles.css I try to center the navbar (but it remains fixed in the initial position):
.navbar {
text-align: center;
}
How can I solve the problem? Thanks in advance.
To center your navbar content, as justify-content-lg-center to your container to use flex to center everything, and add flex-lg-grow-0 to your navbar-collapse div so that it doesn’t grow (normally, that div grows to take up the available space, pushing the search form to the right).
Some of your attributes had a space in them like data-bs- target which won’t work correctly. Running your code through the W3 Validator one in a while is a good way to be sure there aren’t any typos.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid justify-content-lg-center">
<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 flex-lg-grow-0 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="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</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">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
I used Stackoverflow's snippet tool as it's a handy way to see the code in operation.
I want to center the logo and have 2 navbar items from its left and the other 2 from the right. It will increase my website functionality and help with the more modern look. If someone could help me I would be really grateful, thanks a lot!!!
tried searching here i didnt find anything that was working for me.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.html">
<img src="img\binet-logo1.png" height="70"width="100" alt=""></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<li class="nav-item">
<a class="nav-link" href="index.html">Дома</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Продукти
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="produkti.html">Ултразвучни системи</a>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="zanas.html">За Нас</a>
</li>
</li>
<li class="nav-item">
<a class="nav-link" href="C:\Users\Bojan\Desktop\BinetMedical\contact.html">Контакт</a>
</li>
</ul>
</div>
</div>
You can try using this code to get the logo in the middle and the nav links on the sides.
<nav class="navbar navbar-expand-md navbar-fixed-top navbar-dark bg-dark">
<div class="container">
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="index.html">Дома</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Продукти
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="produkti.html">Ултразвучни системи</a>
</a>
</li>
</ul>
<ul class="nav navbar-nav mx-auto">
<li class="nav-item">
<a class="nav-link" href="#"> <img src="img\binet-logo1.png" height="70"width="100" alt=""></a>
</li>
</ul>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="zanas.html">За Нас</a>
</li>
<li class="nav-item">
<a class="nav-link" href="C:\Users\Bojan\Desktop\BinetMedical\contact.html">Контакт</a>
</li>
</ul>
</div>
</nav>
Each part is a seperate ul and i added the class mx-auto
This question already has answers here:
Bootstrap align navbar items to the right
(24 answers)
Closed 5 years ago.
Im trying to float navbar items to the right instead of the left
here is the html i have used floats float-right class and i think pull-right is deprecated so anyone that could help would be appreciated
<header class="container-fluid" id="nav">
<nav class="navbar navbar-expand-lg ">
<a class="navbar-brand" href="#">Navbar</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 mr-auto" id="navbarSupportedContent">
<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="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
</nav>
</header>
Create another <ul class="navbar-nav ml-auto"> for the navbar items you want on the right.
ml-auto will pull your navbar-nav to the right where mr-auto will pull it to the left.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
<style>
/* Stackoverflow preview fix, please ignore */
.navbar-nav {
flex-direction: row;
}
.nav-link {
padding-right: .5rem !important;
padding-left: .5rem !important;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary rounded">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link">Left Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link">Left Link 2</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link">Right Link 1</a>
</li>
<li class="navbar-item">
<a class="nav-link">Right Link 2</a>
</li>
</ul>
</nav>
</body>
Add class "navbar-right" in <ul class="navbar-nav ">