Replace mobile browser default fullscreen dropdown

Viewed 80

I've looked for solutions but I couldn't find any.

I have a select and few option, here's my example:

<select>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

I want it to look like this, not this, which is showing on mobile by default.

  1. Is it possible to do so?
  2. Is there any way to do this in CSS?
1 Answers

Every browser and operating system renders <select> tags differently. There's some good information on that here and here.

Mobile <select> elements are rendered in mobile browsers with usability and accessibility in mind, so it's advisable not to subvert that, but there are a few different strategies you can use to make them look and behave the way you want.

One of those strategies that takes a css-only approach involves keeping the native, semantic <select> tag, creating a "presentation" layer, then sitting the native <select> tag in front of the "presentation" layer but setting the native tag to be invisible.

Using the select will then trigger the normal interaction. It's a good option because it preserves the semantic html and avoids having to recreate an interaction that is hard to get right, especially when you factor in mobile experiences and accessibility for keyboard-only users.

And it achieves your goal by creating explicit presentation rules you can enforce for mobile devices.

Here's a contrived example, but you can set your own style rules to make this look the way you want, and you could use media queries to target the devices you have in mind if you choose:

.wrapper {
  position: relative;
  width: max-content;
}

select {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* setting the native select to be invisible by setting opacity to 0*/
  opacity: 0;
}

.presentation-select {
  color: black;
  background-color: white;
  font-size: 1rem;
  padding: 4px 8px;
  padding-right: 52px;
  border-radius: 0px;
  border: 1px solid black;
}

.arrow-wrapper {
  position: absolute;
  top: 7px;
  right: 28px;
  pointer-events: none;
}
<div class="wrapper">
  <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
  <select>
   <div class="presentation-select">
    1
     <div class="arrow-wrapper">
       <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="black" class="bi bi-chevron-down" viewBox="0 0 16 16">
         <path fill-rule="evenodd" d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z"/>
       </svg>
     </div>
   </div>
</div>

Hope this was helpful! ✌️

Related