Navbar

Documentation and examples for Bootstrap’s powerful, responsive navigation header, the navbar. Includes support for branding, navigation, and more, including support for our collapse plugin.

How it works

Here’s what you need to know before getting started with the navbar:

  • Navbars require a wrapping .navbar with .navbar-expand{-sm|-md|-lg|-xl|-xxl} for responsive collapsing and color scheme classes.
  • Navbars and their contents are fluid by default. Change the container to limit their horizontal width in different ways.
  • Use our spacing and flex utility classes for controlling spacing and alignment within navbars.
  • Navbars are responsive by default, but you can easily modify them to change that. Responsive behavior depends on our Collapse JavaScript plugin.
  • Ensure accessibility by using a <nav> element or, if using a more generic element such as a <div>, add a role="navigation" to every navbar to explicitly identify it as a landmark region for users of assistive technologies.
  • Indicate the current item by using aria-current="page" for the current page or aria-current="true" for the current item in a set.
  • New in v5.2.0: Navbars can be themed with CSS variables that are scoped to the .navbar base class. .navbar-light has been deprecated and .navbar-dark has been rewritten to override CSS variables instead of adding additional styles.

The animation effect of this component is dependent on the prefers-reduced-motion media query. See the reduced motion section of our accessibility documentation.

Supported content

Navbars come with built-in support for a handful of sub-components. Choose from the following as needed:

  • .navbar-brand for your company, product, or project name.
  • .navbar-nav for a full-height and lightweight navigation (including support for dropdowns).
  • .navbar-toggler for use with our collapse plugin and other navigation toggling behaviors.
  • Flex and spacing utilities for any form controls and actions.
  • .navbar-text for adding vertically centered strings of text.
  • .collapse.navbar-collapse for grouping and hiding navbar contents by a parent breakpoint.
  • Add an optional .navbar-scroll to set a max-height and scroll expanded navbar content.

Here’s an example of all the sub-components included in a responsive light-themed navbar that automatically collapses at the lg (large) breakpoint.

Navbar - 图1

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarSupportedContent">
  8. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Link</a>
  14. </li>
  15. <li class="nav-item dropdown">
  16. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  17. Dropdown
  18. </a>
  19. <ul class="dropdown-menu">
  20. <li><a class="dropdown-item" href="#">Action</a></li>
  21. <li><a class="dropdown-item" href="#">Another action</a></li>
  22. <li><hr class="dropdown-divider"></li>
  23. <li><a class="dropdown-item" href="#">Something else here</a></li>
  24. </ul>
  25. </li>
  26. <li class="nav-item">
  27. <a class="nav-link disabled" aria-disabled="true">Disabled</a>
  28. </li>
  29. </ul>
  30. <form class="d-flex" role="search">
  31. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  32. <button class="btn btn-outline-success" type="submit">Search</button>
  33. </form>
  34. </div>
  35. </div>
  36. </nav>

This example uses background (bg-body-tertiary) and spacing (me-auto, mb-2, mb-lg-0, me-2) utility classes.

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.

Text

Add your text within an element with the .navbar-brand class.

Navbar - 图2

html

  1. <!-- As a link -->
  2. <nav class="navbar bg-body-tertiary">
  3. <div class="container-fluid">
  4. <a class="navbar-brand" href="#">Navbar</a>
  5. </div>
  6. </nav>
  7. <!-- As a heading -->
  8. <nav class="navbar bg-body-tertiary">
  9. <div class="container-fluid">
  10. <span class="navbar-brand mb-0 h1">Navbar</span>
  11. </div>
  12. </nav>

Image

You can replace the text within the .navbar-brand with an <img>.

Navbar - 图3

html

  1. <nav class="navbar bg-body-tertiary">
  2. <div class="container">
  3. <a class="navbar-brand" href="#">
  4. <img src="/docs/5.3/assets/brand/bootstrap-logo.svg" alt="Bootstrap" width="30" height="24">
  5. </a>
  6. </div>
  7. </nav>

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 <img>.

Navbar - 图4

html

  1. <nav class="navbar bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">
  4. <img src="/docs/5.3/assets/brand/bootstrap-logo.svg" alt="Logo" width="30" height="24" class="d-inline-block align-text-top">
  5. Bootstrap
  6. </a>
  7. </div>
  8. </nav>

Nav

Navbar navigation links build on our .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.

