Unsure what category to put this in. I’m currently trying to create a navigation similar to the images below. You click the parent element, then the child list appears. I’ve got that working but in CSS is it possible to simply go as this parent element has been activated with the .expanded element, lets hide the other parent elements so it’s just the child and back button element displayed?
It’s not really possible in CSS without at least adding a class to the parent wrapper via JS when a menu is active. You could then use that class on the main parent (i.e. bignav.active) to hide all the elements except for the ones that you just expanded.
It would need a little thought though as you have nested menus within sub menus which means do you want the child nested menu to be the only one visible (thus hiding the previous sub menu). For example the clients menu is a child of the services menu.
Would you hide the services menu when the clients menu is open? Would you also hide the words ‘Services’ and the Word ‘Clients’ ?
Also what happens when you hide the other items? Does the page reflow because there is empty space where everything was or do you just leave blank holes in the page where the elements were originally.
There is a lot to consider before a solution could be considered but at the simplest level you would start by adding a class to the parent as mentioned and then utilising that to hide and show everything except the ones you want.
I can see this getting quite complicated very quickly . I was also not quite sure what the back button was doing as surely the X or the (-) would activate the closing of the menu. If you can clarify what you want in a bit more detail we could work towards a suitable solution.
Off topic you can’t put the hr where you put it:) All content in a list must be inside list element tags. Nothing at all can live outside of a list tag.
Ahhh ok that’s good to know. Was unsure of how much I could achieve with CSS alone. JavaScript it is
Yeee after my initial attempt this is exactly what I ran into. Lack of thinking far enough ahead on my end haha. Better to support deep nested navigations from outset.
So initially the +/- and back button was me wanting to make it multi-use (accordion, file tree, and a mega menu) but to keep things simple: the + acts as the trigger and the back button acts as the - taking you to the previous upper level.
So for example:
[Initial state of what’s visible.]
Home
Services +
Contact
[You click ‘Services’, now this is only visible]
< Back - takes you back to its parent level
Service 1
Service 2
Clients +
[You click ‘Clients’, now this is only visible]
< Back - takes you back to its parent level
Something
Something 1
I forked this pen i found earlier that is pretty much what i’m trying to accomplish just minus the jquery lol. Mobile Menu Prototyping (codepen.io)
Thanks for your help.
This is really good to know, I wasn’t aware. Thanks
We should probably throw this back to the JS forum for refinement but here’s how I would attempt this as a starting point.
It only handles 2 nestings so any further nestings would need to be hard coded or perhaps automated in some way but I didn’t want to get any more complex for a proof of concept
Thanks Paul, ye that’s pretty much it. Yes please can we send it back to the JS forum. For unlimited/more than 2 nesting purposes how would that be automated? Would another loop be needed? or could you make use of nextElementSibling to continue getting the children.
This allows for multiple levels with a much simplified css and html.
Here’s the barebones unstyled version that may be easier to follow:
(It might need testing as I’ve only just finished it )
Lastly here is a version with a reset (back) button added when two levels or more of the menu are open so that you can close easily when nested in multiple levels.
I’m gonna be editing slightly to make it mimic this behaviour, if you click menu then click services you can see the transition effect. so gotta switch from left slide in to right slide in https://www.delt.net/
Thankssss! Also got one question regarding the trigger. I see it’s the + that turns into a big X. For more flexibility is it possible to change the code slightly so it uses an actual back button as the trigger? Or potentially replacing the actual like replacing the actual element from a <b> to a <button>?