Bootstrap 4 Collapsing Navbar

Progressively collapse menu items into a dropdown menu

Tom Michew
WDstack
3 min readFeb 7, 2019

--

Bootstrap 4 has a nice responsive Navbar with 5 different breakpoint options. However, when there are many menu items, none of the responsive breakpoints are ideal, and the Navbar items start to wrap which doesn’t look good.

Here is an alternative responsive Navbar that automatically collapses (as the viewport is resized) the extra overflowing menu items into a Dropdown menu.

This implementation is with jQuery, but this JavaScript logic could just as easily be implemented as a Vue, React or Angular component.

Here’s the basic logic:

  • If the Navbar height is larger than it’s container, the menu items have wrapped and therefore we ADD items to the dropdown
  • Otherwise, we REMOVE items from the dropdown and return them to the Navbar
  • We check the above logic when the DOM is loaded, and while the window is resized.

The jQuery Function

The HTML Markup

And the Navbar markup is the standard Bootstrap 4 responsive Navbar with all of the Navbar Items and a Dropdown on the right.

Putting it all together, here is the complete Navbar:

Grab the source code at Codeply.

Thanks for reading, and check out the other helpful Bootstrap development resources at Bootstrap4.guide

-T

--

--

Tom Michew
WDstack

Technical marketer, developer, startup guy #growthhacker @wdstack @codeplyapp