Navbar - 图5

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarNav">
  8. <ul class="navbar-nav">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Features</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link" href="#">Pricing</a>
  17. </li>
  18. <li class="nav-item">
  19. <a class="nav-link disabled" aria-disabled="true">Disabled</a>
  20. </li>
  21. </ul>
  22. </div>
  23. </div>
  24. </nav>

And because we use classes for our navs, you can avoid the list-based approach entirely if you like.

Navbar - 图6

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
  8. <div class="navbar-nav">
  9. <a class="nav-link active" aria-current="page" href="#">Home</a>
  10. <a class="nav-link" href="#">Features</a>
  11. <a class="nav-link" href="#">Pricing</a>
  12. <a class="nav-link disabled" aria-disabled="true">Disabled</a>
  13. </div>
  14. </div>
  15. </div>
  16. </nav>

You can also use dropdowns in your navbar. Dropdown 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.

Navbar - 图7

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarNavDropdown">
  8. <ul class="navbar-nav">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Features</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link" href="#">Pricing</a>
  17. </li>
  18. <li class="nav-item dropdown">
  19. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  20. Dropdown link
  21. </a>
  22. <ul class="dropdown-menu">
  23. <li><a class="dropdown-item" href="#">Action</a></li>
  24. <li><a class="dropdown-item" href="#">Another action</a></li>
  25. <li><a class="dropdown-item" href="#">Something else here</a></li>
  26. </ul>
  27. </li>
  28. </ul>
  29. </div>
  30. </div>
  31. </nav>

Forms

Place various form controls and components within a navbar:

Navbar - 图8

html

  1. <nav class="navbar bg-body-tertiary">
  2. <div class="container-fluid">
  3. <form class="d-flex" role="search">
  4. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  5. <button class="btn btn-outline-success" type="submit">Search</button>
  6. </form>
  7. </div>
  8. </nav>

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.

Navbar - 图9

html

  1. <nav class="navbar bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand">Navbar</a>
  4. <form class="d-flex" role="search">
  5. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  6. <button class="btn btn-outline-success" type="submit">Search</button>
  7. </form>
  8. </div>
  9. </nav>

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 HTML.

Navbar - 图10

html

  1. <nav class="navbar bg-body-tertiary">
  2. <form class="container-fluid">
  3. <div class="input-group">
  4. <span class="input-group-text" id="basic-addon1">@</span>
  5. <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
  6. </div>
  7. </form>
  8. </nav>

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.

Navbar - 图11

html

  1. <nav class="navbar bg-body-tertiary">
  2. <form class="container-fluid justify-content-start">
  3. <button class="btn btn-outline-success me-2" type="button">Main button</button>
  4. <button class="btn btn-sm btn-outline-secondary" type="button">Smaller button</button>
  5. </form>
  6. </nav>

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.

Navbar - 图12

html

  1. <nav class="navbar bg-body-tertiary">
  2. <div class="container-fluid">
  3. <span class="navbar-text">
  4. Navbar text with an inline element
  5. </span>
  6. </div>
  7. </nav>

Mix and match with other components and utilities as needed.

Navbar - 图13

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar w/ text</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarText">
  8. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Features</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link" href="#">Pricing</a>
  17. </li>
  18. </ul>
  19. <span class="navbar-text">
  20. Navbar text with an inline element
  21. </span>
  22. </div>
  23. </div>
  24. </nav>

Color schemes

New dark navbars in v5.3.0 — We’ve deprecated .navbar-dark in favor of the new data-bs-theme="dark". Add data-bs-theme="dark" to the .navbar to enable a component-specific color mode. Learn more about our color modes.


New in v5.2.0 — Navbar theming is now powered by CSS variables and .navbar-light has been deprecated. CSS variables are applied to .navbar, defaulting to the “light” appearance, and can be overridden with .navbar-dark.

Navbar themes are easier than ever thanks to Bootstrap’s combination of Sass and CSS variables. The default is our “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.

Navbar - 图14

  1. <nav class="navbar bg-dark border-bottom border-body" data-bs-theme="dark">
  2. <!-- Navbar content -->
  3. </nav>
  4. <nav class="navbar bg-primary" data-bs-theme="dark">
  5. <!-- Navbar content -->
  6. </nav>
  7. <nav class="navbar" style="background-color: #e3f2fd;">
  8. <!-- Navbar content -->
  9. </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.

Navbar - 图15

html

  1. <div class="container">
  2. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  3. <div class="container-fluid">
  4. <a class="navbar-brand" href="#">Navbar</a>
  5. </div>
  6. </nav>
  7. </div>

