How To Restrict Width of Bootstrap 3 Dropdown-Menu in Navbar - css

I would like to prevent the Bootstrap .dropdown-menu from ever being wider than the main nav bar (which I have restricted to be the width of the .container class). I'd like for the dropdown menu to take up the width of the container class, but it shouldn't extend beyond that on either side.
Any ideas how to accomplish this, preferably using CSS?
Here's an example of what it currently looks like and what I'd like it to look like:
Here's a boiler plate navbar with a dropdown menu that has one really long menu item:
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<!-- 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">
<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</li>
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action a really long menu item that extends way beyond the limits of "container" and may even extend beyond the view port area to the right, making some of the text unreadable.</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
</div>

Explanation
Bootstrap's adds position: absolute to the .dropdown-menu class. As you may know, all absolutely positioned elements are positioned in relation to the first parent element they find with position: relative. In Bootstrap, this is provided by the .dropdown wrapper
So if you want to position the element relative to the container, and not the nav item, we'll have to remove relative positioning from the .dropdown wrapper. You can do this by resetting the value to the initial value for the position property, which is static.
Congratulations! The menu is no longer constrained by the .dropdown element, but we still have some work to do.
Because bootstrap was not intending to space constrain the menu, menu items are given the property white-space: nowrap so they'll extend as long as they need. Think lines of code inside code blocks here on stack overflow (1 line = 1 line). Since we want the line to eventually end, this won't do. So we'll reset the anchor tags back to white-space: normal.
At this point the .dropdown-menu should take up the full size of the .navbar (which itself takes up the full size of the .container). This is where yamm3 is doing something really cool. It sets left: auto on the dropdown-menu.
According to MDN on the left property:
auto is a keyword that represents:
for absolutely positioned elements, the position of the element based on the right property and treat width: auto as a width based on the content.
So setting the .dropdown-menu to left:auto will cause the menu to start in its current location and extend all the way to the right of the container.
Just Codes
Just add the .fill-width class to your .dropdown element and include the following CSS:
.fill-width.dropdown {
position: static;
}
.fill-width.dropdown > .dropdown-menu {
left: auto;
}
.fill-width.dropdown > .dropdown-menu > li > a {
white-space: normal;
}
Working Demo in jsFiddle
.full-width.dropdown {
position: static;
}
.full-width.dropdown > .dropdown-menu {
right: 0;
}
.full-width.dropdown > .dropdown-menu > li > a {
white-space: normal;
}
.fill-width.dropdown {
position: static;
}
.fill-width.dropdown > .dropdown-menu {
left: auto;
}
.fill-width.dropdown > .dropdown-menu > li > a {
white-space: normal;
}
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<div class="container">
<div class="navbar navbar-inverse">
<!-- Header -->
<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>
<a class="navbar-brand" href="#">
Bootstrap 3 Skeleton
</a>
</div>
<!-- Navbar Links -->
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Normal <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
</a></li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
<li class="dropdown full-width">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Full Width <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
</a></li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
<li class="dropdown fill-width">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Fill Width <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
</a></li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
</div>
</div>
</div>

Related

Scaling a logo in bootstrap navbar on resizing browser or when the user browser zoom is not 100%?

