I have created a custom non sticky sub menu that I want to appear when a certain menu item is clicked on. I'm having it scrolled to location when clicked and I've decided I want to control visibility with the height attribute. What is the correct way of doing this with CSS?
Also this is my first question ever. I appreciate tips on asking better questions.
The structure looks something like this
#submenu {
height: 0;
overflow: hidden;
}
.submenu-trigger:focus~#submenu {
height: 134px !important;
}
<div id="wrapper">
<header>
<nav>
<ul>
<li><a>Item 1</a></li>
<li><a>Item 2</a></li>
<li><a class="submenu-trigger" href="#submenu">Item 3</a></li>
<li><a>Item 4</a></li>
</ul>
</nav>
</header>
<!-- Placed outside to follow page scroll -->
<section>
<div id="submenu">
<nav>
<ul>
<li><a>Item 1</a></li>
<li><a>Item 2</a></li>
<li><a>Item 3</a></li>
</ul>
</nav>
</div>
</section>
<main>
…
</main>
</div>
Edit: It seems this isn't possible with CSS. How do I get the submenu to retract using JS when the main menu item is no longer in focus?
Edit 2: Implemented a variant of @biberman's suggestion at https://ensjotannklinikk.no/forside-wip/. Here's the differences in the live version:
"Behandlinger" = the menu item in question
.behandlinger-menyinstead of#submenu.behandlinger-iteminstead of.submenu-trigger55pxinstead ofauto, to preserve CSS transition
document.querySelector('.behandlinger-item').addEventListener('click', function() {
var submenu = document.querySelector('.behandlinger-meny');
submenu.style.height = '55px';
});
It's really close, kudos to @biberman.
Only thing missing is
Having it stay open on every click (not toggle on and off).Figured myself.Toggle off (back to height 0) when clicking anywhere else but@biberman strikes again..behandlinger-menyand.behandlinger-itemThis fix doesn't work on mobile. New question.
Thank you so much, Stack Overflow!