Use any of the responsive containers to change how wide the content in your navbar is presented.

Navbar - 图16

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-md">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. </div>
  5. </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.

Navbar - 图17

html

  1. <nav class="navbar bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Default</a>
  4. </div>
  5. </nav>

Navbar - 图18

html

  1. <nav class="navbar fixed-top bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Fixed top</a>
  4. </div>
  5. </nav>

Navbar - 图19

html

  1. <nav class="navbar fixed-bottom bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Fixed bottom</a>
  4. </div>
  5. </nav>

Navbar - 图20

html

  1. <nav class="navbar sticky-top bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Sticky top</a>
  4. </div>
  5. </nav>

Navbar - 图21

html

  1. <nav class="navbar sticky-bottom bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Sticky bottom</a>
  4. </div>
  5. </nav>

Scrolling

Add .navbar-nav-scroll to a .navbar-nav (or other navbar sub-component) to enable vertical scrolling within the toggleable contents of a collapsed navbar. By default, scrolling kicks in at 75vh (or 75% of the viewport height), but you can override that with the local CSS custom property --bs-navbar-height or custom styles. At larger viewports when the navbar is expanded, content will appear as it does in a default navbar.

Please note that this behavior comes with a potential drawback of overflow—when setting overflow-y: auto (required to scroll the content here), overflow-x is the equivalent of auto, which will crop some horizontal content.

Here’s an example navbar using .navbar-nav-scroll with style="--bs-scroll-height: 100px;", with some extra margin utilities for optimum spacing.

Navbar - 图22

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar scroll</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarScroll" aria-controls="navbarScroll" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarScroll">
  8. <ul class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll" style="--bs-scroll-height: 100px;">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Link</a>
  14. </li>
  15. <li class="nav-item dropdown">
  16. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  17. Link
  18. </a>
  19. <ul class="dropdown-menu">
  20. <li><a class="dropdown-item" href="#">Action</a></li>
  21. <li><a class="dropdown-item" href="#">Another action</a></li>
  22. <li><hr class="dropdown-divider"></li>
  23. <li><a class="dropdown-item" href="#">Something else here</a></li>
  24. </ul>
  25. </li>
  26. <li class="nav-item">
  27. <a class="nav-link disabled" aria-disabled="true">Link</a>
  28. </li>
  29. </ul>
  30. <form class="d-flex" role="search">
  31. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  32. <button class="btn btn-outline-success" type="submit">Search</button>
  33. </form>
  34. </div>
  35. </div>
  36. </nav>

Responsive behaviors

Navbars can use .navbar-toggler, .navbar-collapse, and .navbar-expand{-sm|-md|-lg|-xl|-xxl} classes to determine when their content collapses behind a button. 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. For navbars that always collapse, don’t add any .navbar-expand class.

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. Below are examples of different toggle styles.

With no .navbar-brand shown at the smallest breakpoint:

Navbar - 图23

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
  7. <a class="navbar-brand" href="#">Hidden brand</a>
  8. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Link</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link disabled" aria-disabled="true">Disabled</a>
  17. </li>
  18. </ul>
  19. <form class="d-flex" role="search">
  20. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  21. <button class="btn btn-outline-success" type="submit">Search</button>
  22. </form>
  23. </div>
  24. </div>
  25. </nav>

With a brand name shown on the left and toggler on the right:

Navbar - 图24

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
  8. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Link</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link disabled" aria-disabled="true">Disabled</a>
  17. </li>
  18. </ul>
  19. <form class="d-flex" role="search">
  20. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  21. <button class="btn btn-outline-success" type="submit">Search</button>
  22. </form>
  23. </div>
  24. </div>
  25. </nav>

With a toggler on the left and brand name on the right:

Navbar - 图25

html

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary">
  2. <div class="container-fluid">
  3. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <a class="navbar-brand" href="#">Navbar</a>
  7. <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
  8. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  9. <li class="nav-item">
  10. <a class="nav-link active" aria-current="page" href="#">Home</a>
  11. </li>
  12. <li class="nav-item">
  13. <a class="nav-link" href="#">Link</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link disabled" aria-disabled="true">Disabled</a>
  17. </li>
  18. </ul>
  19. <form class="d-flex" role="search">
  20. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  21. <button class="btn btn-outline-success" type="submit">Search</button>
  22. </form>
  23. </div>
  24. </div>
  25. </nav>

External content

Sometimes you want to use the collapse 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!

Navbar - 图26

