Bootstrap apply nav-stacked if small screen - html

Let's say we have a simple navigation bar like this:
http://getbootstrap.com/components/#nav-pills
<ul class="nav nav-pills">
<li role="presentation" class="active">Home</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
</ul>
How can I add the class "nav-stacked" to it when the window or parent element is smaller than say 940px. Or even better: as soon as the navigation bar can't fit all elements in the same line anymore.
This would be pretty straight forward in javascript, but I'd like to see a pure css/html solution, but without adding #media queries to the bootstrap source code.

Found the solution. There is a class called "nav-justified". It simply stacks the navigation pills as soon as they don't fit into a single row anymore.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<ul class="nav nav-pills nav-justified">
<li role="presentation" class="active">Home</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
</ul>

Try to add this CSS to your solution. Please check the result. Is it what you want to achieve?
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
#media (min-width: 768px) {
.nav-justified > li {
display: inline-block;
width: auto;
}
}
<ul class="nav nav-pills nav-justified">
<li role="presentation" class="active">Home</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
<li role="presentation">Profile</li>
<li role="presentation">Messages</li>
</ul>

Related

Fit all nav items in nav with equal width and no wrap

I am trying to make a nav menu with 15 elements. The elements must have equal width (which I achieved using nav-justified but when I resize the window, the menu keeps shrinking (expected behavior) until one point it stops and goes out of the screen.
Code (jsFiddle):
<ul class="nav nav-justified flex-nowrap">
<li class="nav-item"><a class="nav-link">Item 1</a></li>
<li class="nav-item"><a class="nav-link">Item 2</a></li>
<li class="nav-item"><a class="nav-link">Item 3</a></li>
<li class="nav-item"><a class="nav-link">Item 4</a></li>
<li class="nav-item"><a class="nav-link">Item 5</a></li>
<li class="nav-item"><a class="nav-link">Item 6</a></li>
<li class="nav-item"><a class="nav-link">Item 7</a></li>
<li class="nav-item"><a class="nav-link">Item 8</a></li>
<li class="nav-item"><a class="nav-link">Item 9</a></li>
<li class="nav-item"><a class="nav-link">Item 10</a></li>
<li class="nav-item"><a class="nav-link">Item 11</a></li>
<li class="nav-item"><a class="nav-link">Item 12</a></li>
<li class="nav-item"><a class="nav-link">Item 13</a></li>
<li class="nav-item"><a class="nav-link">Item 14</a></li>
<li class="nav-item"><a class="nav-link">Item 15</a></li>
</ul>
If you applay min-width for example with 1px the elements should shrink more
li, a {
min-width: 1px;
}
Also set overflow to no horicontal scrolling

Can't get dropdown buttons to sit next to each other on Bootstrap navbar

Before I begin, my apologies if this is a stupid question. We've been looking for a mega menu for our site and found this code:
https://bootsnipp.com/snippets/featured/bootstrap-mega-menu
It seemed easy to edit so I began making changes and came out with this:
<div class="container">
<nav class="navbar navbar-default">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".js-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>
<!--<a class="navbar-brand" href="#">MegaMenu</a>-->
</div>
<!-- OPEN -->
<div class="collapse navbar-collapse js-navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown mega-dropdown float-button">Holidays <span class="glyphicon glyphicon-chevron-down pull-right"></span>
<ul class="dropdown-menu mega-dropdown-menu row">
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 1</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
<li class="divider"></li>
<li class="dropdown-header">Destination 2</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
</ul>
</li>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 3</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
<li class="divider"></li>
</ul>
</li>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 4</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
</ul>
</li>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 5</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
<li class="dropdown-listing">Choice 3</li>
<li class="dropdown-listing">Choice 4</li>
<li class="dropdown-listing">Choice 5</li>
</ul>
</li>
</ul>
</li>
<!-- CLOSE -->
</div>
<!-- /.nav-collapse -->
</nav>
</div>
It seems to be working just fine however I wanted to add a second dropdown to the navbar and that is where things started to go wrong. I duplicated this part of the code:
<!-- OPEN -->
<div class="collapse navbar-collapse js-navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown mega-dropdown float-button">Holidays <span class="glyphicon glyphicon-chevron-down pull-right"></span>
<ul class="dropdown-menu mega-dropdown-menu row">
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 1</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
<li class="divider"></li>
<li class="dropdown-header">Destination 2</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
</ul>
</li>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 3</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
<li class="divider"></li>
</ul>
</li>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 4</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
</ul>
</li>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Destination 5</li>
<li class="dropdown-listing">Choice 1</li>
<li class="dropdown-listing">Choice 2</li>
<li class="dropdown-listing">Choice 3</li>
<li class="dropdown-listing">Choice 4</li>
<li class="dropdown-listing">Choice 5</li>
</ul>
</li>
</ul>
</li>
<!-- CLOSE -->
and pasted it directly underneath. It creates another working menu but it is sat underneath the first one rather than to the right as I had expected. What am I doing wrong? I'd like them to sit side by side when not collapsed.
Fresh eyes and any help would be greatly appreciated.
I hope you want this https://bootsnipp.com/user/snippets/Al77D
.mega-dropdown {
position: static !important;
width: 100%;
}
change this to
.mega-dropdown {
position: static !important;
}
just remove the width:100%; it is causing first menu to take entire width

Full width nav tabs in bootstrap 4 alpha

Hello how can make my nav-tabs full width, I tried using width:100%; to no avail.
here's the html code that i used.
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#learn" role="tab">Learn More</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#faq" role="tab">Frequently Asked Questions</a>
</li>
</ul>
There are build-in css classes to achieve this. From official docs:
To proportionately fill all available space with
your .nav-items, use .nav-fill. Notice that all horizontal space is
occupied, but not every nav item has the same width.
<ul class="nav nav-tabs nav-fill" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#learn" role="tab">Learn More</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#faq" role="tab">Frequently Asked Questions</a>
</li>
</ul>
For equal-width elements, use .nav-justified. All horizontal space
will be occupied by nav links, but unlike the .nav-fill above, every
nav item will be the same width.
<ul class="nav nav-tabs nav-justified" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#learn" role="tab">Learn More</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#faq" role="tab">Frequently Asked Questions</a>
</li>
</ul>
As per I understood you want to width:100% to <ul class="nav nav-tabs"> right?
But by default <ul class="nav nav-tabs"> is in width: 100% you do not need to give css to <ul> but if you want to width: 100% to <li class="nav-item"> then give css to it like this:
.nav-item {
width:100%;
}
or else instead of text-align: center give <ul class="nav justify-content-center"> to your html.
it might be helpful. you do not need to give css to ul.
<!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/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3>Tabs</h3>
<ul class="nav nav-tabs">
<li class="active">Home</li>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
</ul>
<br>
<p><strong>Note:</strong> This example shows how to create a basic navigation tab. It is not toggleable/dynamic yet (you can't click on the links to display different content)</p>
</div>
<h2>Basic Nav-tabs</h2>
<div class="container">
<h3>Inline List</h3>
<ul class="list-inline">
<li>Home</li>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
</ul>
</div>
</body>
</html>

Having another dropdown on a dropdown

I have this dropdown on my navbar which looks like this:
Now I added a "Contact Us" tab on "HR & Admin" menu.
So is it possible to make "Contact Us" a dropdown based on my code?
Here's my html:
<div class="collapse navbar-collapse" id="bs-navbar-collapse">
<ul class="nav navbar-nav main-navbar-nav">
<li class="active">HOME</li>
<li class="dropdown">
HR & Admin <span class="caret"></span>
<ul class="dropdown-menu">
<li>C.O.C.</li>
<li>Contact Us</li>
<li>Job Vacancies</li>
</ul>
</li>
Yes like so, leave the css to you of course:-
<ul class="nav navbar-nav main-navbar-nav">
<li class="active">HOME</li>
<li class="dropdown">
<a href="#" title="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">HR & Admin <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>C.O.C.</li>
<li>Contact Us
<ul class="dropdown-menu-2">
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</li>
<li>Job Vacancies</li>
</ul>
</li>
</ul>
Just add another ul li inside your Contact Us li like so:
<li>Contact Us</li>
<ul>
<li>Example 1</li>
<li>Example 2</li>
</ul>
</li>

Why isn't pull-right working for my Bootstrap navbar?

See the Information section of the navbar here. I'm trying to get it to float right. I used .pull-right like the Bootstrap docs say, but it isn't working.
I also tried messing around with Inspect Element and applying float: right !important; directly to the li, which didn't work, and then to the a, which also didn't work.
This is my code:
<div class="navbar"">
<div class="navbar-inner">
<ul class="nav">
<li class="divider-vertical"></li>
<li id="academics" class="dropdown main_college_nav_item">Academics <b class="caret"></b>
<ul class="dropdown-menu">
<li id="professors"><a>Professors</a></li>
<li id="classes"><a>Classes</a></li>
<li id="difficulty"><a>Difficulty</a></li>
<li id="major"><a>Majors</a></li>
</ul>
</li>
<li class="divider-vertical"></li>
<li id="living_environment" class="dropdown main_college_nav_item">Living Environment <b class="caret"></b>
<ul class="dropdown-menu">
<li id="campus"><a>Campus</a></li>
<li id="food"><a>Food</a></li>
<li id="housing"><a>Housing</a></li>
<li id="weather"><a>Weather</a></li>
<li id="safety"><a>Safety</a></li>
</ul>
</li>
<li class="divider-vertical"></li>
<li id="social_life" class="dropdown main_college_nav_item"><a class="dropdown-toggle" data-toggle="dropdown">Social Life <b class="caret"></b></a>
<ul class="dropdown-menu">
<li id="kids"><a>Kids/Atmosphere</a></li>
<li id="parties"><a>Parties</a></li>
</ul>
</li>
<li class="divider-vertical"></li>
<li id="information" class="main_college_nav_item"><a class="pull-right">Information <i class="icon-resize-vertical"> </i></a></li>
<li class="divider-vertical"></li>
</ul>
</div>
</div>
Use the below HTML instead of your HTML.
I have created Separate ul for the Information tab and floated to right.
Update: Working Demo
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li class="divider-vertical"></li>
<li id="academics" class="dropdown main_college_nav_item">Academics <b class="caret"></b>
<ul class="dropdown-menu">
<li id="professors" class="active"><a>Professors</a></li>
<li id="classes"><a>Classes</a></li>
<li id="difficulty"><a>Difficulty</a></li>
<li id="major"><a>Majors</a></li>
</ul>
</li>
<li class="divider-vertical"></li>
<li id="living_environment" class="dropdown main_college_nav_item">Living Environment <b class="caret"></b>
<ul class="dropdown-menu">
<li id="campus"><a>Campus</a></li>
<li id="food"><a>Food</a></li>
<li id="housing"><a>Housing</a></li>
<li id="weather"><a>Weather</a></li>
<li id="safety"><a>Safety</a></li>
</ul>
</li>
<li class="divider-vertical"></li>
<li id="social_life" class="dropdown main_college_nav_item"><a class="dropdown-toggle" data-toggle="dropdown">Social Life <b class="caret"></b></a>
<ul class="dropdown-menu">
<li id="kids"><a>Kids/Atmosphere</a></li>
<li id="parties"><a>Parties</a></li>
</ul>
</li>
<li class="divider-vertical"></li>
</ul>
<ul class="nav pull-right">
<li class="divider-vertical"></li>
<li id="information" class="main_college_nav_item"><a>Information <i class="icon-resize-vertical"> </i></a></li>
</ul>
</div>
</div>