DEV Community

David Day
David Day

Posted on

CSS-only Dropdown with Native HTML

I previously wrote an article on my rewrite of PicoCSS. One of the components that I completely rewrote was a CSS-only and classless dropdown using native HTML. Previously, the library offered a class-based version, but I wanted to rewrite it keeping with the classless approach that T'Pico took.

There are some differences between a native <select> element, a native <details> element, and a dropdown component even though they're all interactive elements that toggle the visibility of content. The <select> element is typically used in forms to let users select a predefined value. The <details> element is typically used as an accordion to reveal additional information (most commonly used as FAQ sections). A dropdown component is really a way of opening a floating menu of actions.

On the HTML side, a dropdown works by leveraging the native <button> element along with the popovertarget attribute. The popovertarget references a list (for T'Pico, specifically the <ul> element) with the popover attribute.

While buttons and lists have been in HTML practically forever, the only recent introduction has been the popovertarget and popover attributes. According to the Mozilla Developer Network, both attributes have been available across browsers since April 2024.

Take for example this markup:

<button type="button" popovertarget="search-engines">Search Engines</button>
<ul id="search-engines" popover>
  <li>
    <a href="http://search.brave.com/">Brave</a>
  </li>
  <li>
    <a href="https://duckduckgo.com/">DuckDuckGo</a>
  </li>
  <li>
    <a href="https://www.dogpile.com/">Dogpile</a>
  </li>
  <li>
    <button type="button" popovertarget="search-engines" popovertargetaction="hide">Close menu</button>
  </li>
</ul>
Enter fullscreen mode Exit fullscreen mode

The browser will natively open the popover without any JavaScript. This alone will work, although it'd look fairly boring using the browser defaults. To style the popover to look and behave like a dropdown tied to the button without any JavaScript, bleeding edge CSS properties/method such as position-area, position-try-fallbacks, and anchor-size() can be used. How bleeding edge are we talking about? Well, according to the Mozilla Developer Network, these only became available in January... of this year!

This is a bare minimum example of styling the list:

:where(ul[popover]) {
  position: fixed;
  position-area: block-end span-inline-end;
  position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
  min-inline-size: anchor-size(width);
}
Enter fullscreen mode Exit fullscreen mode

If you would like to see how I styled the dropdown in T'Pico, you can check out the source code here: https://github.com/dday9/t-pico/blob/master/src/components/dropdown.css

And as always, here's a working demo of a dropdown in action: https://jsfiddle.net/qsvdepar/

P.S. - I'm interested in hearing your feedback. In T'Pico, I used the same selector shown above which means that any <ul> element that uses the popover attribute will have the dropdown styles, even if isn't intended to be a dropdown. Since the component is designed to be a button immediately followed by an unordered list that leverages the Popover API, I could assert this structure by changing to:

:where(button[popovertarget] + ul[popover]) {
  /* ... */
}
Enter fullscreen mode Exit fullscreen mode

I'm curious to hear y'all's thoughts on the architectural decision.

Top comments (0)