Created
December 18, 2023 11:35
-
-
Save gusbemacbe/1f45e2ce02bf1d02e5006e62671883af to your computer and use it in GitHub Desktop.
Simple CSS Dropdown Menu with Hover and :focus-within and Focus states and ARIA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nav role="navigation"> | |
<ul> | |
<li><a href="#">One</a></li> | |
<li><a href="#" aria-haspopup="true">Two</a> | |
<ul class="dropdown" aria-label="submenu"> | |
<li><a href="#">Sub-1</a></li> | |
<li><a href="#">Sub-2</a></li> | |
<li><a href="#">Sub-3</a></li> | |
</ul> | |
</li> | |
<li><a href="#">Three</a></li> | |
</ul> | |
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a { | |
text-decoration: none; | |
} | |
nav { | |
font-family: monospace; | |
} | |
ul { | |
background: darkorange; | |
list-style: none; | |
margin: 0; | |
padding-left: 0; | |
} | |
li { | |
color: #fff; | |
background: darkorange; | |
display: block; | |
float: left; | |
padding: 1rem; | |
position: relative; | |
text-decoration: none; | |
transition-duration: 0.5s; | |
} | |
li a { | |
color: #fff; | |
} | |
li:hover, | |
li:focus-within { | |
background: red; | |
cursor: pointer; | |
} | |
li:focus-within a { | |
outline: none; | |
} | |
ul li ul { | |
background: orange; | |
visibility: hidden; | |
opacity: 0; | |
min-width: 5rem; | |
position: absolute; | |
transition: all 0.5s ease; | |
margin-top: 1rem; | |
left: 0; | |
display: none; | |
} | |
ul li:hover > ul, | |
ul li:focus-within > ul, | |
ul li ul:hover, | |
ul li ul:focus { | |
visibility: visible; | |
opacity: 1; | |
display: block; | |
} | |
ul li ul li { | |
clear: both; | |
width: 100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment