Is there a CSS-only way to style a <select>
dropdown?
I need to style a <select>
form as much as humanly possible, without any JavaScript. What are the properties I can use to do so in CSS?
This code needs to be compatible with all major browsers:
- Internet Explorer 6,7 and 8
- Firefox
- Safari
I know I can make it with JavaScript: Example.
And I'm not talking about simple styling. I want to know, what the best we can do with CSS only.
I found similar questions on Stack Overflow.
And this one on Doctype.com.
A very nice example that uses
:after
and:before
to do the trick is in Styling Select Box with CSS3 | CSSDeckI had this exact problem, except I couldn't use images and was not limited by browser support. This should be «on spec» and with luck start working everywhere eventually.
It uses layered rotated background layers to «cut out» a dropdown arrow, as pseudo-elements wouldn't work for the select element.
Edit: In this updated version I am using CSS variables and a tiny theming system.
The blog post How to CSS form drop down style no JavaScript works for me, but it fails in Opera though:
You definitely should do it like in Styling select, optgroup and options with CSS. In many ways, background-color and color are just what you would typically need to style options, not the entire select.
A CSS & HTML only solution
I seems compatible with Chrome, Firefox & IE11. But please leave your feedback regarding other web browsers.
As suggested by @Danield answer, I wrap my select in a div (even two divs for x-browser compatibility) to get the expected behavior.
See http://jsfiddle.net/bjap2/
HTML:
Notice the 2 div wrappers. Also notice the extra div added to place the arrow-down button wherever you like (positioned absolutely), here we put it on the left.
CSS
I got to your case using Bootstrap. This is the simplest solution that works:
Note: the base64 stuff is
fa-chevron-down
in SVG.