DEV Community

Cover image for CSS focus powered dropdown menu

CSS focus powered dropdown menu

Chris Bongers on November 15, 2020

Today we'll be making something slightly different. A full CSS powered dropdown menu! No JavaScript required! We will make use of a button, wit...
Collapse
 
angeliquejw profile image
Angelique •

Given that this is a CSS-only solution, I was really anticipating accessibility to be totally non-existent. Truly grateful, Chris, to see that is NOT the case and you've thoughtfully included ARIA attributes in your example. πŸ‘πŸ»πŸ‘πŸ»πŸ‘πŸ»πŸ‘πŸ»

Collapse
 
dailydevtips1 profile image
Chris Bongers •

Thank you, Angelique, I try to do so, but it's hard.
Not 100% sure on how it would work when you tab, so it opens and how you would get it away again.

Collapse
 
swizzard profile image
sam •

It doesn't seem to work on ios chrome?

Collapse
 
dailydevtips1 profile image
Chris Bongers •

You are right Sam!

For ios we would need to add something like this to the button:

onclick="this.focus()"
Enter fullscreen mode Exit fullscreen mode

But then we are using JS again, and it won't re-close the menu with this.

Collapse
 
swizzard profile image
sam •

well that's a bummer--there's no shim or any other option?

Thread Thread
 
dailydevtips1 profile image
Chris Bongers •

Hey Sam, unfurtionally not that I know off, someone else might know, I'll do some more research on how to fix this for iOS stable.

Collapse
 
yellow1912 profile image
yellow1912 •

On mobile clicking on the avatar again does not close the drop-down.

Collapse
 
raubaca profile image
Raul Barrera • • Edited

Because it's open on avatar focus, so if you tap outside it must be closed

Collapse
 
dailydevtips1 profile image
Chris Bongers •

Yeah with focus that doesn't work, you have to click anywhere BUT the avatar to close it.

Collapse
 
raubaca profile image
Raul Barrera •

Nice tip!

Collapse
 
dailydevtips1 profile image
Chris Bongers •

Thank you Raul! ✌️

Collapse
 
rahxuls profile image
Rahul •

Amazing post for beginners and everyone. Usually, when doing this i made mistakes but all is clear now. AmazingπŸ’₯πŸ’₯πŸ’₯

Collapse
 
dailydevtips1 profile image
Chris Bongers •

Oh very nice to hear Rahul, glad it's clear