How to style Bootstrap dropdown-menu to be full page width? - html

I need to modify the dropdown-menu provided in Bootstrap, using the navbar-fixed-top, to appear on hover not vertically but horizontally li { display: inline-block } (that's easy), but so I need the actual ul.dropdown-menu to stretch the full width of the page. I can't seem to figure it out.
Don't give me a megamenu plugin or anything, please, just how can I fix it to stretch the width of the whole page? (not the page container either, the window)
Will probably need to wrap another element too, actually, as the ul needs to be centered.
So does anyone know how to do this?
EDIT: Figured it out (like 5 minutes after posting this) and with no added elements:
.nav { margin-bottom: 0; }
.dropdown { position: static; }
.dropdown-menu { width: 100%; text-align: center; }
.dropdown-menu>li { display: inline-block; }
and there you have it!

First you shouldn't wrap the navbar component in a div.container then update the css with the following Code:
.nav > li.dropdown.open {
position: static;
}
.nav > li.dropdown.open .dropdown-menu {
display:table; width: 100%; text-align: center; left:0; right:0;
}
.dropdown-menu>li {
display: table-cell;
}
check the demo here http://www.bootply.com/8EgGsi4F7w

<li class="dropdown open" style="position: initial;">
<ul class="dropdown-menu" style="width: 100%;">
just use the position: initial in the main li(dropdown) of your nav and in the child ul dropdown-menu set width 100%

Here the alternative that worked for me.
I just tried option 2. The key is:
Adding class position-static along with (the) dropdown class, which is the parent class of dropdown-menu as follows:
<li class="nav-item dropdown position-static">
Credit to its author: VigneshKannan3 from GeeksforGeeks
It looks like this answer to a similar question has the same approach.

Check this
.nav > li.dropdown.open {
position: static;
}
.nav > li.dropdown.open .dropdown-menu {
display: table;
border-radius: 0px;
width: 100%;
text-align: center;
left: 0;
right: 0;
}
.dropdown-menu > li {
display: table-cell;
height: 50px;
line-height: 50px;
vertical-align: middle;
}
#media screen and (max-width: 767px) {
.dropdown-menu > li {
display: block;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<div class="navbar-brand">Blackcat</div>
<button class="navbar-toggle" data-toggle="collapse" data-target=".btnCollapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse btnCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">Shop</li>
<li class="dropdown">
Artist <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Rich</li>
<li>Shay</li>
<li>Jose</li>
<li>Marie</li>
<li>Simon</li>
<li>Jamie</li>
<li>Andrew</li>
<li>Teddie</li>
</ul>
</li>
<li>FAQs</li>
<li>Contact</li>
</ul>
</div>
</div>
<!-- end container -->
</div>
<!-- end navbar navbar-inverse navbar-static-top -->
<!-- container -->
<div class="container">
<div class="row">
<p>Site content here...</p>
</div>
<!-- End row -->
</div>
<!-- End container -->

Related

Is there a way to keep my navbar-right to the right and always visible at top of page when scrolling?

Initial load:
After scrolling:
I'm trying to have a fixed nav at the top of the viewport. So as you scroll down the page the nav is always visible. My problem is that I have my navbar brand set to the left of the nav and my links to the rest of my page set to the right. When I add position fixed to the navbar brand it works fine (the brand stays visible as I scroll down the page), however no matter what I've tried when I add position fixed to any other classes, id's, or elements involving the links set to the right, they end up getting fixed to the left and overlaying the brand or sitting directly under the brand. I cannot figure out why this is happening.
I've tried adding position fixed to various classes within the navbar with no luck. I also tried forcing the items to the right with float: right!important. Most of the articles I've found suggest adding position: fixed to the .nav class, but when I do that the items are pushed to the left. I included the html for my nav as well as the CSS for the nav, the background-video and the modal I link to in the nav just in case there is some reason this may be causing the problem. Thanks for any help! It's much appreciated.
HTML
<nav class="navbar">
<div class="container-fluid">
<div class="navbar-header">
<h3>allicndev</h3>
</div><!-- /navbar-header -->
<ul class="nav navbar-nav navbar-right">
<li class="active">About</li>
<li class="active">Portfolio</li>
<li class="active">
<!-- <span>Resume</span> -->
Resume
</li>
<li class="active">Skills</li>
<li class="active">Contact</li>
</ul><!-- /nav navbar-nav navbar-right -->
</div><!-- /container-fluid -->
</nav>
<!-- /navigation -->
<!-- modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">X</button>
<h4 id="myModalLabel" class="modal-title">Resume</h4>
</div><!-- /modal-header -->
<div class="modal-content">
<embed id="modal-embed" src="assets/anresume.pdf">
</div><!-- /modal-content -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div><!-- /modal-footer -->
</div><!-- /modal fade -->
<!-- /modal -->
CSS
* {
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
position: relative;
}
body {
margin: 0;
font-family: 'Montserrat', Arial, sans-serif;
font-size: 17px;
}
#myVideo {
position: absolute;
right: 0;
bottom: 0;
min-width: 100%;
min-height: 100%;
}
.container-fluid {
padding-right: 30px;
/* position: fixed; */
}
nav {
height: 75px;
display: border-box;
color: white;
font-size: 12px;
text-align: center;
position: fixed;
/*z-index: 10px;*/
}
nav.navbar {
width: 100%;
}
.navbar-header {
position: fixed;
}
.nav.navbar-nav.navbar-right > li {
padding-top: 15px;
margin: 10px;
}
.nav > li > a {
display: inline;
padding-left: 0;
padding-right: 0;
padding-top: 5px;
background-color: transparent;
}
.navbar-nav>.active>a, .navbar-nav>.active>a:focus, .navbar-nav>.active>a:hover {
background-color: transparent;
}
a.db-line {
transition: white .3s linear;
}
a {
color: white;
text-decoration: none;
}
a > h3 {
color: white;
text-decoration: none;
}
a:hover {
color: #f97f04;
}
/* a:visited {
color: #EDCF10;
} */
.modal {
display: none;
height: 100%;
left: 10px;
position: fixed;
top: 0;
width: 100%;
}
.modal.open {
display: block;
}
.modal-header,
.modal-footer {
height: 75px;
}
.modal-content,
.modal-footer {
width: 100%;
}
.modal-footer {
bottom: 0px;
}
#modal-embed {
width: 100%;
height: 75vh;
}
If possible I expect the brand ("allicndev") to be to the left top and the links ("about", "portfolio", "resume", "skills", and "contact") to be to the right top and the whole nav to stay visible this way as the page is scrolled through or links targeted to sections are clicked.
Yes, you should use the class fixed-top for fixed top navbar while scrolling
<div class="navbar fixed-top">
</div>
The markup in your example is for Bootstrap 3, so that's problem #1, given that you've tagged this Bootstrap 4.
Assuming Bootstrap 4 is correct, adding the correct markup and classes seems to create what you're describing: https://jsfiddle.net/4rtjcpzm/1/
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<nav class="navbar navbar-expand fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="#home">allicndev</a>
<div class="collapse navbar-collapse in" id="navbarCollapse">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item active">About</li>
<li class="nav-item ">Portfolio</li>
<li class="nav-item ">
<!-- <span>Resume</span> -->
Resume
</li>
<li class="nav-item "><a class="nav-link" href="#skills">Skills</a></li>
<li class="nav-item "><a class="nav-link" href="#contact">Contact</a></li>
</ul><!-- /nav navbar-nav navbar-right -->
</div>
</div><!-- /container-fluid -->
</nav>
The ml-auto (margin-left: auto, if you check the source) class is what allows the links to slide all the way to the right. fixed-top and sticky-top will both stick the navbar to the top of the viewport.
sticky-top is probably the better class to use in terms of not needing to bump the body content down (the padding-top you see in the CSS part of the fiddle), but that depends on whether you know (or care) if most of your expected visitors will use a browser that supports position: sticky;.
As an aside, do note that a > h3 (an H3 element inside an anchor element) is not (by default) valid HTML; <a> is an inline element, and <h3> is a block element, which is invalid according to the HTML spec.

Issues with Mobile Navbar

Edit #1: After I give my height a definition of 89px, my mobile menu seems to go behind my content.
I'm having an issue with the mobile version of my navbar. The navbar isn't as tall as logo, so when I click on it my "Home" menu item isn't being shown unless I hover over it.
Here's the pictures for both issues:
Here's my HTML:
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="row">
<div class="navbar-header">
<a class="navbar-brand " style="margin-top: -15px; float: left;" href="#">
<img alt="Brand" class="pull-left" style="margin-left: -15px;" src="Images/logo-resized.png">
</a>
<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav">
<li>Home</li>
<li>Internet</li>
<li>Phone</li>
<li>Android TV</li>
<li>Shaw Direct</li>
<li>Contact Us</li>
</ul>
</div>
</div>
</div>
</nav>
And here's my CSS:
.navbar-default {
background-color: #00AEFE;
}
.nav.navbar-nav li a{
color: white;
font-size: 18px;
line-height: 50px;
}
.col-xs-0 {
display: none;
}
nav {
height: 89px;
}
In bootstrap navbar-brand has some padding, also the height of <img> should be controled by additional css like navbar-brand img {height:40px}.
although if we could see your live html, it was nice.
EDITED
use this CSS to give some height longer than height of your image like 100px or higher
#media (min-width: 768px) {
.navbar-header {
height: 100px;
}
}
OR
If you don't want to change the height of navbar-header you can add some padding to navbar-toggle button so it will add height of navbar-header:
#media (min-width: 768px) {
.navbar-toggle {
padding: 33px 10px;
}
}
<a class="navbar-brand " style="margin-top: -15px; float: left;height:auto" href="#">
<img alt="Brand" class="pull-left" style="margin-left: -15px;" src="Images/logo-resized.png">
</a>
Change the height of navbar-brand class
OR
<style>
.navbar-brand img {
max-height: 40px;
width: auto;
}
</style>

