--- title: Dropdown desc: Dropdown can open a menu or any other element when the button is clicked. layout: components classnames: component: - class: dropdown desc: Dropdown container part: - class: dropdown-content desc: Content part placement: - class: dropdown-start desc: Align horizontally to start of button default: true - class: dropdown-center desc: Align horizontally to center of button - class: dropdown-end desc: Align horizontally to end of button - class: dropdown-top desc: Open from top - class: dropdown-bottom desc: Open from bottom default: true - class: dropdown-left desc: Open from left - class: dropdown-right desc: Open from right modifier: - class: dropdown-hover desc: Opens on hover too - class: dropdown-open desc: Force open --- ## There are 3 methods to use a dropdowns 1. [details and summary elements](#method-1-details-and-summary) 2. [popover API and anchor positioning `new`](#method-2-popover-api-and-anchor-positioning-new) 3. [CSS focus](#method-3-css-focus) ## Method 1. details and summary details and summary are native HTML elements that can be used to create dropdowns. Dropdown using details and summary opens and closes the content when the summary is clicked. You can also open/close it using JS by adding/removing the `open` attribute ```:Structure Container for the button + content │ │ button to toggle the visibility of the content │ │
──╯ │ open or close ──╯ Content
``` ### ~Dropdown using details and summary ```html
open or close
``` ## Method 2. popover API and anchor positioning `new` Popover API is the new HTML standard for creating dropdowns. It opens the dropdown on a top layer, which means there's no need to z-index management and there's no overflow issue. Anchor positioning is a new CSS standard for positioning elements relative to an anchor. ```:Structure Unique ID │ │ │
Content
``` > :INFO: > > `popovertarget` is the unique ID of the popover content. > `anchor-name`/`position-anchor` is the unique name of the anchor. > :INFO: > > CSS Anchor Positioning is a new standard but isn't yet supported in Firefox and Safari ([caniuse.com](https://caniuse.com/css-anchor-positioning)). > In those browsers, the dropdown will appear centered like a modal. > There's also [this polyfill](https://github.com/oddbird/css-anchor-positioning) that can be helpful. ### ~Dropdown using popover API and anchor positioning ```html ``` ```jsx {/* change popover-1 and --anchor-1 names. Use unique names for each dropdown */} {/* For TSX uncomment the commented types below */} ``` ## Method 3. CSS focus The content gets displayed when the button is focused. ```:Structure Focusable button │
Click to open
─────╯
Content
───────╮
│ Content shown when button is focused ``` > :INFO: > > Why div? > Safari has a [CSS bug](https://bugs.webkit.org/show_bug.cgi?id=22261) since 2008 that prevents button elements from being focused, so we use div `tabindex="0"` as a workaround. > This approach is accessible with the addition of `role="button"` and functions consistently across all browsers. ### ~Dropdown menu
Click outside to close
```html
Click
``` ## Positions ### ~Dropdown / aligns to start of button horizontally ```html
Click ⬇️
``` ### ~Dropdown / aligns to end of button horizontally ```html
Click ⬇️
``` ### ~Dropdown / aligns to center of button horizontally ```html
Click ⬇️
``` ### ~Dropdown top ```html
Click ⬆️
``` ### ~Dropdown top / aligns to center of button horizontally ```html
Click ⬆️
``` ### ~Dropdown top / aligns to end of button horizontally ```html
Click ⬆️
``` ### ~Dropdown bottom (default) ```html
Click ⬇️
``` ### ~Dropdown bottom (default) / aligns to center of button horizontally ```html
Click ⬇️
``` ### ~Dropdown bottom (default) / aligns to end of button horizontally ```html
Click ⬇️
``` ### ~Dropdown left ```html
Click ⬅️
``` ### ~Dropdown left / aligns to center of button vertically ```html
Click ⬅️
``` ### ~Dropdown left / aligns to end of button vertically ```html
Click ⬅️
``` ### ~Dropdown right ```html
Click ➡️
``` ### ~Dropdown right / aligns to end of button vertically ```html
Click ➡️
``` ### ~Dropdown right / aligns to center of button vertically ```html
Click ➡️
``` ## Modifiers ### ~Dropdown on hover ```html
Hover
``` ### ~Force open ```html
Button
``` ## More examples ### ~Card as dropdown ```html
Click

This is a card. You can use any element as a dropdown.

``` ### ~Dropdown in navbar ```html
daisyUI
Button
Dropdown
``` ### ~Helper dropdown
A normal text and a helper dropdown
```html A normal text and a helper dropdown

You needed more info?

Here is a description!

```