Navbar·续
<ResizableExample className="d-flex flex-column gap-2" showMarkup={false} code={` <nav class="navbar md:navbar-expand" data-bs-theme="dark"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-end" tabindex="-1" id="navbarColor01" aria-labelledby="navbarColor01Label"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarColor01Label">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2 md:mb-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"/> <button class="btn-outline theme-secondary" type="submit">Search</button> </form> </div> </dialog> </div> </nav>
<nav class="navbar md:navbar-expand bg-primary" data-bs-theme="dark"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-end" tabindex="-1" id="navbarColor02" aria-labelledby="navbarColor02Label"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarColor02Label">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2 md:mb-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" data-bs-theme="light" type="search" placeholder="Search" aria-label="Search"/> <button class="btn-solid theme-inverse" type="submit">Search</button> </form> </div> </dialog> </div> </nav>
<nav class="navbar md:navbar-expand bg-1" data-bs-theme="light"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarColor03" aria-controls="navbarColor03" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-end" tabindex="-1" id="navbarColor03" aria-labelledby="navbarColor03Label"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarColor03Label">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2 md:mb-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"/> <button class="btn-outline theme-primary" type="submit">Search</button> </form> </div> </dialog> </div> </nav> `} />
<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.
<nav class="navbar lg:navbar-expand bg-1 fg-2"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> </div> </nav> </div>`} />
Use any of the responsive containers to change how wide the content in your navbar is presented.
<div class="md:container"> <a class="navbar-brand" href="#">Navbar</a> </div> </nav>`} />
Placement
Use our 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 (scrolls with the page until it reaches the bottom, then stays there).
Fixed navbars use position: fixed, meaning they’re pulled from the normal flow of the DOM and may require custom CSS (e.g., padding-top on the <body>) to prevent overlap with other elements.
Responsive behaviors
Navbars can use .navbar-toggler, and .lg:navbar-expand{-sm|-md|-lg|-xl|-2xl} 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 .lg:navbar-expand class on the navbar. For navbars that always show the drawer, don’t add any .lg:navbar-expand class.
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
These examples omit the .lg:navbar-expand class to always show the drawer behavior. Click the toggler to see the drawer slide in from different directions.
<div class="container-fluid"> <a class="navbar-brand" href="#">Left Drawer</a> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarLeftDrawer" aria-controls="navbarLeftDrawer" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-start" tabindex="-1" id="navbarLeftDrawer" aria-labelledby="navbarLeftDrawerLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarLeftDrawerLabel">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2 md:mb-0"> <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"><a class="nav-link disabled" aria-disabled="true">Disabled</a></li> </ul> </div> </dialog> </div> </nav>`} />
<div class="container-fluid"> <a class="navbar-brand" href="#">Top Drawer</a> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarTopDrawer" aria-controls="navbarTopDrawer" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-top" tabindex="-1" id="navbarTopDrawer" aria-labelledby="navbarTopDrawerLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarTopDrawerLabel">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2 md:mb-0"> <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"><a class="nav-link disabled" aria-disabled="true">Disabled</a></li> </ul> </div> </dialog> </div> </nav>`} />
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 .lg: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):
<div class="container-fluid"> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-end" tabindex="-1" id="navbarTogglerDemo01" aria-labelledby="navbarTogglerDemo01Label"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarTogglerDemo01Label">Hidden brand</h5> </div> <div class="drawer-body"> <a class="navbar-brand" href="#">Hidden brand</a> <ul class="navbar-nav me-auto mb-2 md:mb-0"> <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"><a class="nav-link disabled" aria-disabled="true">Disabled</a></li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"/> <button class="btn-outline theme-success" type="submit">Search</button> </form> </div> </dialog> </div> </nav>`} />
With a brand name shown on the left and toggler on the right:
<div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <dialog class="drawer drawer-end" tabindex="-1" id="navbarTogglerDemo02" aria-labelledby="navbarTogglerDemo02Label"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarTogglerDemo02Label">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2"> <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"><a class="nav-link disabled" aria-disabled="true">Disabled</a></li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"/> <button class="btn-outline theme-success" type="submit">Search</button> </form> </div> </dialog> </div> </nav>`} />
With a toggler on the left and brand name on the right:
<div class="container-fluid"> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> <a class="navbar-brand" href="#">Navbar</a> <dialog class="drawer drawer-end" tabindex="-1" id="navbarTogglerDemo03" aria-labelledby="navbarTogglerDemo03Label"> <div class="drawer-header"> <h5 class="drawer-title" id="navbarTogglerDemo03Label">Menu</h5> </div> <div class="drawer-body"> <ul class="navbar-nav me-auto mb-2"> <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"><a class="nav-link disabled" aria-disabled="true">Disabled</a></li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"/> <button class="btn-outline theme-success" type="submit">Search</button> </form> </div> </dialog> </div> </nav>`} />
External content
Sometimes you want to use the drawer plugin to trigger a container element for content that structurally sits outside of the .navbar. Because our plugin works on the id and data-bs-target matching, that’s easily done!
<div class="drawer-header"> <h5 class="drawer-title" id="navbarToggleExternalContentLabel">Collapsed content</h5> </div> <div class="drawer-body"> <span class="fg-2">Toggleable via the navbar toggler.</span> </div> </dialog> <nav class="navbar bg-body" data-bs-theme="dark"> <div class="container-fluid"> <button class="btn-icon navbar-toggler" type="button" data-bs-toggle="drawer" data-bs-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon" aria-hidden="true"></span> </button> </div> </nav>`} />
When you do this, we recommend including additional JavaScript to move the 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. We also recommend making sure that the toggler has the aria-controls attribute, pointing to the id of the content container. In theory, this allows assistive technology users to jump directly from the toggler to the container it controls–but support for this is currently quite patchy.
CSS
Variables
Sass map
We reassign the global $breakpoints map to $navbar-breakpoints in scss/_navbar.scss and use this for our loop to generate 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 (e.g., .lg:navbar-expand) are combined with the $navbar-breakpoints map and generated through a loop in scss/_navbar.scss.