Prevent Elements in Navbar from going to a second row in Bootstrap 3

I'm struggling with the alignment in the following navbar that uses Bootstrap 3.
Problem: The Login and Signup elements are on different rows. Whats the correct way to adjust them so that they are both on the same line? display: inline-block; and white-space: nowrap; does not appear to work on the li elements.
Note that the input element should fill up the space between the logo and the Login/Signup elements.
Bootply: http://www.bootply.com/Bh5TBV9vS0
<nav class="navbar navbar-default navbar-fixed-top">
HTML
<a class="brand" href="#">
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png" width="100">
</a>
<form role="form" class="form-horizontal search-form">
<!-- <i class="fa fa-search"></i> -->
<input type="text" class="form-control" placeholder="Search">
</form>
<ul class="nav navbar-nav">
<li>Login</li>
<li>Sign up</li>
</ul>
</nav>
CSS:
.brand {
width: 170px;
height: 80px;
background-color: #ccc;
display: inline-block
}
.navbar.transparent.navbar-inverse .navbar-inner {
background: rgba(0,0,0,0.4);
}
.search-form {
width: 100%;
height: 30px;
display: inline-block;
}
.search-form {
margin-top:10px;
padding: 0 10px;
}
nav { display: flex; }
Remove float:left for your list items and apply dispaly:inline-block and apply the style white-space:nowrap for your ul element.
ul.navbar-nav{ white-space:nowrap;}
.navbar-nav li{display:inline-block; float:none;}
I have updated width of .search-form from 100% to 76% and now it works fine.
Check here

Collapsed bootstrap menu bar not working