html

  1. <div class="collapse" id="navbarToggleExternalContent" data-bs-theme="dark">
  2. <div class="bg-dark p-4">
  3. <h5 class="text-body-emphasis h4">Collapsed content</h5>
  4. <span class="text-body-secondary">Toggleable via the navbar brand.</span>
  5. </div>
  6. </div>
  7. <nav class="navbar navbar-dark bg-dark">
  8. <div class="container-fluid">
  9. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
  10. <span class="navbar-toggler-icon"></span>
  11. </button>
  12. </div>
  13. </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.

Offcanvas

Transform your expanding and collapsing navbar into an offcanvas drawer with the offcanvas component. We extend both the offcanvas default styles and use our .navbar-expand-* classes to create a dynamic and flexible navigation sidebar.

In the example below, to create an offcanvas navbar that is always collapsed across all breakpoints, omit the .navbar-expand-* class entirely.

Navbar - 图27

html

  1. <nav class="navbar bg-body-tertiary fixed-top">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Offcanvas navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasNavbar" aria-labelledby="offcanvasNavbarLabel">
  8. <div class="offcanvas-header">
  9. <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Offcanvas</h5>
  10. <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  11. </div>
  12. <div class="offcanvas-body">
  13. <ul class="navbar-nav justify-content-end flex-grow-1 pe-3">
  14. <li class="nav-item">
  15. <a class="nav-link active" aria-current="page" href="#">Home</a>
  16. </li>
  17. <li class="nav-item">
  18. <a class="nav-link" href="#">Link</a>
  19. </li>
  20. <li class="nav-item dropdown">
  21. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  22. Dropdown
  23. </a>
  24. <ul class="dropdown-menu">
  25. <li><a class="dropdown-item" href="#">Action</a></li>
  26. <li><a class="dropdown-item" href="#">Another action</a></li>
  27. <li>
  28. <hr class="dropdown-divider">
  29. </li>
  30. <li><a class="dropdown-item" href="#">Something else here</a></li>
  31. </ul>
  32. </li>
  33. </ul>
  34. <form class="d-flex mt-3" role="search">
  35. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  36. <button class="btn btn-outline-success" type="submit">Search</button>
  37. </form>
  38. </div>
  39. </div>
  40. </div>
  41. </nav>

To create an offcanvas navbar that expands into a normal navbar at a specific breakpoint like lg, use .navbar-expand-lg.

  1. <nav class="navbar navbar-expand-lg bg-body-tertiary fixed-top">
  2. <a class="navbar-brand" href="#">Offcanvas navbar</a>
  3. <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#navbarOffcanvasLg" aria-controls="navbarOffcanvasLg" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="offcanvas offcanvas-end" tabindex="-1" id="navbarOffcanvasLg" aria-labelledby="navbarOffcanvasLgLabel">
  7. ...
  8. </div>
  9. </nav>

When using offcanvas in a dark navbar, be aware that you may need to have a dark background on the offcanvas content to avoid the text becoming illegible. In the example below, we add .navbar-dark and .bg-dark to the .navbar, .text-bg-dark to the .offcanvas, .dropdown-menu-dark to .dropdown-menu, and .btn-close-white to .btn-close for proper styling with a dark offcanvas.

Navbar - 图28

html

  1. <nav class="navbar navbar-dark bg-dark fixed-top">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Offcanvas dark navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasDarkNavbar" aria-controls="offcanvasDarkNavbar" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="offcanvas offcanvas-end text-bg-dark" tabindex="-1" id="offcanvasDarkNavbar" aria-labelledby="offcanvasDarkNavbarLabel">
  8. <div class="offcanvas-header">
  9. <h5 class="offcanvas-title" id="offcanvasDarkNavbarLabel">Dark offcanvas</h5>
  10. <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  11. </div>
  12. <div class="offcanvas-body">
  13. <ul class="navbar-nav justify-content-end flex-grow-1 pe-3">
  14. <li class="nav-item">
  15. <a class="nav-link active" aria-current="page" href="#">Home</a>
  16. </li>
  17. <li class="nav-item">
  18. <a class="nav-link" href="#">Link</a>
  19. </li>
  20. <li class="nav-item dropdown">
  21. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  22. Dropdown
  23. </a>
  24. <ul class="dropdown-menu dropdown-menu-dark">
  25. <li><a class="dropdown-item" href="#">Action</a></li>
  26. <li><a class="dropdown-item" href="#">Another action</a></li>
  27. <li>
  28. <hr class="dropdown-divider">
  29. </li>
  30. <li><a class="dropdown-item" href="#">Something else here</a></li>
  31. </ul>
  32. </li>
  33. </ul>
  34. <form class="d-flex mt-3" role="search">
  35. <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  36. <button class="btn btn-success" type="submit">Search</button>
  37. </form>
  38. </div>
  39. </div>
  40. </div>
  41. </nav>

