0PricingLogin
Tailwind CSS Academy · Lesson

Styling Headless Menu and Dropdown

Apply Tailwind utility classes to Headless UI's Menu component to create a polished dropdown with keyboard navigation and ARIA attributes.

Anatomy of a Headless UI Menu

The Headless UI Menu is composed of four pieces: Menu (the root wrapper that provides context), Menu.Button (the clickable trigger), Menu.Items (the dropdown panel that shows when open), and one or more Menu.Item elements (individual selectable options). Each piece accepts className or a render function that receives state, giving you full Tailwind control at every level.

import { Menu } from '@headlessui/react';

// Structure overview
<Menu as='div' className='relative inline-block'>
  {/* 1. Trigger button */}
  <Menu.Button>Open</Menu.Button>

  {/* 2. Dropdown panel (shown when open) */}
  <Menu.Items>
    {/* 3. Individual options */}
    <Menu.Item>{({ active }) => <button>Option A</button>}</Menu.Item>
    <Menu.Item>{({ active }) => <button>Option B</button>}</Menu.Item>
  </Menu.Items>
</Menu>

Styling the Menu Trigger Button

The Menu.Button trigger should communicate interactivity and show its open/closed state visually. Access the open render prop to change the button's appearance when the dropdown is visible. Common patterns include rotating a chevron icon, changing the background color to match the open dropdown, and adding a ring for focus visibility.

import { Menu } from '@headlessui/react';
import { ChevronDownIcon } from '@heroicons/react/20/solid';

<Menu.Button className='inline-flex items-center gap-2 rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500'>
  {({ open }) => (
    <>
      Actions
      <ChevronDownIcon
        className={cn(
          'h-4 w-4 text-gray-400 transition-transform duration-150',
          open && 'rotate-180'
        )}
      />
    </>
  )}
</Menu.Button>

All lessons in this course

  1. Introduction to Headless UI
  2. Styling Headless Menu and Dropdown
  3. Building Accessible Dialogs
  4. Transitions With Headless UI
← Back to Tailwind CSS Academy