Styling elements consistently across browsers has long been a difficult task. While it is now possible to style the button part of a s do.

Screenshot of a select element containing a list of French cities. The select element is styled in a way that currently cannot be achieved with the built-in select HTML element and CSS. The options have borders around them, icons next to them, and they are organized in a grid with 4 columns.

In this article, we’ll review the current state of styling element:

  • Button: this part is visible to users before they interact with the . They’re just easy to understand.

    With this out of the way, let’s review how the various parts can be styled today.

    Styling the button part

    Doing this is pretty easy nowadays. We can apply CSS properties to the element (on the left in the image below) to something more visually pleasing and which might integrate better with the rest of our website (on the right in the image below):

    Two select elements, we only see the button part, not the drop-down with the options. The first one on the left has the default browser styling. The second one on the right has been custom-styled with CSS. It has a bigger font, more padding, a different background, and borders.

    Even better, styling the button part works very well across browsers. Back in 2018, the Filament Group design studio published Styling a Select Like It’s 2019, a blog post and CSS code snippet that showed how to style a s to look the same across browsers.

    Styling the listbox part

    Sadly, there’s no way for web developers today to style the listbox. There simply is no way to target this part of the looks in a web page. Let’s take a look at an example in Microsoft Edge (on the left) and Firefox (on the right), both on Windows 11:

    Two select elements, with default browser styles, and 3 options. The first select on the left is rendered by Edge, with a large and blurry drop-shadow and large rounded corners. The second select, on the right, is rendered by Firefox, and has a sharper and smaller drop shadow, and smaller rounded corners.

    As this example shows, the listbox part comes with a few things you might want to customize to your needs:

    • the way the shadow drops on the rest of the page might not work well with the rest of your styles,
    • the color, thickness, and rounding of the border might feel a bit out of place as well.

    Depending on the browser, and operating system, the listbox part may be implemented with internal user-agent shadow DOM, or directly by the OS, none of which can be targeted by our CSS unfortunately.

    Styling the option part

    For this final part, things are a bit better. Several CSS properties can be used to change the appearance of an option, and some properties applied to the spec: the element. This element is a new implementation of a select control, but fully stylable with CSS, and much more extensible too.

    The element is only available in Chromium-based browsers for now, and to use it, you’ll have to switch the “Experimental Web Platform features” flag in about://flags first.

    Using , and a few lines of CSS code, you can create designs like these two examples below:

    Two examples of a styled selectmenu element. The first one shows a list of browsers. The button part is styled with light grey borders and rounded corners. The listbox part has a light drop-shadow, and the options in the listbox have browser icons next to them. The second example contains a list of French cities as options. The options are organized in 2 vertical columns. Borders are drawn between options. And little square icons are displayed next to each option.

    Notice the custom borders and shadows, the separators between options, the images, and custom layout.

    Here is part of the HTML and CSS code that was used for the demo on the right, with the list of cities:

    The new element lets you style every aspect of the different parts of the control. As a developer, you get access to it all, and are free to define your own styles, while being certain that the browser will take care of things like positioning the popup, handling keyboard access, and wiring the right accessibility semantics.

    If you want to learn more about Open UI, the element, and how you can help with this effort, you can read my longer article about it: Say Hello to selectmenu, a Fully Style-able select Element, on CSS Tricks.

    Keep in mind, however, that this is very new and experimental. The spec and implementation will most definitely change as the result of ongoing discussions.

    But why does this matter?

    You might argue that styling the color and font of options is more than enough, and anything more is not really necessary. After all, a select is made for users to quickly choose an option from a list and that’s it. If you, as a web developer, cannot style this part of your user flow and instead are forced to rely on what the browser and OS provide, then at least your users will have a familiar and optimized experience.

    Take iOS for example, the iPhone and iPad operating system, options on this platform can’t be styled at all, even choosing a different font or color doesn’t work, for good reasons. As the available room may be limited on the device, it’s great that the OS can take over the rendering, positioning, sizing, and styling of the list of options for users to effectively make their choice in a familiar UI.

    So, if you need your users to choose from a list of options (and if other solutions like a bunch of radio buttons aren’t feasible), using a native element, and it’s possible to switch one with the other.

    Note, however, that at the time of this writing the element also allows web developers to replace the entire user agent shadow DOM and instead insert their own custom markup. This means it is possible to not only replace the button, listbox, and options parts, but also add extra elements around or within them. This is a super power that can make it possible to create a lot of functionality that the existing , and it also means that you wouldn’t be able to switch between a element gets you very far already, is accessible, way cheaper than using a custom library, and familiar to your users.

    And now, with the work that Open UI and Microsoft are doing, there’s an even brighter future ahead of us. One where we can style the contents of our selects just like we’ve always wanted, without the added complexity.

    Are there other things you wish the Web could do? Let browser vendors know, submit your ideas on The Web We Want!

    – Patrick Brosset, Senior Product Manager, Microsoft Edge

    Source

Leave a Reply

Your email address will not be published. Required fields are marked *