I am trying to add a separator in the collapsed nav when viewed on mobile. Ideally the nav would look exactly the same as it does not when not collapsed, but when collapsed would have a separator between "About" and "Log In".
Here is the code for the nav:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Company</a>
</div>
<div class="collapse navbar-collapse" id="main-navbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li>About</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Log In</li>
<li>Sign Up</li>
</ul>
</div>
</div>
</nav>
JSFiddle
I have tried adding <li role="separator" class="divider"></li> where I want the separator, but it does not show up:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Company</a>
</div>
<div class="collapse navbar-collapse" id="main-navbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li>About</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li role="separator" class="divider"></li>
<li>Log In</li>
<li>Sign Up</li>
</ul>
</div>
</div>
</nav>
JSFiddle
How can I add a separator for the collapsed nav?
This separator would hopefully look very similar to that of a dropdown in a nav:
In Bootstrap v4.0.0-alpha.6, you can use
<div class="dropdown-divider"></div>
jsFiddle
It does not show up as It is defined as follows in Bootstrap css:
.dropdown-menu .divider {
height: 1px;
margin: 9px 0;
overflow: hidden;
background-color: #e5e5e5;
}
And you do not have .divider under .dropdown-menu
What you should do ?
#media (max-width:767px){
.divider {
height: 1px;
margin: 9px 0;
overflow: hidden;
background-color: #e5e5e5;
}
}
You have to put this code:
<li role="separator" class="divider"></li>
between your own "< li >" tags.
Example:
<ul class="dropdown-menu" aria-labelledby="drop2">
<li> Your own Li Tags</li>
<li> Your own Li Tags</li>
<li> Your own Li Tags</li>
<li role="separator" class="divider"></li>
<li> Your own Li Tags</li>
<li> Your own Li Tags</li>
</ul>
Boostrap 4.1 Solution
Was trying to accomplish this in a BS4.1 Navbar component and ended up here (via Google). Since .divider does not exist in this version, I used <hr> to achieve the desired result...
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="https://www.sociallinkgenerator.com/#generator">Social Links</a></li>
<li class="nav-item"><a class="nav-link" href="https://www.sociallinkgenerator.com/facebook-link-generator/#opengraph-generator">Open Graph</a></li>
<li class="nav-item"><a class="nav-link" href="https://www.sociallinkgenerator.com/twitter-link-generator/#twitter-card-generator">Twitter Cards</a></li>
<li class="nav-item"><a class="nav-link" href="/json-ld-generator#json-ld-generator">JSON-LD</a></li>
<li class="nav-item"><hr class="border-top"></li>
<li class="nav-item"><a class="nav-link" href="https://www.sociallinkgenerator.com/#donation">Feeling Generous?</a></li>
<li class="nav-item"><a class="nav-link" href="https://www.sociallinkgenerator.com/#contact">Need a Developer?</a></li>
</ul>
Bonus: This <hr> did not affect expanded display since its margins place it behind the next .nav-item element :)
Note: .border-top was added to the <hr> for lighter color.
you may insert an
<hr>
Tag instead of your divider like this:
<ul class="nav navbar-nav navbar-right">
<hr>
<li>Log In</li>
<li>Sign Up</li>
</ul>
Please tell me if it is as you wanted.
If you don't want to touch css there is little hack for it :D.
Instead of
<li role="separator" class="divider"></li>
use this
<form class="navbar-form navbar-left" style="padding:0px; border-top-width:0px;"></form>
Bootstrap gives forms 1px borders on top and bottom when collapsed. You can use this and insert empty form which is invisible while expanded but with one border while collapsed.
Related
I'm working on a project using CodePen and I'm using bootstrap to make a navbar. But for some reason it's displaying vertically instead of horizontally. Even when I copy and paste code from the W3Schools examples and the Bootstrap website. I don't know what else to do. Please help.
This is the W3Schools example code:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
</div>
This is example code from the Bootstrap website:
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link <span class="sr-only">(current)</span></li>
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
<li role="separator" class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
Both do not work. And yes, I've triple checked my <link> and <script> tags.
Edit: here is my CodePen
First like in the comments you are using bootstrap 3 markup and you are linking to both bootstrap 3 and 4 remove the bootstrap 4 css. Then you need to have the div with the class of collapse navbar-collapse so just add this to the div that has the id of nav just like the following:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#about">
<span style="font-family: 'Source Code Pro', Monospace; color:green"><i class="fa fa-code"></i>Ryan</span></a>
</div>
<div class="collapse navbar-collapse" id="nav">
<ul class="nav navbar-nav">
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
You may want to add the collapse button to the navbar header as well otherwise your links wont show up at mobile widths so it would look like:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#about">
<span style="font-family: 'Source Code Pro', Monospace; color:green"><i class="fa fa-code"></i>Ryan</span>
</a>
</div>
<div class="collapse navbar-collapse" id="nav">
<ul class="nav navbar-nav">
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
As someone said - remove bootstrap 4 from loaded CSS files and it will work
I got this NavBar and no matter what I do to it, it looks really horrible for responsiveness. I can't even get the logo to respond when I shrink my window size down
html
<header>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="index.html">
<img class="img-responsive" src="app/images/Wildwood_Home_Logo.png" />
</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-left" style="background-color:white !important">
<li>WHAT WE MAKE</li>
<li>WHO WE ARE</li>
<li>WHAT THEY SAID</li>
<li>WHAT WE WON</li>
<li>WHAT YOU'LL SEE</li>
<li>WHERE TO BUY</li>
<li>SHOP ONLINE</li>
</ul>
</div>
</div>
</nav>
CSS
.navbar-default {
background-color:#83A4C4;
}
.navbar-brand {
height: 100%;
padding: 0;
}
.navbar-default .navbar-nav>li>a {
color: #FFFFFF;
}
.navbar-toggle {
background-color: white;
}
.navbar-toggle .icon-bar {
background-color: #83A4C4 !important;
}
#media (max-width: 767px) and (min-width: 1310px)
{
.navbar-default .navbar-nav .open .dropdown-menu>li>a
{
color:white;
}
}
the media query I was messing around with, I just had it set to max-width of 767px
go to the bootstrap online documentation and take the code exactly as it is then modify, this is the code of navbar, delete the parts you don't want in your page
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link <span class="sr-only">(current)</span></li>
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
<li role="separator" class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
I've been learning to use bootstrap so I decided to use it to create a Navbar. So, I decided to try and centre it so I looked online for a solution but none of them work. So please could you advise me on what to do.
HTML:
</head>
<body>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li class="dropdown">Items <span class="sr-only"></span>
<ul class="dropdown-menu">
<li>Item 1</li>
<li role="separator" class="divider"></li>
<li>Item 2 </li>
<li role="separator" class="divider"></li>
<li>Item 3</li>
</ul></li>
</ul>
</div>
Current CSS:
.navbar .navbar-nav {
display: inline-block;
float: none;
vertical-align: top;
}
.navbar .navbar-collapse {
text-align: center;
}
It looks like the CSS you have and code you have will work fine. Just wrap your div in a nav element.
<nav class="navbar navbar-default" role="navigation">
<div class="collapse navbar-collapse">
... Rest of Your Code ...
</div>
</nav>
try use this CSS code to solve your problem:
.navbar-collapse .navbar-nav{
margin: 0 auto;
width: 300px; /*(example - [width: auto; is wrong])*/
}
.navbar-collapse{
margin: 0 auto;
}
.navbar-collapse li{
margin: 0 auto;
}
If you are using bootstrap, why are you using your current CSS for navbar? Just use what bootstrap is offering.
I am not sure whether you mean to center the text in the navbar or center the navbar all together, but this is found on bootstrap's website
<body>
<div class="container">
<!-- Static navbar -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>About</li>
<li>Contact</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li>Separated link</li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="active">Default <span class="sr-only">(current)</span></li>
<li>Static top</li>
<li>Fixed top</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
</nav>
So, I'm brand new to all of this. This is my first ever actual project with HTML/CSS, so please keep that in mind. Constructive criticism is always welcome, as it will only help me become better.
Now that that's out of the way, my issue:
So, I'm using Bootstrap because it's easy to use and from my experience so far is somewhat user friendly. I can't for the lift of me however figure out how to move my dropdown button to the right-hand side of my navbar.
Here's the code for my navbar (again, sorry if it's trash. New to this):
<div id="navbar" class="navbar navbar-default navbar-custom navbar-fixed-top">
<a class="navbar-brand" href="">AVC Gaming</a>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
Forums <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Ban Appeal</li>
<li>Registration</li>
</ul>
</li>
<li class="button">Donate</li>
<li class="dropdown">
UK 111 <span class="caret"></span>
<ul class="dropdown-menu">
<li>Forums</li>
<li>Arma 2</li>
<li>Arma 3</li>
</ul>
</li>
</ul>
</div>
</div>
If you want, there is a live version here: http://www.avc-gaming.us/avc
I've tried adding "pull-right" to the UK 111 dropdown <ul> but that didn't do anything. When I added "pull-right" to the Donate button, all it did was swap places with the UK 111 dropdown.
Any ideas?
The pull-right works like it should. But because the width of ul.nav navbar-nav is just taking up the space of it's content it's not working like you want. If you'll put a width of for example 90% on ul.nav navbar-nav you will see it aligns right.
You can just add bootstrap's own class pull-right. I have a example here!
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid pull-right">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>Home</li>
</ul>
</div>
</div>
You have to move <li> of UK 111 to separate <ul> and add 'pull-right' class to it along with other classes that you are using for styling your list.
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
Forums <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Ban Appeal</li>
<li>Registration</li>
</ul>
</li>
<li class="button">Donate</li>
</ul>
<ul class="nav navbar-nav pull-right">
<li class="dropdown">
UK 111 <span class="caret"></span>
<ul class="dropdown-menu">
<li>Forums</li>
<li>Arma 2</li>
<li>Arma 3</li>
</ul>
</li>
</ul>
</div>
If you want to move All of them to the right of the page put Float:right in line <ul class="nav navbar-nav"> like so:
<div id="navbar" class="navbar navbar-default navbar-custom navbar-fixed-top">
<a class="navbar-brand" href="">AVC Gaming</a>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav" style="float:right">
<li class="dropdown">
Forums <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Ban Appeal</li>
<li>Registration</li>
</ul>
</li>
<li class="button">Donate</li>
<li class="dropdown">
UK 111 <span class="caret"></span>
<ul class="dropdown-menu">
<li>Forums</li>
<li>Arma 2</li>
<li>Arma 3</li>
</ul>
</li>
</ul>
</div>
</div>
OR
If you want to move just the last one then set Width:96% on line <ul class="nav navbar-nav"> and float:Right on your last dropdownlist <li class="dropdown">
<div id="navbar" class="navbar navbar-default navbar-custom navbar-fixed-top">
<a class="navbar-brand" href="">AVC Gaming</a>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav" style="Width:96%">
<li class="dropdown">
Forums <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Ban Appeal</li>
<li>Registration</li>
</ul>
</li>
<li class="button">Donate</li>
<li class="dropdown" style="Float:right">
UK 111 <span class="caret"></span>
<ul class="dropdown-menu">
<li>Forums</li>
<li>Arma 2</li>
<li>Arma 3</li>
</ul>
</li>
</ul>
</div>
</div>
Bootstrap 3 provides this functionality. Please refer to the Docs for a complete example and specifically for right aligned links: Component Alignment
About Pull-right and Navbars
Align nav links, forms, buttons, or text, using the .navbar-left or
.navbar-right utility classes. Both classes will add a CSS float in
the specified direction. For example, to align nav links, put them in
a separate <ul> with the respective utility class applied.
These classes are mixin-ed versions of .pull-left and .pull-right, but
they're scoped to media queries for easier handling of navbar
components across device sizes.
Component Alignment
Right aligning multiple components Navbars currently have a limitation
with multiple .navbar-right classes. To properly space content, we use
negative margin on the last .navbar-right element. When there are
multiple elements using that class, these margins don't work as
intended.
See complete working example Snippet at FullPage.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<nav id="navbar" class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="">AVC Gaming</a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
Forums <span class="caret"></span>
<ul class="dropdown-menu">
<li>Ban Appeal
</li>
<li>Registration
</li>
</ul>
</li>
<li class="button">Donate
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
UK 111 <span class="caret"></span>
<ul class="dropdown-menu">
<li>Forums
</li>
<li>Arma 2
</li>
<li>Arma 3
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
I'm using bootstrap and trying to put a search form inside a navbar dropdown menu, but I'm finding the form looks squashed because the width of the dropdown is too small. Also, the form elements are not how I would like. How do I fix this?
I'm new to CSS and web design in general. Please help!
Current layout:
What I'm trying to achieve:
<header class="navbar navbar-inverse navbar-fixed-top navbar-inverse" role="banner">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
Brand
</div>
<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Browse</li>
</ul>
<ul class="nav navbar-nav">
<li class="dropdown">
Locate <b class="caret"></b>
<ul class="dropdown-menu">
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<button type="button" class="btn btn-default navbar-btn"><li class="icon-screenshot"></li></button>
<input type="text" class="form-control" placeholder="Address">
</div>
<div class="btn-group">
<button type="submit" class="btn btn-default">Search</button>
<ul class="dropdown-menu" role="menu">
<li>Link</li>
<li>Link</li>
</ul>
</div>
</form>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
USER <b class="caret"></b>
<ul class="dropdown-menu">
<li><i class="icon-pencil"></i> Link</li>
<li><i class="icon-upload"></i> Link</li>
<li class="divider"></li>
<li><i class="icon-signout"></i> Link</li>
</ul>
</li>
</ul>
</nav>
</div>
</header>
Try this:
.navbar-nav>li>.dropdown-menu { width: yourwidth }
You can add display: inline-block so that its width will be according to its content automatically
You can increase the "min-width" value in the dropdown.less
or make a new line in the css
.dropdown-menu { min-width: 200px }