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.

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.

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

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  17. Dropdown
  18. </a>
  19. <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
  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" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  28. </li>
  29. </ul>
  30. <form class="d-flex">
  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 color (bg-light) and spacing (my-2, my-lg-0, me-sm-0, my-sm-0) 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.

Navbar - 图2

  1. <!-- As a link -->
  2. <nav class="navbar navbar-light bg-light">
  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 navbar-light bg-light">
  9. <div class="container-fluid">
  10. <span class="navbar-brand mb-0 h1">Navbar</span>
  11. </div>
  12. </nav>

Adding images to the .navbar-brand will likely always require custom styles or utilities to properly size. Here are some examples to demonstrate.

Navbar - 图3

  1. <!-- Just an image -->
  2. <nav class="navbar navbar-light bg-light">
  3. <div class="container">
  4. <a class="navbar-brand" href="#">
  5. <img src="/docs/5.0/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24">
  6. </a>
  7. </div>
  8. </nav>

Navbar - 图4

  1. <!-- Image and text -->
  2. <nav class="navbar navbar-light bg-light">
  3. <div class="container-fluid">
  4. <a class="navbar-brand" href="#">
  5. <img src="/docs/5.0/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24" class="d-inline-block align-top">
  6. Bootstrap
  7. </a>
  8. </div>
  9. </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.

Active states—with .active—to indicate the current page can be applied directly to .nav-links or their immediate parent .nav-items.

Please note that you should also add the aria-current attribute on the .nav-link itself.

Navbar - 图5

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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" href="#" tabindex="-1" 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

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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" href="#" tabindex="-1" 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

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  20. Dropdown link
  21. </a>
  22. <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
  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

  1. <nav class="navbar navbar-light bg-light">
  2. <div class="container-fluid">
  3. <form class="d-flex">
  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

  1. <nav class="navbar navbar-light bg-light">
  2. <div class="container-fluid">
  3. <a class="navbar-brand">Navbar</a>
  4. <form class="d-flex">
  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

  1. <nav class="navbar navbar-light bg-light">
  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

  1. <nav class="navbar navbar-light bg-light">
  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

  1. <nav class="navbar navbar-light bg-light">
  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

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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

Theming the navbar has never been easier thanks to the combination of theming classes and background-color utilities. Choose from .navbar-light for use with light background colors, or .navbar-dark for dark background colors. Then, customize with .bg-* utilities.

Navbar - 图14

  1. <nav class="navbar navbar-dark bg-dark">
  2. <!-- Navbar content -->
  3. </nav>
  4. <nav class="navbar navbar-dark bg-primary">
  5. <!-- Navbar content -->
  6. </nav>
  7. <nav class="navbar navbar-light" 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

  1. <div class="container">
  2. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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, or stickied to the top (scrolls with the page until it reaches the top, 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.

Also note that .sticky-top uses position: sticky, which isn’t fully supported in every browser.

Navbar - 图17

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

Navbar - 图18

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

Navbar - 图19

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

Navbar - 图20

  1. <nav class="navbar sticky-top navbar-light bg-light">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Sticky top</a>
  4. </div>
  5. </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 - 图21

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  17. </li>
  18. </ul>
  19. <form class="d-flex">
  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 - 图22

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  17. </li>
  18. </ul>
  19. <form class="d-flex">
  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 - 图23

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  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" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  17. </li>
  18. </ul>
  19. <form class="d-flex">
  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 - 图24

  1. <div class="collapse" id="navbarToggleExternalContent">
  2. <div class="bg-dark p-4">
  3. <h5 class="text-white h4">Collapsed content</h5>
  4. <span class="text-muted">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.