'Auto-Height a responsive menu
I'm trying to make a mobile menu with the structure of ul > li > ul which each sub ul having its height based of the number of li.
But when entering a sub-sub tiny ul of a big sub ul makes the sub-sub tiny ul overflow vertically which is showing the content of its parent :
How can I prevent the sub-sub tiny ul to be overflow vertically ?
$(document).ready(function(){
let menu = $('nav')
// open submenu
menu.find('a').on('click', function(){
let parent = $(this).parent()
parent.toggleClass('active')
})
// close submenu
menu.find('.back').on('click', function(){
let parent = $(this).parent().parent()
parent.toggleClass('active')
})
})
:root {
font-size: 10px;
}
* {
box-sizing: border-box;
}
nav {
position: fixed;
width: 300px;
box-shadow: 0 0 5px rgba(0, 0, 0, .2);
overflow: hidden;
}
nav a:hover {
color: blue;
}
nav > ul {
height: auto;
}
ul {
list-style: none;
padding-inline-start: 0;
padding: 0 1.5rem;
margin: 0;
width: 100%;
background: #FFF;
}
ul > li:not(:last-child) {
border-bottom: 1px solid #AAA;
}
ul > li > a {
display: flex;
justify-content: space-between;
padding: 1.2rem 1.5rem;
font-size: 1.5rem;
line-height: 1.5em;
font-weight: bold;
text-transform: uppercase;
font-family: sans-serif;
cursor: pointer;
}
ul > li:not(.back) > ul ~ a::after {
content: '►';
}
ul > li > ul {
position: absolute;
top: 0;
left: 0;
transform: translateX(100%);
transition: .3s;
overflow-x: hidden;
z-index: 2;
height: 100%;
}
ul > li.back {
margin: 0 -1.5rem;
border-bottom-width: 0;
z-index: 1;
}
ul > li.back > a {
background: #DDD;
font-size: 1rem;
justify-content: flex-start;
}
ul > li.back > a::before {
content: '◄';
margin-right: .5rem;
}
ul > li.see-all > a {
padding: .5rem 0;
font-size: 1.3rem;
text-align: center;
text-transform: inherit;
justify-content: center;
}
ul > li.active > ul {
transform: translateX(0);
}
ul.dark {
padding: 0;
}
ul.dark > li:not(:last-child) {
border-bottom: 1px solid #FFF;
}
ul.dark > li > a {
background: #AAA;
padding: 1.5rem 3rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
<ul>
<li>
<ul>
<li class="back">
<a>Return</a>
</li>
<li>
<ul>
<li class="back">
<a>Return</a>
</li>
<li>
<ul>
<li class="back">
<a>Return</a>
</li>
<li>
<a>Sub-sub-category 1</a>
</li>
<li>
<a>Sub-sub-category 2</a>
</li>
</ul>
<a>Sub-category 1</a>
</li>
<li>
<a>Sub-category 2</a>
</li>
<li>
<a>Sub-category 3</a>
</li>
<li>
<a>Sub-category 4</a>
</li>
<li>
<a>Sub-category 5</a>
</li>
</ul>
<a>Category 1</a>
</li>
<li>
<a>Category 2</a>
</li>
<li>
<a>Category 3</a>
</li>
<li class="see-all">
<a>See all products</a>
</li>
</ul>
<a>Products</a>
</li>
<li>
<ul>
<li class="back">
<a>Return</a>
</li>
<li>
<a>Fiesta</a>
</li>
</ul>
<a>Events</a>
</li>
</ul>
<ul class="dark">
<li>
<a>Stores</a>
</li>
<li>
<a>Account</a>
</li>
<li>
<a>Contact</a>
</li>
</ul>
</nav>
Thanks for feeback
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

