Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Change Select Box Background Without Losing the Right Arrow on Mobile Safari

I need to change the background on my select box to a flat color but I don't want the right arrow (aka the drop-down arrow) to go away.

I have tried:

select {
    -webkit-appearance: none;
    border: 0;
    background: none;
}

This works for all browsers except for Safari on my iPhone 4s with iOS 7. On this browser the right-arrow disappears and it is not clear to the user that it is a select box.

Omitting -webkit-appearance doesn't help.

I don't want to make a fake arrow. That will affect all the other browsers.

Any ideas?

like image 625
StephenKC Avatar asked Sep 04 '14 20:09

StephenKC


Video Answer


2 Answers

This is what I do. At least with iOS8 it works great and gets rid of that awful gradient, while allowing the dropdown to sit on a non white background.

enter image description here

I just used have background-color: white, closely followed by the image.

I did have to add a custom arrow, but I just did it inline in my css with a base64 PNG

.uglyselect & select  
{
    -webkit-appearance: none;
    background-color: white;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAJCAYAAAA/33wPAAAAvklEQVQoFY2QMQqEMBBFv7ERa/EMXkGw11K8QbDXzuN4BHv7QO6ifUgj7v4UAdlVM8Uwf+b9YZJISnlqrfEUZVlinucnBGKaJgghbiHOyLyFKIoCbdvecpyReYvo/Ma2bajrGtbaC58kCdZ1RZ7nl/4/4d5EsO/7nzl7IUtodBexMMagaRrs+06JLMvcNWmaOv2W/C/TMAyD58dxROgSmvxFFMdxoOs6lliWBXEcuzokXRbRoJRyvqqqQvye+QDMDz1D6yuj9wAAAABJRU5ErkJggg==);
    background-position : right center;
    background-repeat: no-repeat;
    padding-right: 1.5em;
}

What is .uglyselect you may ask? It's my Modernizr test for iPad

 Modernizr.addTest('uglyselect', function ()
 {
     return navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false;
 });

You can rename it as you please - or remove it if you're not using Modernizr. I prefer to limit it to iOS in this way so I didn't have to test it across all browsers (and that's what you originally requested anyway). The default dropdown for desktop browsers was acceptable for my design.

(These padding settings were optimized for numeric values and may need to be tweaked for longer text values).

like image 79
Simon_Weaver Avatar answered Oct 26 '22 09:10

Simon_Weaver


You are not going to find a good cross-browser implementation for styling the select element via CSS. The better solution is to create your own dropdown via javascript and style those elements uniformly across all browsers.

like image 39
bboysupaman Avatar answered Oct 26 '22 10:10

bboysupaman