-1

There are a number of questions for partially collapsing Bootstrap navbars, e.g.:

Item1  Item2  Item3  Item4  Item5

Can be collapsed to:

Item1  Item2  Item3  =menu=

Or:

=menu=  Item3  Item4  Item5

And so on. That works by having two navbars where one collapses and one does not.

But I want to collapse a single navbar where the items to hide are not at the edges:

Item1  Item3  Item5  =menu=

Can that be done with Bootstrap 5?

6
  • Why not use one of the classes for hiding elements on different screen sizes?
    – kmoser
    Commented May 25, 2022 at 1:24
  • @kmoser Thanks for the nice workaround. That's what I'm doing currently - it works, but those nav-items are completely hidden. I was hoping there were some nice UX to collapse some items, but show all when the hamburger menu is toggled.
    – lonix
    Commented May 25, 2022 at 1:27
  • Please see How to Ask. You need to show your use case in code.
    – isherwood
    Commented May 25, 2022 at 20:54
  • Alright thanks dude. I've been using it since version minus 1. I'll wait for another answer. Besides, I don't think this use case is possible, but I hope I'm wrong.
    – lonix
    Commented May 26, 2022 at 2:12
  • If the nav items are descendants of the collapsed (hidden) element, you may be able to selectively hide/show some of them based on whether their ancestor is hidden or not. See Is there a selector to exclude display: none elements?
    – kmoser
    Commented May 26, 2022 at 5:03

2 Answers 2

0

There is no Bootstrap native solution. But there is a workaround using two identical navbars.

navbar 1:

  • has d-none d-md-block (for example) for nav items that should be hidden on small screens
  • on large screens it shows all nav items
  • it does not have a toggle button (or alternatively, it does, but the "external content" is the second navbar)

navbar 2:

  • has the same navbar items, and a toggle button
  • is always in a collapsed state
  • on small screens it shows the toggle button; when clicked it shows a dropdown with all nav items

e.g. large screen:

Item1  Item2  Item3  Item4  Item5

e.g. small screen:

Item1  Item3  Item5  =menu=
                     Item1
                     Item2
                     Item3
                     Item4
                     Item5

or e.g. small screen:

=menu=  Item1  Item3  Item5
Item1
Item2
Item3
Item4
Item5
0

First of all its hard to tell the exact solution without seeing the code. If the number of menus are limited then you can do it with media queries (apply style to all navbar options except first 2 and last one. Another way is with javascript, (can make a condition according to screen size to trigger event). It will be much easier if you share the piece of code you having trouble with.

1
  • That wouldn't solve the problem - it would just hide some nav-items. There is no native way to show them in a collapsible dropdown.
    – lonix
    Commented May 26, 2022 at 6:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.