When my web page is shown on small devices the menu collapses as expected but when you click on the collapsed menu icon the menu does not appear. There is a slight adjustment of the vertical height of the page below the menu and the last menu item half appears along the top of the page but the rest of the menu is nowhere to be seen.
My Bootstrap code looks like this:
#myNavbar {
position: relative;
border:none;
}
#myNavbar .nav {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: -10px;
}
.navbar-nav.navbar-right:last-child {
margin-right: 0;
}
.navbar-default {
background:#FFFFFF;
border-left:none;
border-right:none;
border-top:none;
border-bottom:solid;
border-bottom-width:2px;
border-color:#01B6AC;
width:100% !important;
margin-right:0px;
padding-right:0px;
border-radius: 0 !important;
-moz-border-radius: 0 !important;
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img class="img-responsive" src="/assets/img/outa.png" width="120px;"/></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav pull-right">
<li>Saved searches</li>
<li>Settings</li>
<li>About</li>
<li>Sign In</li>
</ul>
</div>
</div>
</nav>
Anyone know what I am doing wrong?
You problem is here
#myNavbar .nav {
position: absolute;
}
When you use position:absolute; the element will be positioned absolutely from the first positioned parent div, if it can't find one it will position absolutely from the window. You can refer to THIS question for further info. So what you can do is add #media query and set a size over certain pixels where the position will be absolute. So something like
#media(min-width: 400px) {
#myNavbar .nav {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: -10px;
}
}
just give proper reference of bootstrap.css and bootstrap.js and your code will work fine.

Centering links (within li's) in Bootstrap navbar

I've implemented the solution here which almost solves my problem, however, I need the <ul class="nav"> to be centered between the entire navbar and not just the elements adjacent to it (<a class="brand"> and <p class="navbar-text pull-right"> in my case).
Important: I customized my navbar to be 96px instead of the default 40px, and I want the <ul class="nav"> to appear centered and "underneath" the other elements as it will have its own background-color etc.
Here is my code so far:
<div class="navbar navbar-inverse navbar-fixed-top center">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/home"><img src="http://jayeshcp.files.wordpress.com/2013/03/twitter-bootstrap.jpg" style="width:400px;height:60px;" /></a>
<div class="nav-collapse collapse">
<ul class="nav">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<p class="navbar-text pull-right">
<input class="span2" type="text" placeholder="Email">
<input class="span2" type="password" placeholder="Password">
</p>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
Note the extra center class that I added - that is from the solution that I linked to. It looks like this:
.center.navbar .nav,
.center.navbar .nav > li {
float:none;
display:inline-block;
*display:inline; /* ie7 fix */
*zoom:1; /* hasLayout ie7 trigger */
vertical-align: top;
}
.center .navbar-inner {
text-align:center;
}
.center .dropdown-menu {
text-align: left;
}
How do I center the ul and give it padding above without breaking everything else in Bootstrap? Would offer more than bounty if there was another currency!
edit: jsfiddle is here (I pasted the whole minified css file (with the customized navbar height) instead of referencing it, sorry. Underneath it are the other customizations.)
p.navbar-text
{
float: none;
position: relative;
text-align: right;
}
.navbar .nav
{
display:block !important;
float: right !important;
width: 100%;
}
jsfiddle: http://jsfiddle.net/C7LWm/103/
Here's the solution for placing it underneath instead of below:
.navbar .nav
{
position: absolute;
left:0;
right:0;
width: 100%;
background-color:yellow;
}
p.navbar-text
{
width: 440px;
text-align:right;
position:relative;
z-index:2
}
.navbar .brand
{
position: relative;
z-index:2;
padding: 0;
width: 30%;
height: 60px;
background-image: url('http://placehold.it/400x60&text=Your Logo Here');
background-repeat: no-repeat; background-size: contain;
margin: 18px 0;
background-position: center left
}
.navbar .brand img
{
display:none;
}
Jsfiddle: http://jsfiddle.net/C7LWm/109/