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} for responsive collapsing and color scheme classes.
  • Navbars and their contents are fluid by default. Use optional containers to limit their horizontal width.
  • 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.
  • Navbars are hidden by default when printing. Force them to be printed by adding .d-print to the .navbar. See the display utility class.
  • 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.

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

Read on for an example and list of supported sub-components.

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.
  • .form-inline 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. <a class="navbar-brand" href="#">Navbar</a>
  3. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarSupportedContent">
  7. <ul class="navbar-nav mr-auto">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Link</a>
  13. </li>
  14. <li class="nav-item dropdown">
  15. <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  16. Dropdown
  17. </a>
  18. <div class="dropdown-menu" aria-labelledby="navbarDropdown">
  19. <a class="dropdown-item" href="#">Action</a>
  20. <a class="dropdown-item" href="#">Another action</a>
  21. <div class="dropdown-divider"></div>
  22. <a class="dropdown-item" href="#">Something else here</a>
  23. </div>
  24. </li>
  25. <li class="nav-item">
  26. <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  27. </li>
  28. </ul>
  29. <form class="form-inline my-2 my-lg-0">
  30. <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  31. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  32. </form>
  33. </div>
  34. </nav>

This example uses color (bg-light) and spacing (my-2, my-lg-0, mr-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. <a class="navbar-brand" href="#">Navbar</a>
  4. </nav>
  5. <!-- As a heading -->
  6. <nav class="navbar navbar-light bg-light">
  7. <span class="navbar-brand mb-0 h1">Navbar</span>
  8. </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. <a class="navbar-brand" href="#">
  4. <img src="/docs/4.4/assets/brand/bootstrap-solid.svg" width="30" height="30" alt="">
  5. </a>
  6. </nav>

Navbar - 图4

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

Navbar - 图5

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  2. <a class="navbar-brand" href="#">Navbar</a>
  3. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarNav">
  7. <ul class="navbar-nav">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Features</a>
  13. </li>
  14. <li class="nav-item">
  15. <a class="nav-link" href="#">Pricing</a>
  16. </li>
  17. <li class="nav-item">
  18. <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  19. </li>
  20. </ul>
  21. </div>
  22. </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. <a class="navbar-brand" href="#">Navbar</a>
  3. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
  7. <div class="navbar-nav">
  8. <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
  9. <a class="nav-item nav-link" href="#">Features</a>
  10. <a class="nav-item nav-link" href="#">Pricing</a>
  11. <a class="nav-item nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  12. </div>
  13. </div>
  14. </nav>

You may also utilize dropdowns in your navbar nav. 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. <a class="navbar-brand" href="#">Navbar</a>
  3. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarNavDropdown">
  7. <ul class="navbar-nav">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Features</a>
  13. </li>
  14. <li class="nav-item">
  15. <a class="nav-link" href="#">Pricing</a>
  16. </li>
  17. <li class="nav-item dropdown">
  18. <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  19. Dropdown link
  20. </a>
  21. <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
  22. <a class="dropdown-item" href="#">Action</a>
  23. <a class="dropdown-item" href="#">Another action</a>
  24. <a class="dropdown-item" href="#">Something else here</a>
  25. </div>
  26. </li>
  27. </ul>
  28. </div>
  29. </nav>

Forms

Place various form controls and components within a navbar with .form-inline.

Navbar - 图8

  1. <nav class="navbar navbar-light bg-light">
  2. <form class="form-inline">
  3. <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  4. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  5. </form>
  6. </nav>

Immediate children elements in .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. <a class="navbar-brand">Navbar</a>
  3. <form class="form-inline">
  4. <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  5. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  6. </form>
  7. </nav>

Input groups work, too:

Navbar - 图10

  1. <nav class="navbar navbar-light bg-light">
  2. <form class="form-inline">
  3. <div class="input-group">
  4. <div class="input-group-prepend">
  5. <span class="input-group-text" id="basic-addon1">@</span>
  6. </div>
  7. <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
  8. </div>
  9. </form>
  10. </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="form-inline">
  3. <button class="btn btn-outline-success" 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. <span class="navbar-text">
  3. Navbar text with an inline element
  4. </span>
  5. </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. <a class="navbar-brand" href="#">Navbar w/ text</a>
  3. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarText">
  7. <ul class="navbar-nav mr-auto">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Features</a>
  13. </li>
  14. <li class="nav-item">
  15. <a class="nav-link" href="#">Pricing</a>
  16. </li>
  17. </ul>
  18. <span class="navbar-text">
  19. Navbar text with an inline element
  20. </span>
  21. </div>
  22. </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 or add one within 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. <a class="navbar-brand" href="#">Navbar</a>
  4. </nav>
  5. </div>

When the container is within your navbar, its horizontal padding is removed at breakpoints lower than your specified .navbar-expand{-sm|-md|-lg|-xl} class. This ensures we’re not doubling up on padding unnecessarily on lower viewports when your navbar is collapsed.

Navbar - 图16

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  2. <div class="container">
  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. <a class="navbar-brand" href="#">Default</a>
  3. </nav>

Navbar - 图18

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

Navbar - 图19

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

Navbar - 图20

  1. <nav class="navbar sticky-top navbar-light bg-light">
  2. <a class="navbar-brand" href="#">Sticky top</a>
  3. </nav>

Responsive behaviors

Navbars can utilize .navbar-toggler, .navbar-collapse, and .navbar-expand{-sm|-md|-lg|-xl} classes to change 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 in lowest breakpoint:

Navbar - 图21

  1. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  2. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
  3. <span class="navbar-toggler-icon"></span>
  4. </button>
  5. <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
  6. <a class="navbar-brand" href="#">Hidden brand</a>
  7. <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Link</a>
  13. </li>
  14. <li class="nav-item">
  15. <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  16. </li>
  17. </ul>
  18. <form class="form-inline my-2 my-lg-0">
  19. <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  20. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  21. </form>
  22. </div>
  23. </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. <a class="navbar-brand" href="#">Navbar</a>
  3. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
  7. <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Link</a>
  13. </li>
  14. <li class="nav-item">
  15. <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  16. </li>
  17. </ul>
  18. <form class="form-inline my-2 my-lg-0">
  19. <input class="form-control mr-sm-2" type="search" placeholder="Search">
  20. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  21. </form>
  22. </div>
  23. </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. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
  3. <span class="navbar-toggler-icon"></span>
  4. </button>
  5. <a class="navbar-brand" href="#">Navbar</a>
  6. <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
  7. <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
  8. <li class="nav-item active">
  9. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  10. </li>
  11. <li class="nav-item">
  12. <a class="nav-link" href="#">Link</a>
  13. </li>
  14. <li class="nav-item">
  15. <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  16. </li>
  17. </ul>
  18. <form class="form-inline my-2 my-lg-0">
  19. <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  20. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  21. </form>
  22. </div>
  23. </nav>

External content

Sometimes you want to use the collapse plugin to trigger hidden content elsewhere on the page. Because our plugin works on the id and data-target matching, that’s easily done!

Navbar - 图24

  1. <div class="pos-f-t">
  2. <div class="collapse" id="navbarToggleExternalContent">
  3. <div class="bg-dark p-4">
  4. <h5 class="text-white h4">Collapsed content</h5>
  5. <span class="text-muted">Toggleable via the navbar brand.</span>
  6. </div>
  7. </div>
  8. <nav class="navbar navbar-dark bg-dark">
  9. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
  10. <span class="navbar-toggler-icon"></span>
  11. </button>
  12. </nav>
  13. </div>