NAVBAR

bootstrap-logo-in-vector

A navigation bar is a navigation header normally placed at the top of the page:

navbar

With Bootstrap, a navigation bar can stretch or collapse, depending on the size of the screen. A standard navigation bar with the class .navbar is created, followed by a responsive compression class: .navbar-expand-xxl | xl | lg | md | sm (stacks the navigation bar vertically on xxlarge, extra large, large screens , medium or small). To add links within the navigation bar, use a <ul> element (or a <div>) with class = “navbar-nav“. Then add <li> elements with a .nav-item class followed by an <a> element with a .nav-link class:

EXAMPLE OF NAVBAR BASIC CODE

Copy to Clipboard

VERTICAL NAVBAR

Remove the .navbar-expand- * class to create a navigation bar that will always be vertical:

<!– A grey vertical navbar –>
<nav class=”navbar bg-light>
  …
</nav>

CENTER A NAVBAR

Add the .justify-content-center class to center the navigation bar:

<nav class=”navbar navbar-expand-sm bg-light justify-content-center>
  …
</nav>

COLORS OF THE NAVBAR

colors navbar

Use one of the .bg-color classes to change the background color of the navigation bar (.bg-primary, .bg-success, .bg-info, .bg-warning, .bg-danger, .bg-secondary,. bg -dark and .bg-light). Tip: Add a white text color to all links in the navigation bar with the .navbar-dark class or use the .navbar-light class to add a black text color.

CODE EXAMPLE

Copy to Clipboard

BRAND / LOGO

When using the .navbar-brand class with images, Bootstrap 5 will automatically shape the image to fit vertically on the navigation bar.

CODE EXAMPLE

Copy to Clipboard

NAVBAR TEXT

Use the .navbar-text class to vertically align all elements within the navigation bar that are not links (ensures correct padding and text color).

<nav class=”navbar navbar-expand-sm bg-dark navbar-dark>
  <div class=”container-fluid>
    <span class=”navbar-text“>Navbar text</span>
  </div>
</nav>

Quite often, especially on small screens, you want to hide navigation links and replace them with a button that should reveal them when clicked. To create a collapsible navigation bar, use a button with class = “navbar-toggler”, data-bs-toggle = “collapse” and data-bs-target = “# thetarget”. Then wrap the contents of the navigation bar (links, etc.) inside a <div> element with class = “collapse navbar-collapse“, followed by an id that corresponds to the button’s data-bs-target: “thetarget “.

Copy to Clipboard

NAVBAR WITH DROP-DOWN

CODE EXAMPLE

Copy to Clipboard

NAVBAR FORM AND BUTTON

CODE

Copy to Clipboard

FIXED NAVIGATION BAR

The navigation bar can also be pinned to the top or bottom of the page. A fixed navigation bar remains visible in a fixed position (top or bottom) regardless of page scrolling. The .fixed-top class makes the navigation bar fixed at the top:

<nav class=”navbar navbar-expand-sm bg-dark navbar-dark fixed-top>
  …
</nav>

Use the .fixed-bottom class to make the navigation bar stay at the bottom of the page:

<nav class=”navbar navbar-expand-sm bg-dark navbar-dark fixed-bottom>
  …
</nav>

Use the .sticky-top class to pin the navigation bar/stay at the top of the page when you scroll past it. Note: this class does not work in IE11 and earlier (it will treat it as position: relative).

<nav class=”navbar navbar-expand-sm bg-dark navbar-dark sticky-top>
  …
</nav>

LINKS TO PREVIOUS POST

BOOTSTRAP 5

LINK TO THE CODE ON GITHUB

GITHUB