Could someone help me fix my navbar ? It is not expanding properly when clicking on the toggler, im sure i've misplaced a div but I have moved around elements with very little success...
<header class="site-header" role="banner">
<div class="navbar-wrapper">
<nav class="navbar navbar-light bg-light navbar-expand-lg ">
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarToggler" aria-controls="navbarToggler" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- end button -->
<a class="navbar-brand" src="index.html"><img class="logo-brand"src="img/kin.jpg" alt="header icon"></a>
<!-- collapsed section -->
<div class="navbar-collapse collapse" id="navbarToggler">
<!-- ml-auto for right and mr-auto for left -->
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active"><a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item"><a class="nav-link" href="#">Activities</a></li>
</ul>
</div>
</nav> <!--navbar -->
</div><!--navbar wrapper-->
</header>
I've compared it to a code that works and cannot find the difference... anyone with a good eye to fix me mistake ?
Ive copied a working collapsing navbar in my code and the same thing happens it the wrapper wont expand while the text comes out of its parent
Thanks
Take a look at following example:
Your code has no problem.
Be sure you provide right links for libraries. I added bootstrap css and js library links. And it needs Jquery before bootsrap js link.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<header class="site-header" role="banner">
<div class="navbar-wrapper">
<nav class="navbar navbar-light bg-light navbar-expand-lg ">
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarToggler" aria-controls="navbarToggler" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- end button -->
<a class="navbar-brand" src="index.html"><img class="logo-brand"src="img/kin.jpg" alt="header icon"></a>
<!-- collapsed section -->
<div class="navbar-collapse collapse" id="navbarToggler">
<!-- ml-auto for right and mr-auto for left -->
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">Activities</a></li>
</ul>
</div>
</nav> <!--navbar -->
</div><!--navbar wrapper-->
</header>
`
Test Home
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width", initial-scale="1.0" shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
`
Also, do not forget to add the initial stylesheet imports in the head of the HTML document as that stylesheet will play a key role in displaying the page properly. Specifying the width for the device width will allow the html document to adjust to the device width and UTF-8 specifies the character encoding to the HTML interpreter its something that all developers should get into the habit of including!
Related
I copied the navbar from the bootstrap navbar documentation, but when I click, the hamburger menu does not work for me, it just does not display the nav items for me.
my hamburger menu
My code in bs-navbar.component.html
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<img src="../../assets/images/logo.png" alt="" width="55" height="60"/>
<a class="navbar-brand fw-bold ms-4" href="#">medochek</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" href="#">Замовити</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Контакти</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Галерея</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Про мед</a>
</li>
</ul>
<span class="navbar-text">
+380 66-842-45-32
</span>
</div>
</div>
</nav>
I use Angular so I'm imported bootstrap like this: #import "~bootstrap/dist/css/bootstrap.css" in styles.css
You need the following imports in your framework's import style to be imported for the bootstrap navbar button to work and also for all the styles visible completely.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
The compatible versions of the above links you can check & replace for the bootstrap version you are using.
The second option is you can write your own javascript code to add/remove dynamic CSS active class from your navbar menu.
This question already has answers here:
How to align nav items to the right in Bootstrap 5?
(7 answers)
Closed 2 years ago.
I'm trying to setup a navbar where the links would be to the right when screen is large enough and the bar is not collapsed. However, despite having ml-auto included as the class in the unordered list (ul) tag, the links (Contact, Pricing, Download) are still stuck to left next to the Brand item. How do I fix this? The following is the code:
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<section id="title">
<nav class="navbar bg-dark navbar-expand-lg navbar-dark">
<a class="navbar-brand" href="#">brand</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="w-100">
<div class="navbar-collapse collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" 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>
Hi Gokulan,
Let's use the mr-auto instead on the <ul> tag to move the elements to the right. If is possible, please use the code I am providing you with on this post. Don't forget to run it in full page if you test this snippet first on stackOverflow to see the results. I certainly hope this helps, pal!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TinDog</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</head>
<body>
<section id="title">
<!-- Nav Bar -->
<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" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav mr-auto">
<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>
</body>
</html>
So no one else is having this problem in the course I'm taking.
As you can see I'm using
Chrome 74.0.3729.169 Desktop Windows
Firefox 67.0.1 Desktop Windows
html:
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/4.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="">DrinDrin</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToCollapse" aria-controls="navbarToCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarToCollapse">
<!-- ml-auto tells ul to move over,make space for left element -->
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="">Projects</anchor>
</li>
<li class="nav-item">
<a class="nav-link" href="">Foods</anchor>
</li>
<li class="nav-item">
<a class="nav-link" href="">Link</anchor>
</li>
</ul>
</div>
</nav>
<!-- <button class="btn btn-dark">THBUTTON</button>
<button class="btn btn-outline-dark">THBUTTON</button>
<button class="btn btn-large btn-outline-dark">THBUTTON</button>
<button class="btn btn-primary">THBUTTON</button> -->
</body>
</html>
Your closing tags need to match your opening tags. So
Words Here</anchor> needs to be <a href="">Words Here.
On an unrelated note, jQuery version isn't loading. Try https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js instead of https://ajax.googleapis.com/ajax/libs/jquery/4.3.1/jquery.min.js
I apologize in advice for being another bootstrap navbar question but I could not find a solution in the questions already answered on here.
Thank you for your help in advance.
Using bootstrap 4.1.3.
Problem: Navbar not collapsing on link click.
The following is a link which is inside of the <head> tag (placed before the style.css, if the matters):
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
And here are two scripts placed just before the </body> tag:
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
This is my navbar code:
<nav class="navbar navbar-expand-md navbar-light fixed-top">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#reviews">Reviews</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#contact">Contact</a>
</li>
</ul>
</div>
</nav>
EDIT:
I see what you're saying now:
Bootstrap has attributes to be applied to the tags in order for the functionality to work correctly on the navbar.
The attributes needed are the data-toggle and data-target attributes.
Change your 'a' tags to look like this:
<a class="nav-link" data-toggle="collapse" data-target=".navbar-collapse.show" href="#reviews">Reviews</a>
working jsfiddle: https://jsfiddle.net/e6cfL9m0/
This question has actually already been answered here:
How to hide collapsible Bootstrap 4 navbar on click
I am trying to have 3 elements on a navbar. A left aligned brand, a right aligned group of links that collapse on mobile, and an additional button that is also right aligned like the links, but should not collapse with them.
To right align the button and links I wrapped them in ml-auto div. This works great on mobile (1st image), but on PC the button appears above the links (2nd image). I tried to add btn-group to the div which sort of works, but the button merges with links and it looks odd (3rd image).
How can I properly align these components horizontally?
Please note that I do not want the button to be in the same group with the other right links. I want the button to be always visible even on a small screen. I've seen a few similar questions on SO, but they all have the right aligned items in the same, collapsible group which is no good for me.
You can paste the code below to w3school TryIt editor
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">Home</a>
<div class="ml-auto">
<button class="btn btn-success">My Button</button>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"/>
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 1</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 2</a>
</li>
</ul>
</div>
</nav>
None of the existing answers work and require complicated hacks that aren't correct use of the Bootstrap 4 Navbar. The Navbar in these answers doesn't display properly on mobile. The .row is only meant to contain grid columns, and the grid isn't supported Navbar content
The anwer is simple. The only adjustment you need is to disable the flex-grow (using flex-grow-0) that is used on the navbar-collapse. This allows the ml-auto to push the button to the right.
<nav class="navbar navbar-expand-sm bg-dark navbar-dark justify-content-end">
<a class="navbar-brand" href="#">Home</a>
<button class="btn btn-success ml-auto">My Button</button>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse flex-grow-0" id="navbarSupportedContent">
<ul class="navbar-nav text-right">
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 1</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 2</a>
</li>
</ul>
</div>
</nav>
Additionally add text-right to the navbar-nav if you want the links right on mobile.
Demo: https://www.codeply.com/go/ljI9F6aRLk
Also see: Bootstrap 4 - Navbar items outside the collapse
I modify your code. Try to view this one on a fullpage and resize accordingly:
I add d-sm-noneclass on the 1st button, I add justify-content-end class on your nav collapse then create another button <button class="btn btn-success d-none d-sm-block">My Button</button>to be part of your nav collapse and display none once it goes on other sizes except sm.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">Home</a>
<div class="ml-auto">
<button class="btn btn-success d-sm-none">My Button</button>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<button class="btn btn-success d-none d-sm-block">My Button</button>
<ul class="navbar-nav ml-auto ">
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 1</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 2</a>
</li>
</ul>
</div>
</nav>
<br>
<div class="container">
<h3>Navbar Forms</h3>
<p>Use the .form-inline class to align form elements side by side inside the navbar.</p>
</div>
</body>
</html>
I have found the solution finally. The trick is to use the grid system properly. Modify the col classes as per your requirement for responsiveness (in navbar collapse part) Also the button and collapse are not part of the same group just as you wanted, as wrapping it in the same div will cause the collapse to not work correctly on mobile screens. Hope it helps you..
body {
padding-top: 105px;
}
.collapse{
justify-content:flex-end;
}
.text-nowrap {
white-space: nowrap;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark fixed-top">
<div class="container">
<a href="index.html" class="navbar-brand">
<h3 class="d-inline align-middle text-white">
Brand
</h3>
</a>
<button id="myButton" class="btn btn-success ml-auto mr-2">
My button
</button>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbarNav" class="col-md-3 col-lg-2 collapse navbar-collapse ml-5">
<ul class="navbar-nav ">
<li class="nav-item">
<span class="text-nowrap">Right Link 1 </span>
</li>
<li class="nav-item">
<span class="text-nowrap">Right Link 2 </span>
</li>
</ul>
</div>
</div>
</nav>
<section>
<div class="text-center">
<p>hello this is teadsfadsf adsfadfs adfsasdfadf adsf adsf asdf adsf asdf adsf a
adfs ads
f
asd fadfs adfs
ads fds fdfs asdfadsxt</p>
</div>
</section>
</body>
</html>
Figured it out. Adding "row" to div makes them stack horizontally. Although, that caused some funny behavior when the collapsed button is pressed. It would shift towards to middle. Adding more left margin auto fixed that.
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">Home</a>
<div class="row ml-auto">
<button class="btn btn-success ml-auto">My Button</button>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto" />
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 1</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 2</a>
</li>
</ul>
</div>
</div>
</nav>