Select List Theming in Drupal 8

Select List Theming in Drupal 8

4 minutes
Drupal select list banner
AI Summary

Styling native form elements has always been a battle against browser inconsistencies (and let's not even mention the nightmares of old Internet Explorer). In this guide, we bypass the limitations of standard CSS appearance: none and implement Select2—a powerful library that turns boring dropdowns into searchable, customizable components. We cover the full Drupal 8 integration process, from registering the library in libraries.yml to writing the custom Drupal.behaviors JavaScript needed to make it robust for mobile and touch devices.

Theming of form items and then performance issues is one of the hard jobs. I am not mentioning IE here because Dear internet Explorer has crosses all limits of giving pain. Now the happy news is that IE is dead and I am sure it will be burning in the deepest hole of hell. The pain, the time waste that IE has done in past years will not be fixed by Microsoft ever. Even if they sell their shares and pay all money to Front-End Developers.

I think I am going far with this now, back on the track. There are many ways to theme form items. Browsers and Operating systems render form items differently. Which is a reason for design inconsistencies across platforms. Depending on the amount of theming you have to do for form items and select boxes particularly.

Starting with the below code in your style sheets will give you jump start and idea what of what else to achieve. Its important to cascade the default appearance.

appearance: none;
-vendor_prefix-appearance: none;

What if we have to select multiple options from drop-down or we have a very long drop-down list and (For example country names) and scrolling through is time consuming. What if we have a search inside the select list?

All this is possible with lot of libraries available but here we are focusing on one of the best libraries select2. Select2 gives us a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

Particularly theming the select drop-down, which isn’t easy. border, drop-down arrows option list cant be themed easily without certain JS. We will unleash select2 for these things. It isn’t recomended to use Select2 for small customizations. If you want maximum out of it then it can be used otherwise there will be some small performance issues.

Its simple to use select2 in any site but with drupal there comes small customizations.

Firstly we will install select2 library and declare in our theme.libraries.yml

 js:
   libs/select2.min.js: {}
  

in my case I have put all libraries in libs folder. Now we need to do some little customizations to make it work on our drupal site. Actually it is mainly a class and wrapper issues that select2 isn’t getting applied with just library setup.

in our common JS file, we need to add some customised code. For me it is.

(function ($, Drupal, window, document) {

  'use strict';

  Drupal.behaviors.SelectCases = {
    attach: function (context, settings) {
      $(window, context).load(function () {
      // Changing our default select lists
            $('#content select.form-select').each(function (i) {
              var $selectIndex = $('select-' + (i + 1));
              $(this)
              .select2({
                width: 'style',
                shouldFocusInput: function (instance) {
                  // Attempt to detect touch devices
                  var supportsTouchEvents = (('ontouchstart' in window) ||
                  (navigator.msMaxTouchPoints > 0));
      
                  // Only devices which support touch events should be special cased
                  if (!supportsTouchEvents) {
                    return false;
                  }
      
                  return true;
                }
              });
              $('.select2', context)
                .once()
                .addClass('form-select').addClass('select-id-' + (i + 1));
      
            });
        }
    };
            
 })(jQuery, Drupal, this);
  

Now that new markup is generated and you can easily theme it, because it will be skinned in divs rather than .

Go ahead and theme as per your comps.

Key Takeaways
  • The Native Trap: Native select boxes render differently on every OS and browser. Trying to style the <option> tag is often a dead end.
  • Why Select2? It solves three major problems:
    1. Consistency: It looks the same on all platforms.
    2. UX: Adds search functionality for long lists (like countries).
    3. Styling: It converts the UI into standard <div> elements that are easy to style with CSS.
  • Drupal Integration: You can't just drop the script in. You must register it in theme.libraries.yml and attach it via Drupal.behaviors to ensure it works with Drupal's caching and AJAX loading.
  • Mobile Optimization: The provided code snippet includes a crucial shouldFocusInput check to prevent usability issues on touch devices—a detail often overlooked in standard tutorials.
0
Resume Icon

Professional Journey

An overview of my experience scaling global teams, managing operations, and my evolution from technical architecture to executive leadership.

View Experience →
Blog Icon

Insights & Articles

Deep dives into digital accessibility, team scaling strategies, and the technical challenges of building inclusive web architectures.

Browse All Posts →
Talks Icon

Speaking & Keynotes

A curated list of my talks, workshops, and panel discussions delivered at technology conferences across Europe, Asia, and North America.

Explore All Talks →