I wanted to make a drop down menu that would display sub-menus on a click using CSS pseudo class called focus-within, but it does not work if I click the dropdown menu (Click to dropdown) again.
I have added the code in the code snippet, so you can run the example.
.example-menu {
list-style: none;
padding: 0;
margin: 0;
display: inline-flex;
flex-direction: row;
background: #ffffff;
border: solid 2px #000000;
}
.example-dropdown {
position: relative;
}
.example-dropdown > ul {
list-style: none;
padding: 0;
margin: 0;
display: none;
flex-direction: column;
position: absolute;
background: #ffffff;
border: solid 2px #000000;
}
.example-dropdown:focus-within > ul {
display: flex;
}
.example-menu a {
display: inline-block;
padding: 0.5rem;
}
.example-dropdown > a {
display: inline-block;
padding: 1rem;
color: #000000;
}
.example-dropdown > a::after {
content: " \2193"
}
<ul class="example-menu">
<li class="example-dropdown">
<a tabindex="0">Click to dropdown</a>
<ul>
<li><a href="http://example.com">Link</a></li>
<li><a href="http://example.com">Link</a></li>
<li class="example-dropdown">
<a tabindex="0">Click to dropdown</a>
<ul>
<li><a href="http://example.com">Link</a></li>
<li><a href="http://example.com">Link</a></li>
<li><a href="http://example.com">Link</a></li>
</ul>
</li>
</ul>
</li>
</ul>
The dropdown does not disappear.
Is there any way to make it work?