145

I've got an HTML select box that I need to style. I'd prefer to use just CSS but if I have to I'll use jQuery to fill in the gaps.

Can anyone recommend a good tutorial or plugin?

I know, Google, but I've been searching for the last two hours and I'm not finding anything that meets my needs.

It needs to be:

  • Compatible with jQuery 1.3.2
  • Accessible
  • Unobtrusive
  • Completely customizable in terms of styling every aspect of a select box

Does anyone know anything that will meet my needs?

Apoorv
  • 1,091
  • 6
  • 19
  • 6
    you mean you want a customizable dropdown (built in jquery), right? because select boxes are not even browser objects (they are platform objects), very simple, very raw, and you cannot style much of it (you cannot style the borders for instance) – Ayyash Jul 02 '09 at 03:06
  • +1 for nice explaination and formatting! – Manish Dec 21 '09 at 14:18
  • 6
    **[Chosen](http://harvesthq.github.com/chosen/)** is worth mentioning but is not very customizable and has a lot of open issues. Nice to look at and use, though. – a paid nerd Oct 06 '11 at 21:03
  • 1
    @Ayyash What you are saying is true for MSHTML (Internet Explorer & friends) and WebCore (Safari) on some systems (certainly Mac OS). It is not true for Gecko (Firefox & friends). So that actual problem is that you cannot style `select` elements cross-browser. But if people are willing to live with the differences, you *can* style them – *including* the borders. – PointedEars Nov 12 '12 at 13:19
  • 1
    @PointedEras yes, in 2012 they can, but im guessing that was 3 years ago when I said what I said, im pretty sure I gave a shot back then and it didnt work – Ayyash Nov 14 '12 at 05:18
  • I found this: http://jquery-ui.googlecode.com/svn/branches/labs/selectmenu/index.html Using JqueryUI. Looks promising! – Shripad Krishna Jun 18 '10 at 05:55

15 Answers15

45

I've seen some jQuery plugins out there that convert <select>'s to <ol>'s and <option>'s to <li>'s, so that you can style it with CSS. Couldn't be too hard to roll your own.

Here's one: https://gist.github.com/1139558 (Used to he here, but it looks like the site is down.)

Use it like this:

$('#myselectbox').selectbox();

Style it like this:

div.selectbox-wrapper ul {
  list-style-type:none;
  margin:0px;
  padding:0px;
}
div.selectbox-wrapper ul li.selected { 
  background-color: #EAF2FB;
}
div.selectbox-wrapper ul li.current { 
  background-color: #CDD8E4;
}
div.selectbox-wrapper ul li {
  list-style-type:none;
  display:block;
  margin:0;
  padding:2px;
  cursor:pointer;
}
itsadok
  • 28,822
  • 30
  • 126
  • 171
Mark A. Nicolosi
  • 82,413
  • 11
  • 44
  • 46
  • I tried this version initially but the DIV is not positioned relative to where the select box is. Instead it is positioned hard to the left. –  Jul 02 '09 at 04:16
  • I haven't used this myself. The demo (http://www.brainfault.com/demo/selectbox/0.5/) for it looks fine. Look around for other implementations. I know I've seen several other plugins that do this. – Mark A. Nicolosi Jul 02 '09 at 04:51
  • 1
    I tried this and it worked for a while until I had to put more than one on the page. Now it's difficult to style because for some stupid reason the author thought it OK to use the same ID for all the elements rather than unique IDs or classes. Now I'm thinking of writing my own... – Jonathan Aug 15 '10 at 06:20
  • 19
    404 - any ideas where there were mirrors? – Moak Nov 22 '10 at 15:36
  • @MarkA.Nicolosi:Its completely override the select box as ul li . so initiated js functions in select box events are not working. How can i solve this – Gowri Nov 24 '11 at 05:06
  • 2
    Just beware that most select menu plugins are inaccessible to screen readers. Haven't yet found one that works. – Marcy Sutton Apr 19 '12 at 20:26
18

Update: As of 2013 the two I've seen that are worth checking are:

  • Chosen - loads of cool stuff, 7k+ watchers on github. (mentioned by 'a paid nerd' in the comments)
  • Select2 - inspired by Chosen, part of Angular-UI with a couple useful tweaks on Chosen.

Yeah!


As of 2012 one of the most lightweight, flexible solutions I've found is ddSlick. Relevant (edited) info from the site:

  • Adds images and text to select options
  • Can use JSON to populate options
  • Supports callback functions on selection

And here's a preview of the various modes:

enter image description here

papercowboy
  • 3,369
  • 2
  • 28
  • 32
14

As for CSS, Mozilla seems to be the most friendly, especially from FF 3.5+. Webkit browsers mostly just do their own thing, and ignore any style. IE is very limited, though IE8 lets you at least style border color/width.

The following actually looks fairly nice in FF 3.5+ (picking your color preference, of course):

select {
    -moz-border-radius: 4px;
    -moz-box-shadow: 1px 1px 5px #cfcfcf inset;
    border: 1px solid #cfcfcf;
    vertical-align: middle;
    background-color: transparent;
}
option {
    background-color: #fef5e6;
    border-bottom: 1px solid #ebdac0;
    border-right: 1px solid #d6bb86;
    border-left: 1px solid #d6bb86;
}
option:hover {
    cursor: pointer;
}

But when it comes to IE, you have to disable the background color on the option if you don't want it to display when the option menu isn't pulled down. And, as I said, webkit does its own thing.

arxpoetica
  • 4,841
  • 3
  • 30
  • 35
11

We've found a simple and decent way to do this. It's cross-browser,degradable, and doesn't break a form post. First set the select box's opacity to 0.

.select { 
    opacity : 0;
    width: 200px;
    height: 15px;
}

<select class='select'>
    <option value='foo'>bar</option>    
</select>

this is so you can still click on it

Then make div with the same dimensions as the select box. The div should lay under the select box as the background. Use { position: absolute } and z-index to achieve this.

.div {
    width: 200px;
    height: 15px;
    position: absolute;
    z-index: 0;
}

<div class='.div'>{the text of the the current selection updated by javascript}</div>
<select class='select'>
    <option value='foo'>bar</option>    
</select>

Update the div's innerHTML with javascript. Easypeasy with jQuery:

$('.select').click(function(event)) { 
    $('.div').html($('.select option:selected').val());
}

That's it! Just style your div instead of the select box. I haven't tested the above code so you'll probably need tweak it. But hopefully you get the gist.

I think this solution beats {-webkit-appearance: none;}. What browsers should do at the very most is dictate interaction with form elements, but definitely not how their initially displayed on the page as that breaks site design.

Shaun
  • 311
  • 3
  • 5
10

Here's a little plug if you mostly want to

  • go crazy customizing the closed state of a select element
  • but at open state, you favor a better native experience to picking options (scroll wheel, arrow keys, tab focus, ajax modifications to options, proper zindex, etc)
  • dislike the messy ul, li generated markups

Then jquery.yaselect.js could be a better fit. Simply:

$('select').yaselect();

And the final markup is:

<div class="yaselect-wrap">
  <div class="yaselect-current"><!-- current selection --></div>
</div>
<select class="yaselect-select" size="5">
  <!-- your option tags -->
</select>

Check it out on github.com

choonkeat
  • 5,557
  • 2
  • 26
  • 19
5

You can style to some degree with CSS by itself

select {
    background: red;
    border: 2px solid pink;
}

But this is entirely up to the browser. Some browsers are stubborn.

However, this will only get you so far, and it doesn't always look very good. For complete control, you'll need to replace a select via jQuery with a widget of your own that emulates the functionality of a select box. Ensure that when JS is disabled, a normal select box is in its place. This allows more users to use your form, and it helps with accessibility.

alex
  • 479,566
  • 201
  • 878
  • 984
4

Most of the browsers doesn't support customizing of select tag using css. But I find this javascript which can be used to style select tag. But as usual no support for IE browsers.

http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/ I noticed an error on this that Onchange attribute dosen't work

SRI
  • 41
  • 1
4

I just found this which seems really good.

http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/

Jenny
  • 41
  • 1
2

If have a solution without jQuery. A link where you can see a working example: http://www.letmaier.com/_selectbox/select_general_code.html (styled with more css)

The style-section of my solution:

<style>
#container { margin: 10px; padding: 5px; background: #E7E7E7; width: 300px; background: #ededed); }
#ul1 { display: none; list-style-type: none; list-style-position: outside; margin: 0px; padding: 0px; }
#container a {  color: #333333; text-decoration: none; }
#container ul li {  padding: 3px;   padding-left: 0px;  border-bottom: 1px solid #aaa;  font-size: 0.8em; cursor: pointer; }
#container ul li:hover { background: #f5f4f4; }
</style>

Now the HTML-code inside the body-tag:

<form>
<div id="container" onMouseOver="document.getElementById('ul1').style.display = 'block';" onMouseOut="document.getElementById('ul1').style.display = 'none';">
Select one entry: <input name="entrytext" type="text" disabled readonly>
<ul id="ul1">
    <li onClick="document.forms[0].elements['entrytext'].value='Entry 1'; document.getElementById('ul1').style.display = 'none';"><a href="#">Entry 1</a></li>
    <li onClick="document.forms[0].elements['entrytext'].value='Entry 2'; document.getElementById('ul1').style.display = 'none';"><a href="#">Entry 2</a></li>
    <li onClick="document.forms[0].elements['entrytext'].value='Entry 3'; document.getElementById('ul1').style.display = 'none';"><a href="#">Entry 3</a></li>
</ul>
</div>
</form>
2

Updated for 2014: You don't need jQuery for this. You can fully style a select box without jQuery using StyleSelect. Eg, given the following select box:

<select class="demo">
  <option value="value1">Label 1</option>
  <option value="value2" selected>Label 2</option>
  <option value="value3">Label 3</option>
</select>

Running styleSelect('select.demo') would create a styled select box as follows:

enter image description here

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
1

I created the jQuery plugin, SelectBoxIt, a couple of days ago. It tries to mimic the behavior of a regular HTML select box, but also allows you to style and animate the select box using jQueryUI. Take a look and let me know what you think.

http://www.selectboxit.com

Greg Franko
  • 1,780
  • 15
  • 14
1

You should try using some jQuery plugin like ikSelect.

I tried to make it very customizable but easy to use.

http://github.com/Igor10k/ikSelect

i10k
  • 11
  • 1
1

This seems old but here a very interesting plugin - http://uniformjs.com

MayThrow
  • 2,159
  • 4
  • 24
  • 38
0

this on uses the twitter-bootstrap styles to turn selectin dropdown menu https://github.com/silviomoreto/bootstrap-select

anacarolinats
  • 667
  • 8
  • 19
-1

Simple solution is Warp your select box inside a div, and style the div matching your design. Set opacity:0 to select box, it will make the select box invisible. Insert a span tag with jQuery and change its value dynamically if user change drop down value. Total demonstration shown in this tutorial with code explanation. Hope it will solve your problem.

JQuery Code looks like similar to this.

 <script>
   $(document).ready(function(){
     $('.dropdown_menu').each(function(){
       var baseData = $(this).find("select option:selected").html();
       $(this).prepend("<span>" + baseData + "</span>");
     });

     $(".dropdown_menu select").change(function(e){
       var nodeOne = $(this).val();
       var currentNode = $(this).find("option[value='"+ nodeOne +"']").text();
       $(this).parents(".dropdown_menu").find("span").text(currentNode);
     });
   });
</script>
fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
user3716078
  • 109
  • 1
  • 4