Navbar
Create flexible, responsive navigation bars. Includes support for branding, navigation, and a built-in drawer for mobile.
The Idea React Navbar wrapper composes the Bootstrap 6 .navbar markup into semantic
React components. This guide adapts the Bootstrap Navbar documentation.
Example
Here's a navbar that includes most supported sub-components and a responsive right drawer with the
Drawer wrapper. Use the lower-right corner grip to resize your browser and preview the
responsive behavior.
How it works
Here's what you need to know before getting started with the navbar:
- Navbars require a wrapping
.navbarwith a breakpoint-firstnavbar-expandclass (for example,md:navbar-expand) for responsive collapsing and color scheme classes. Set theexpandprop to'md'to render that class. - Navbars and their contents are fluid by default. Change the container to limit their horizontal width in different ways.
- Use margin, padding, and flex utility classes for controlling spacing and alignment within navbars.
- Navbars are responsive by default using the drawer component. On small viewports, navigation links slide in from the side as a Drawer.
- Ensure accessibility by using a
<nav>element—Navbaralways renders one—or addrole="navigation"to a generic element. - Indicate the current item with
aria-current="page"for the current page oraria-current="true"for the current item in a set.
The drawer animation honors prefers-reduced-motion. Bootstrap's Callout for reduced motion
applies to the React Drawer as well.
Supported content
Navbars come with built-in support for a handful of sub-components. Choose from the following as needed:
NavbarBrandfor your company, product, or project name.NavbarNavfor a full-height and lightweight navigation, including support for menus.NavbarTogglerfor use with the drawer and other navigation toggling behaviors.- Flex and spacing utilities for any form controls and actions.
NavbarTextfor adding vertically centered strings of text.
Brand
The .navbar-brand can be applied to most elements, but an anchor works best, as some elements might
require utility classes or custom styles. Use NavbarBrand for the anchor form.
Text
Add your text within an element with the .navbar-brand class.
Image
You can replace the text within the .navbar-brand with an image. The Image wrapper renders
Bootstrap's image utilities.
Image and text
You can also make use of some additional utilities to add an image and text at the same time. Note
the addition of .d-inline-block and .align-text-top on the image.
Nav
Navbar navigation links build on the .nav options with their own modifier class and require the
use of toggler classes for proper responsive styling. Navigation in navbars will also grow to
occupy as much horizontal space as possible to keep your navbar contents securely aligned.
Add the .active class on .nav-link to indicate the current page. Please note that you should also
add the aria-current attribute on the active .nav-link. The NavItem and NavLink
wrappers keep these attributes and the disabled state consistent.
And because we use classes for our navs, you can avoid the list-based approach entirely if you like.
You can also use menus in your navbar. Menus require a wrapping element for positioning, so be sure
to use separate and nested elements for .nav-item and .nav-link as shown below. The Menu
wrapper manages open state and keyboard navigation for the trigger and panel.
Forms
Place various form controls and components within a navbar:
Immediate child elements of .navbar use flex layout and will default to
justify-content: space-between. Use additional flex utilities as needed to adjust this behavior.
Input groups work, too. If your navbar is an entire form, or mostly a form, you can use the <form>
element as the container and save some markup.
Various buttons are supported as part of these navbar forms, too. This is also a great reminder that vertical alignment utilities can be used to align different sized elements.
Text
Navbars may contain bits of text with the help of .navbar-text. This class adjusts vertical
alignment and horizontal spacing for strings of text.
Mix and match with other components and utilities as needed.
Dark navbar
To make a navbar dark, add data-bs-theme="dark" to the .navbar element. The theme prop renders
that attribute for you.
Translucent navbar
Add .navbar-translucent to blur and saturate the background of a navbar. This works well for
navbars positioned over content, such as fixed or sticky navbars.
Color schemes
Navbar themes are easier than ever thanks to Bootstrap's combination of Sass and CSS variables. The
default is the "light navbar" for use with light background colors, but you can also apply
data-bs-theme="dark" to the .navbar parent for dark background colors. Then, customize with
.bg-* and additional utilities.
<nav class="navbar" data-bs-theme="dark">
<!-- Navbar content -->
</nav>
<nav class="navbar bg-primary" data-bs-theme="dark">
<!-- Navbar content -->
</nav>
<nav class="navbar bg-1" data-bs-theme="light">
<!-- Navbar content -->
</nav>
Containers
Although it's not required, you can wrap a navbar in a .container to center it on a page—though
note that an inner container is still required. Or you can add a container inside the .navbar to
only center the contents of a fixed or static top navbar.
Use any of the responsive containers to change how wide the content in your navbar is presented.
Placement
Use Bootstrap's position utilities to place navbars in non-static positions. Choose from fixed to the top, fixed to the bottom, stickied to the top (scrolls with the page until it reaches the top, then stays there), or stickied to the bottom.
Fixed navbars use position: fixed, meaning they're pulled from the normal flow of the DOM and may
require custom CSS (for example, padding-top on the <body>) to prevent overlap with other
elements.
<Navbar className="fixed-top">…</Navbar>
<Navbar className="fixed-bottom">…</Navbar>
<Navbar className="sticky-top">…</Navbar>
<Navbar className="sticky-bottom">…</Navbar>Responsive behaviors
Navbars can use NavbarToggler and breakpoint-first navbar-expand classes to determine when their
content appears in a drawer or inline. In combination with other utilities, you can easily choose
when to show or hide particular elements.
For navbars that never collapse, add the navbar-expand class on the navbar by setting expand
without a breakpoint. For navbars that always show the drawer, omit expand.
Drawer
By default, navbars use the drawer component for their responsive behavior. This provides a modern drawer-style menu that slides in from any side of the screen.
Drawer placement
You can customize which side the drawer appears from using drawer placement classes:
.drawer-start- slides in from the left (or right in RTL).drawer-end- slides in from the right (or left in RTL).drawer-top- slides in from the top.drawer-bottom- slides in from the bottom
The placement prop on Drawer and the navbar demos maps to these classes. These examples omit the
navbar-expand class to always show the drawer behavior. Click the toggler to see the drawer slide
in from different directions.
Toggler
Navbar togglers are left-aligned by default, but should they follow a sibling element like a
.navbar-brand, they'll automatically be aligned to the far right. Reversing your markup will
reverse the placement of the toggler.
These examples omit the navbar-expand class to always show the collapsed state with the toggler
visible. Click the toggler to open the drawer.
With no .navbar-brand shown (hidden inside the drawer):
With a brand name shown on the left and toggler on the right:
With a toggler on the left and brand name on the right:
External content
Sometimes you want to use the drawer to trigger a container element for content that structurally
sits outside of the .navbar. Because the React wrapper controls the drawer through state rather
than id matching, the drawer and toggler can be composed anywhere in the same component.
When you do this, include additional logic to move focus programmatically to the container when it is opened. Otherwise, keyboard users and users of assistive technologies will likely have a hard time finding the newly revealed content—particularly if the container that was opened comes before the toggler in the document's structure.
CSS
Variables
Navbar colors, spacing, and toggler dimensions are defined by CSS variables on .navbar and
.navbar-nav. See the upstream navbar CSS for the complete variable and Sass map reference.
Sass map
Bootstrap reassigns the global $breakpoints map to $navbar-breakpoints in scss/_navbar.scss
and uses this for the loop that generates the responsive navbar expand/collapse classes. This allows
you to customize the breakpoints for navbars independently of the global breakpoints.
Sass loops
Responsive navbar expand/collapse classes (for example, md:navbar-expand) are combined with the
$navbar-breakpoints map and generated through a loop in scss/_navbar.scss.
React behavior
Bootstrap toggles the navbar drawer through the data-bs-toggle="drawer" plugin. The React components
manage open state themselves and expose the same markup as props:
| Component | Prop | Type | Description |
|---|---|---|---|
Navbar | expand | boolean | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | Renders the breakpoint-first navbar-expand class. |
Navbar | theme | 'light' | 'dark' | Sets data-bs-theme for the navbar color scheme. |
NavbarToggler | expanded | boolean | Sets aria-expanded while the drawer is open. |
Drawer | placement | 'start' | 'end' | 'top' | 'bottom' | Chooses the side the drawer slides in from. |
Drawer | onDismiss | () => void | Called by the close button, backdrop, or Esc. |
NavbarDemo and NavbarExternalDemo are documentation-only components that keep the drawer state
for these examples. In your own application, keep the open state in your component or model and
pass it to Drawer and NavbarToggler.