26 KiB
layout | title |
---|---|
page | Navbar |
Default navbar
Navbars are responsive meta components that serve as navigation headers for your application or site. They begin collapsed (and are toggleable) in mobile views and become horizontal as the available viewport width increases.
Requires JavaScript
If JavaScript is disabled and the viewport is narrow enough that the navbar collapses, it will be impossible to expand the navbar and view the content within the .navbar-collapse
.
Changing the collapsed mobile navbar breakpoint
The navbar collapses into its vertical mobile view when the viewport is narrower than @grid-float-breakpoint
, and expands into its horizontal non-mobile view when the viewport is at least @grid-float-breakpoint
in width. Adjust this variable in the Less source to control when the navbar collapses/expands. The default value is 768px
(the smallest "small" or "tablet" screen).
<!-- 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"><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></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-secondary">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
{% highlight html %}
<!-- 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"><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></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-secondary">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
Plugin dependency
The responsive navbar requires the collapse plugin to be included in your version of Bootstrap.
Make navbars accessible
Be sure to add a role="navigation"
to every navbar to help with accessibility.
Brand image
Replace the navbar brand with your own image by swapping the text for an <img>
. Since the .navbar-brand
has its own padding and height, you may need to override some CSS depending on your image.
Forms
Place form content within .navbar-form
for proper vertical alignment and collapsed behavior in narrow viewports. Use the alignment options to decide where it resides within the navbar content.
As a heads up, .navbar-form
shares much of its code with .form-inline
via mixin. Some form controls, like input groups, may require fixed widths to be show up properly within a navbar.
Mobile device caveats
There are some caveats regarding using form controls within fixed elements on mobile devices. See our browser support docs for details.
Always add labels
Screen readers will have trouble with your forms if you don't include a label for every input. For these inline navbar forms, you can hide the labels using the .sr-only
class.
Buttons
Add the .navbar-btn
class to <button>
elements not residing in a <form>
to vertically center them in the navbar.
Context-specific usage
Like the standard button classes, .navbar-btn
can be used on <a>
and <input>
elements. However, neither .navbar-btn
nor the standard button classes should be used on <a>
elements within .navbar-nav
.
Text
Wrap strings of text in an element with .navbar-text
, usually on a <p>
tag for proper leading and color.
Signed in as Mark Otto
{% endhighlight %}Non-nav links
For folks using standard links that are not within the regular navbar navigation component, use the .navbar-link
class to add the proper colors for the default and inverse navbar options.
Signed in as Mark Otto
{% endhighlight %}Component alignment
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.
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.
We'll revisit this when we can rewrite that component in v4.
Fixed to top
Add .navbar-fixed-top
and include a .container
or .container-fluid
to center and pad navbar content.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-6">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
</nav>
Body padding required
The fixed navbar will overlay your other content, unless you add padding
to the top of the <body>
. Try out your own values or use our snippet below. Tip: By default, the navbar is 50px high.
Make sure to include this after the core Bootstrap CSS.
Fixed to bottom
Add .navbar-fixed-bottom
and include a .container
or .container-fluid
to center and pad navbar content.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-7">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
</nav>
Body padding required
The fixed navbar will overlay your other content, unless you add padding
to the bottom of the <body>
. Try out your own values or use our snippet below. Tip: By default, the navbar is 50px high.
Make sure to include this after the core Bootstrap CSS.
Static top
Create a full-width navbar that scrolls away with the page by adding .navbar-static-top
and include a .container
or .container-fluid
to center and pad navbar content.
Unlike the .navbar-fixed-*
classes, you do not need to change any padding on the body
.
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-8">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
</nav>
Inverted navbar
Modify the look of the navbar by adding .navbar-inverse
.
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-9">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>