For the last 4 hours i am trying to get an image scaled when the browser resizes. The website uses bootstrap and the image is situated in the navbar-brand part. I know the navbar-brand part is normally used for a logo, but in this situation there is a bunch of text under the logo which is part of the image.
Now what i like to accomplish is to scale this image when the browser resizes. I have tried a bunch of different css option without any success.
My latest:
.navbar-brand {
max-height: 500px;
}
.navbar-brand img
{
max-height: 100%;
height: auto;
width: auto;
}
I could use some fresh input ?
As you mentioned in comments, the image will take certain percentage (31% in your case) of the navbar width. You can set width as percentage to div.navbar-header which is the wrapper of img.navbar-brand. Media query is added to make the added style compliance with Bootstrap navbar responsive rules.
#media (min-width: 768px) {
.container-fluid>.navbar-header,
.container>.navbar-header {
width: 31%; /*31% as you mentioned in comments*/
}
img.navbar-brand {
width: auto;
height: auto;
max-height: 500px;
max-width: 100%;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<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>
<img src="https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/flip.jpg" class="navbar-brand">
</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>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>

Nav bar disappears at 769px width

I am using the Bootstrap navbar-fixed-top method to create a floating nav bar.
I have a few issues I cannot figure out:
Why are my logo and my social icons disappearing when I reduce screen-width to 769px?
Why is my logo and social icons right at the top of the page?
My social icons are too small and bunched up next to each other. How do I change this?
http://codepen.io/dhruvghulati/pen/qrdNBm
What should I do to have a persistent nav bar which is more standard?
.collapse class looks like this:
.collapse {
display: none;
visibility: hidden;
}
And there is:
#media (min-width: 768px)
.navbar-collapse.collapse {
display: block!important;
height: auto!important;
padding-bottom: 0;
overflow: visible!important;
visibility: visible!important;
}
Which means .collapse will be not displayed until screen is 768px width.
Also there is (used for logo):
#media (min-width: 768px)
.navbar-left {
float: left!important;
}
That makes logo float left since screen is 768px width, there is similar class for .navbar-right which is used for social icons container.
You should probably visit the official Bootstrap docs to get more information on navbars. I would use this as the template for your navbar and go from there:
<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 -->
Essentially, Bootstrap replaces your nav link with a hamburger button at that screen size. Also, you should give the logo the proper classes (from the code above) to make sure it's always visible. To make the navbar stick to the top of the page, simply add the navbar-fixed-top class to the nav tag. the opening tag should now look like this: <nav class="navbar navbar-default navbar-fixed-top">.
I think its because you are using position:absolute; try using position: relative; instead

how to change navbar bootstrap height and make the text keep in the middle? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
There're so many ways I've tried to edit bootstrap navbar. Actually, for changing the color, and costuming its height had already done. However, the texts left behind.this is the case.
EDIT:
If you want to make the navbar less than 50px you need to override css property min-height in .navbar-default from bootstrap.
JSFiddle
Note:
This will affect your hamburger menu in smaller devices though. You could make it smaller in 768px and above without defining the min-height in .navbar default, but doing it in a media query, like this:
#media (min-width:768px) {
.navbar-default {
min-height:30px;
}
}
SOLUTION:
When you change your navbar's height, vertical aligning the anchors can be done doing the following:
Remove the top and bottom padding from the anchors.
Set the anchor's line-height with the same value as your navbar's height.
JSFiddle
CODE SNIPPET:
.navbar-default {
height: 80px;
}
.navbar-default .navbar-nav > li > a,
.navbar-brand {
padding-top: 0;
padding-bottom: 0;
line-height: 80px;
}
.navbar-form {
margin-top: 22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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 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" role="search">
<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>

Boostrap Dropdown moves other divs down

When I click the dropdown button the other content under it moves down with it. The I've tried z-index, position:relative; position:static How can I prevent this from happening. I don't want anything to move when the dropdown is clicked. Any ideas?
<div class='whiteBox'>
<div class='newProducts'></div>
<div id="compareInfo">
<div id="comparisionTitle">Title</div>
<div class="divStyle">
<div class="btn-group">
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown" id="chooseItem">Drop down Box<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a id="item1DropDown" href="#">item1</a></li>
<li>item2</li>
<li>item3</li>
</ul>
</div>
<!-- /btn-group -->
</div>
<div id="compareBoxes">
<div id="Label">Text under Dropdown</div>
<button class=
"btn btn-compare" id="compareButton" type=
"submit">Submit</button>
</div>
</div>
</div>
#divStyle{
list-style:none;
position:relative;
float:left;
right:240px;
}
You have to set the div you want to have a dropdown menu like this:
list-style:none;
position:relative;
float:left;
And then set your own position for it.
Here is an excellent customizable tutorial on it.
You seem to be missing the dropdown class on your div, and the role field for the li and a tags within your dropdown. Try adding role="presentation" to those.
Like:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle"...>...</button>
<ul class="dropdown-menu">
<li role="presentation"><a role="presentation ...>item1</a></li>
</ul>
</div>
This is a simplified version of a typical bootstrap navbar dropdown (in the documention you can see it working in the original navbar)
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
and when it's clicked it toogles display:block and the position is always
position: absolute;
top: 100%;
left: 0;
z-index: 1000;
Since bootstrap highly depends on the it's markup you need to strictly reference the documentation, so I suggest you go over your code to see what's missing.

Bootstrap nav row resizes smoothly, but active or highlighted links are only partially highlighted

On smaller screen, one of the links in this nav (nav-pills, nav-justified) takes up two lines, which makes the "box" size bigger for the other links in the nav as well. That's great, except that active or highlighted links only highlight part of the box, as shown in the image below. It doesn't look so good, yeah?
image of nav row
How can I make the active or highlighted part fill up the whole box?
Also, how can I vertically center the link within the box?
Here's the relevant html:
<nav>
<ul class="nav nav-pills nav-justified">
<li>Atmosphere</li>
<li class="active">Food</li>
<li>Drink</li>
<li>Access & Info</li>
<li>Coupons</li>
<li>Reservation</li>
</ul>
</nav>
And css:
header, section, footer, aside, nav, main, article, figure {display: block;}
nav > ul > li {
border: 4px #abcabc;
border-style: double;
}
nav > ul > li > a {
color: #c30200;
font-family: Garamond, sans-serif;
font-size: 18px;
}
Try this:
Normally highlighted part fill up the whole box. If its not means post your code,
For vertically center you need to apply text-center
DEMO
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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 text-center" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link</li>
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>

Resources