CSS

Variables

Added in v5.2.0

As part of Bootstrap’s evolving CSS variables approach, navbars now use local CSS variables on .navbar for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

scss/_navbar.scss

  1. --#{$prefix}navbar-padding-x: #{if($navbar-padding-x == null, 0, $navbar-padding-x)};
  2. --#{$prefix}navbar-padding-y: #{$navbar-padding-y};
  3. --#{$prefix}navbar-color: #{$navbar-light-color};
  4. --#{$prefix}navbar-hover-color: #{$navbar-light-hover-color};
  5. --#{$prefix}navbar-disabled-color: #{$navbar-light-disabled-color};
  6. --#{$prefix}navbar-active-color: #{$navbar-light-active-color};
  7. --#{$prefix}navbar-brand-padding-y: #{$navbar-brand-padding-y};
  8. --#{$prefix}navbar-brand-margin-end: #{$navbar-brand-margin-end};
  9. --#{$prefix}navbar-brand-font-size: #{$navbar-brand-font-size};
  10. --#{$prefix}navbar-brand-color: #{$navbar-light-brand-color};
  11. --#{$prefix}navbar-brand-hover-color: #{$navbar-light-brand-hover-color};
  12. --#{$prefix}navbar-nav-link-padding-x: #{$navbar-nav-link-padding-x};
  13. --#{$prefix}navbar-toggler-padding-y: #{$navbar-toggler-padding-y};
  14. --#{$prefix}navbar-toggler-padding-x: #{$navbar-toggler-padding-x};
  15. --#{$prefix}navbar-toggler-font-size: #{$navbar-toggler-font-size};
  16. --#{$prefix}navbar-toggler-icon-bg: #{escape-svg($navbar-light-toggler-icon-bg)};
  17. --#{$prefix}navbar-toggler-border-color: #{$navbar-light-toggler-border-color};
  18. --#{$prefix}navbar-toggler-border-radius: #{$navbar-toggler-border-radius};
  19. --#{$prefix}navbar-toggler-focus-width: #{$navbar-toggler-focus-width};
  20. --#{$prefix}navbar-toggler-transition: #{$navbar-toggler-transition};

Some additional CSS variables are also present on .navbar-nav:

