I have a div containing 3 dropdowns which sit under the navbar on a normal desktop window. Additionally, I have a couple of links which are right-justified in the navbar. On a page break, I'd like for the dropdown div to collapse into the the navbar along with the right-justified links.
Here's what I have so far:
<nav class="navbar navbar-default navbar-expand-lg fixed-top navbar-dark bg-dark" role="navigation" id="topnav">
<div class="container-fluid" id="nav_content">
<a class="navbar-brand" href="#">Brand</a>
<div class="navbar-header">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navredirect,#dropdowns" aria-controls="navredirect" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div id="navredirect" class="collapse navbar-collapse">
<ul class="navbar-nav ml-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="#">About</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid" id="content">
<div class="row mx-auto justify-content-center" id="dropdowns">
<!-- NOTE: dropdowns are auto-populated with a jQuery script -->
<label>Name:</label>
<select id="name"></select>
<label>State:</label>
<select id="state"></select>
<label>Item:</label>
<select id="item"></select>
</div>
</div>
Right now the redirect links in the navbar are being collapsed properly, but the dropdowns (which I assign as one of the data-targets in the collapse button) are not responding to the page break. Does Bootstrap let you move an external element into the navbar like this?
I'll figure out the eventual styling of the dropdown elements once I get them into the nav. For now I just need to get them up there and I can't figure out how for the life of me. Any ideas?
Related
I am using Angular 12 and Bootstrap 5 to create a SPA. I created a shared navbar component. When my navbar changes to the hamburger menu button, the navbar does collapse as intended. But when i try to close it, there is no reaction. Am i missing something?
<nav class="navbar fixed-top navbar-expand-md navbar-dark shadow">
<div class="container-fluid">
<a class="navbar-brand" routerLink="/someUrl">Foo</a>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarToggleContent"
aria-controls="navbarToggleContent"
aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarToggleContent">
<ul class="navbar-nav ms-auto">
<li class="nav-item p-2">
Item 1
</li>
<li class="nav-item p-2">
Item 2
</li>
<li class="nav-item p-2">
Item 3
</li>
</ul>
</div>
</div>
</nav>
This code is fine. I forget to add the popper.js entry to my angular.json file.
I have a code when i load an image first of all my navbar expands and take too much space on, how i can avoid such?
Secondly when i load an image as mentioned above my links to sections does not work properly as it does not take me to the top of section but bit lower because of size of navbar.
Any suggestions or help please?
<nav class="navbar navbar-expand-md navbar-light bg-dark fixed-top">
<div class="container">
<a href="#" class="navbar-brand">
<img src="/images/osamatraders.svg" width="60" alt="">
</a>
<button type="button" class="navbar-toggler bg-light " data-toggle="collapse" data-target="#nav"><span
class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse justify-content-center" id="nav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link text-light text-uppercase font-weight-bold px-3" href="#aboutus">About
Us</a></li>
How to display content above the bootstrap navigation bar. I tried the below code but I got the display content below the navigation bar.
index.html
<body>
<div class="container" id="topContent">Desktop only content above nav..</div>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">Start Bootstrap</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">
<a class="nav-link" href="#">Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
style.css
#media (max-width: 767px) {
.navbar{
margin-top:0;
top:-65px;
position:relative;
}
#topContent{
margin-top:45px;
}
Assuming you want a fixed Navbar, you would use min-width instead of max-width in the media query...
#media (min-width: 992px) {
.navbar{
margin-top:0;
top:45px;
}
}
Demo 1: https://www.codeply.com/go/l0MAcYsfO2
If you want to avoid the extra CSS, use one of the responsive utility classes. For example, use mt-lg-5 for a top margin that is only applied on desktop (lg and up).
<div class="container" id="topContent">Desktop only content above nav..</div>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top mt-lg-5">
<div class="container">
</div>
</nav>
Demo 2: https://www.codeply.com/go/voowMd6pom
Also see: Bootstrap 4 page wide header bar above navbar
Try to remove the class fixed-top in nav class=
HTML
<body>
<div class="container" id="topContent">Desktop only content above nav..</div>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">Start Bootstrap</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">
<a class="nav-link" href="#">Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
In CSS try to:
#media (max-width: 767px) {
.navbar{
margin-top: 20px // the size that you want to your div above the navbar
}
#topContent{
top: 0;
position: fixed;
}
Remember in this css you are setting the classes for max-width: 767px so test in this size of screen, let me know with that works :)
In this code,
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
fixed-top class is there. It will make the nav bar fixed at top only
I guess the best way to do this is not to tamper with the Actual bootstrap components as anyone would advise you who has been working with it in the last couple of years.
The easiest way to achieve what you want to do is creating two seperate content boxes inside the Navbar like this:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
HELLOO
</div>
<div class="container">
<a class="navbar-brand" href="#">Start Bootstrap</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>
</nav>
Using two containers inside the navbar will asure you that they will stay on top of each other. This might also tamper with the responsive design but you can single handedly erase the diffrent containers for defined resolutions where you don't want them to appear.
You should not try to use a lot of css here becuase it looks easy. Defining Stuff on the html part is way more efficient and can save you a lot of time later on :)
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>
I basically have a pretty simple question, but my portfolio will look better if I can make this simple addition.
I want my NavBar in bootstrap to be in 3 columns, essentially like the image I've mocked up below.
I currently have the base bootstrap navBar setup with the default code like so:
<!-- Navigation -->
<nav class="navbar navbar-expand-lg bg-white fixed-top">
<div class="container">
<a class="nav-link" href="#">my name goes here</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"><a class="nav-link" href="#">about</a></li>
<li class="nav-item"><a class="nav-link" href="#">resume</a></li>
</ul>
</div>
</div>
</nav>
I can make a weird version look right with messing with padding and stuff, but I dont want that. The hardest part for me is understanding the responsive version, and having the one link on the left (work) and two links on the right (about, resume) merge into one dropdown navigation and have the 'my name goes here' become the header above it on mobile.
Any help, or a point in the right direction would be great.
Also, it can be assumed my CSS is the base bootstrap CSS.
Thanks
UPDATE
<!-- Navigation -->
<nav class="navbar navbar-light navbar-expand-md bg-success justify-content-between">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-nav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse dual-nav w-50 order-1 order-md-0">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link pl-0" href="#">work <span class="sr-only">work</span></a>
</li>
</ul>
</div>
Colin Grant
<div class="navbar-collapse collapse dual-nav w-50 order-2">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="">about</a></li>
<li class="nav-item"><a class="nav-link" href="">resume</a></li>
</ul>
</div>
</div>
This seems to work perfectly, any tips for makign it better?
Thanks
Following is how I did it using bootstrap 4. When screen size is <992px right side of the navbar will collapse into a button.
<nav class="navbar navbar-expand-lg navbar-dark bg_color_primary sticky-top d-flex justify-content-between ">
<div class="navbar-nav col-4 align-items-start ">
<div class="text-white bg_color_primary_light rounded-circle p-2">
Work
</div>
</div>
<div class="col-4 d-flex justify-content-center">
<a class="navbar-brand text-white brand_name " href="">
BrandName
</a>
</div>
<button class=" navbar-toggler ml-auto align-items-end" 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>
<div class="collapse navbar-collapse col-4 " id="navbarNavDropdown">
<div class="navbar-nav ml-auto ">
<a class="nav-item nav-link" href="">About</a>
<a class="nav-item nav-link" href="">Resume</a>
</div>
</div>
</nav>