CSS focus powered dropdown menu


Today we are making a CSS only powered dropdown menu!

15 Nov, 2020 · 2 min read

Today we’ll be making something slightly different. A complete CSS-powered dropdown menu!

No JavaScript required!

We will use a button with a focus state. We’ll check if we need to show this menu.

The result will look like this:

Focus menu CSS only

I’m using Tailwind CSS for this tutorial to focus more on the actual effect.

You can find my Tailwind article here.

HTML Structure

The HTML will be a navbar container, a logo, and a user icon on the right.

Tailwind does the styling and mainly uses flex to align the items.

As you can see, we have a button with the ID user-menu next to it. We have a div with the ID user-menu-dropdown. This will be the dropdown we’ll show once we focus on the button.

<nav class="flex items-center justify-between h-full p-3 m-auto bg-orange-200">
  <span>My Logo</span>
  <div class="relative">
    <button id="user-menu" aria-label="User menu" aria-haspopup="true">
        class="w-8 h-8 rounded-full"
      class="absolute right-0 w-48 mt-2 origin-top-right rounded-lg shadow-lg top-10 menu-hidden"
        class="p-4 bg-white rounded-md shadow-xs"
          class="block px-6 py-2 mb-2 font-bold rounded"
          >My profile</a
        <a href="#" class="block px-6 py-2 font-bold rounded" role="menuitem"

CSS menu on focus

To add the effect, we need to target the focus on the button. But first, let’s hide our dropdown and add a small effect.

Note: We could use @apply, but CodePen doesn’t support this

#user-menu ~ #user-menu-dropdown {
  transform: scaleX(0) scaleY(0);
  transition-timing-function: cubic-bezier(0.4, 0, 1, 1);
  transition-duration: 75ms;
  opacity: 0;
  top: 3.25rem;

For the dropdown, we add a transform to make it animate from the corner. Then we add an opacity of 0 to hide it.

Now we need to target the hover.

We use the #user-menu:focus and then target the following (~) dropdown.

We also add a focus-within in case someone clicks a link in the dropdown, and the menu will stay active then.

#user-menu ~ #user-menu-dropdown:focus-within,
#user-menu:focus ~ #user-menu-dropdown {
  transform: scaleX(1) scaleY(1);
  transition-timing-function: cubic-bezier(0, 0, 0.2, 1);
  transition-duration: 100ms;
  opacity: 1;

You can see the complete example on this Codepen.

See the Pen CSS focus powered dropdown menu by Chris Bongers (@rebelchris) on CodePen.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Modifying an SVG path with CSS

10 Dec, 2022 · 2 min read

Modifying an SVG path with CSS

Animate an SVG path with CSS

9 Dec, 2022 · 2 min read

Animate an SVG path with CSS

Join 2099 devs and subscribe to my newsletter