scss/_navbar.scss

  1. --#{$prefix}nav-link-padding-x: 0;
  2. --#{$prefix}nav-link-padding-y: #{$nav-link-padding-y};
  3. @include rfs($nav-link-font-size, --#{$prefix}nav-link-font-size);
  4. --#{$prefix}nav-link-font-weight: #{$nav-link-font-weight};
  5. --#{$prefix}nav-link-color: var(--#{$prefix}navbar-color);
  6. --#{$prefix}nav-link-hover-color: var(--#{$prefix}navbar-hover-color);
  7. --#{$prefix}nav-link-disabled-color: var(--#{$prefix}navbar-disabled-color);

Customization through CSS variables can be seen on the .navbar-dark class where we override specific values without adding duplicate CSS selectors.

scss/_navbar.scss

  1. --#{$prefix}navbar-color: #{$navbar-dark-color};
  2. --#{$prefix}navbar-hover-color: #{$navbar-dark-hover-color};
  3. --#{$prefix}navbar-disabled-color: #{$navbar-dark-disabled-color};
  4. --#{$prefix}navbar-active-color: #{$navbar-dark-active-color};
  5. --#{$prefix}navbar-brand-color: #{$navbar-dark-brand-color};
  6. --#{$prefix}navbar-brand-hover-color: #{$navbar-dark-brand-hover-color};
  7. --#{$prefix}navbar-toggler-border-color: #{$navbar-dark-toggler-border-color};
  8. --#{$prefix}navbar-toggler-icon-bg: #{escape-svg($navbar-dark-toggler-icon-bg)};

Sass variables

Variables for all navbars:

scss/_variables.scss

  1. $navbar-padding-y: $spacer * .5;
  2. $navbar-padding-x: null;
  3. $navbar-nav-link-padding-x: .5rem;
  4. $navbar-brand-font-size: $font-size-lg;
  5. // Compute the navbar-brand padding-y so the navbar-brand will have the same height as navbar-text and nav-link
  6. $nav-link-height: $font-size-base * $line-height-base + $nav-link-padding-y * 2;
  7. $navbar-brand-height: $navbar-brand-font-size * $line-height-base;
  8. $navbar-brand-padding-y: ($nav-link-height - $navbar-brand-height) * .5;
  9. $navbar-brand-margin-end: 1rem;
  10. $navbar-toggler-padding-y: .25rem;
  11. $navbar-toggler-padding-x: .75rem;
  12. $navbar-toggler-font-size: $font-size-lg;
  13. $navbar-toggler-border-radius: $btn-border-radius;
  14. $navbar-toggler-focus-width: $btn-focus-width;
  15. $navbar-toggler-transition: box-shadow .15s ease-in-out;
  16. $navbar-light-color: rgba(var(--#{$prefix}emphasis-color-rgb), .65);
  17. $navbar-light-hover-color: rgba(var(--#{$prefix}emphasis-color-rgb), .8);
  18. $navbar-light-active-color: rgba(var(--#{$prefix}emphasis-color-rgb), 1);
  19. $navbar-light-disabled-color: rgba(var(--#{$prefix}emphasis-color-rgb), .3);
  20. $navbar-light-icon-color: rgba($body-color, .75);
  21. $navbar-light-toggler-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke='#{$navbar-light-icon-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
  22. $navbar-light-toggler-border-color: rgba(var(--#{$prefix}emphasis-color-rgb), .15);
  23. $navbar-light-brand-color: $navbar-light-active-color;
  24. $navbar-light-brand-hover-color: $navbar-light-active-color;

Variables for the dark navbar:

scss/_variables.scss

  1. $navbar-dark-color: rgba($white, .55);
  2. $navbar-dark-hover-color: rgba($white, .75);
  3. $navbar-dark-active-color: $white;
  4. $navbar-dark-disabled-color: rgba($white, .25);
  5. $navbar-dark-icon-color: $navbar-dark-color;
  6. $navbar-dark-toggler-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke='#{$navbar-dark-icon-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
  7. $navbar-dark-toggler-border-color: rgba($white, .1);
  8. $navbar-dark-brand-color: $navbar-dark-active-color;
  9. $navbar-dark-brand-hover-color: $navbar-dark-active-color;

Sass loops

Responsive navbar expand/collapse classes (e.g., .navbar-expand-lg) are combined with the $breakpoints map and generated through a loop in scss/_navbar.scss.

scss/_navbar.scss

  1. // Generate series of `.navbar-expand-*` responsive classes for configuring
  2. // where your navbar collapses.
  3. .navbar-expand {
  4. @each $breakpoint in map-keys($grid-breakpoints) {
  5. $next: breakpoint-next($breakpoint, $grid-breakpoints);
  6. $infix: breakpoint-infix($next, $grid-breakpoints);
  7. // stylelint-disable-next-line scss/selector-no-union-class-name
  8. &#{$infix} {
  9. @include media-breakpoint-up($next) {
  10. flex-wrap: nowrap;
  11. justify-content: flex-start;
  12. .navbar-nav {
  13. flex-direction: row;
  14. .dropdown-menu {
  15. position: absolute;
  16. }
  17. .nav-link {
  18. padding-right: var(--#{$prefix}navbar-nav-link-padding-x);
  19. padding-left: var(--#{$prefix}navbar-nav-link-padding-x);
  20. }
  21. }
  22. .navbar-nav-scroll {
  23. overflow: visible;
  24. }
  25. .navbar-collapse {
  26. display: flex !important; // stylelint-disable-line declaration-no-important
  27. flex-basis: auto;
  28. }
  29. .navbar-toggler {
  30. display: none;
  31. }
  32. .offcanvas {
  33. // stylelint-disable declaration-no-important
  34. position: static;
  35. z-index: auto;
  36. flex-grow: 1;
  37. width: auto !important;
  38. height: auto !important;
  39. visibility: visible !important;
  40. background-color: transparent !important;
  41. border: 0 !important;
  42. transform: none !important;
  43. @include box-shadow(none);
  44. @include transition(none);
  45. // stylelint-enable declaration-no-important
  46. .offcanvas-header {
  47. display: none;
  48. }
  49. .offcanvas-body {
  50. display: flex;
  51. flex-grow: 0;
  52. padding: 0;
  53. overflow-y: visible;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }