59

I'm trying to set the style of an option in a select dropdown menu in Google Chrome. It works in all browsers except IE9 and Chrome.

option.red {
    background-color: #cc0000; 
    font-weight: bold; 
    font-size: 12px; 
    color: white;
}
<select name="color">
    <option class="red" value="red">Red</option>
    <option value="white">White</option>
    <option value="blue">Blue</option>
    <option value="green">Green</option>
</select>

Without using JavaScript, is there a way to set style to the options in Google Chrome? Once selected the background color does not display.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Mike
  • 2,195
  • 4
  • 15
  • 10
  • 1
    I simply want the option code to work in Chrome. – Mike May 04 '11 at 17:08
  • 1
    Works fine for me in chrome, just it looses the red background when selected. – Andrea May 04 '11 at 17:08
  • Only the font-changes works in Safari on mac (font-size + font-weight) – peirix May 04 '11 at 17:17
  • Works fine for me too (Chrome 11) **edit** Actually only the colors work. The `font-size` and `-weight` do nothing. – Rudie May 04 '11 at 17:18
  • 2
    This is not working for me in Chrome. :-\ I'm using chrome for mac. version 11 – Mike May 04 '11 at 17:37
  • 1
    Long story short, as your operating system renders the drop down, that can be the difference in which set of styles show up. It seems like Chrome on OS X doesn't allow for ANY customization while Chrome on Windows 7/8 allows for extensive customization. – vpiTriumph Dec 18 '13 at 21:16
  • I have found this [article](https://css-tricks.com/dropdown-default-styling/) and I have tested an example. No problem in Firefox, but Chrome is not working. I am stuck with this problem too. – Timbergus Mar 23 '16 at 10:42

6 Answers6

30

Unfortunately, WebKit browsers do not support styling of <option> tags yet, except for color and background-color.

The most widely used cross browser solution is to use <ul> / <li> and style them using CSS. Frameworks like Bootstrap do this well.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Tushar Roy
  • 362
  • 3
  • 7
  • 16
    This is not entirely true. It is actually OS dependent which set of styles the drop down supports. Anecdotally, styling options seems to work in Windows 7/8, and I can say definitively it does not work on OS X (10.8). – vpiTriumph Dec 18 '13 at 21:32
  • modern browsers DO support styling the `OPTION` tag - add a style attribute to the tag – Jimmery Jan 24 '17 at 14:13
  • There is a way to change the font-size of ` – Flimm Jul 08 '20 at 12:31
14

It's a choice (from browser devs or W3C, I can't find any W3C specification about styling select options though) not allowing to style select options.

I suspect this would be to keep consistency with native choice lists.
(think about mobile devices for example).

3 solutions come to my mind:

  • Use Select2 which actually converts your selects into uls (allowing many things)
  • Split your selects into multiple in order to group values
  • Split into optgroup
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
  • 3
    You are correct. The explicit choice was made in the CSS2 era because, at that time, browsers used platform UI widgets to render browser UI widgets, and platform UI widgets often supported much more limited styling than CSS would want. Today, FF and IE have moved on to self render all their controls; the phone browsers except mobile FF all have this problem; Chrome is in the process of getting there; Desktop Safari and Opera are falling behind. – John Haugeland Jan 10 '15 at 16:00
  • I agree with Pierre and @John. I've replaced all select boxes on an enormous client site with jQuery UI's selectMenu since JQUI was already available. – Ben Racicot Jan 14 '15 at 20:50
3

I have a workaround using jquery... although we cannot style a particular option, we can style the select itself - and use javascript to change the class of the select based on what is selected. It works sufficiently for simple cases.

$('select.potentially_red').on('change', function() {
 if ($(this).val()=='red') {
  $(this).addClass('option_red');
 } else {
  $(this).removeClass('option_red');
 }
});
$('select.potentially_red').each( function() {
 if ($(this).val()=='red') {
  $(this).addClass('option_red');
 } else {
  $(this).removeClass('option_red');
 }
});
.option_red {
    background-color: #cc0000; 
    font-weight: bold; 
    font-size: 12px; 
    color: white;
}
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- The js will affect all selects which have the class 'potentially_red' -->
<select name="color" class="potentially_red">
    <option value="red">Red</option>
    <option value="white">White</option>
    <option value="blue">Blue</option>
    <option value="green">Green</option>
</select>

Note that the js is in two parts, the each part for initializing everything on the page correctly, the .on('change', ... part for responding to change. I was unable to mangle the js into a function to DRY it up, it breaks it for some reason

xxjjnn
  • 14,591
  • 19
  • 61
  • 94
2

I actually discovered something recently that seems to work for styling individual <option></option> elements within Chrome, Firefox, and IE using pure CSS.

Maybe, try the following:

HTML:

<select>
    <option value="blank">Blank</option>
    <option class="white" value="white">White</option>
    <option class="red" value="red">Red</option>
    <option class="blue" value="blue">Blue</option>
</select>

CSS:

select {
    background-color:#000;
    color: #FFF;
}

select * {
    background-color:#000;
    color:#FFF;
}

select *.red { /* This, miraculously, styles the '<option class="red"></option>' elements. */
    background-color:#F00;
    color:#FFF;
}

select *.white {
    background-color:#FFF;
    color:#000;
}

select *.blue {
    background-color:#06F;
    color:#FFF;
}

Strange what throwing caution to the wind does. It doesn't seem to support the :active :hover :focus :link :visited :after :before, though.

Example on JSFiddle: http://jsfiddle.net/Xd7TJ/2/

LeoV117
  • 114
  • 12
  • 20
    This does not work in a Mac using Chrome. Tested on Mac OSX version 10.9.2. – Solomon Closson Apr 13 '14 at 07:54
  • 1
    I'm running Windows 7 and this seems to work fine. I don't actually like the approach, since it only seems to support the `background-color` and `color` CSS attributes. – LeoV117 Apr 13 '14 at 15:28
  • For chrome in windows 7 it does not color the select box, only the option in the drop down box. So if you pick the coloured option, it has a different colour once selected. – Myforwik Aug 27 '14 at 06:27
  • I have FireFox 58.0.2 on Ubuntu 16.04 64 bit and it does not support options background and color styling. – SaidbakR Feb 27 '18 at 17:59
2

Since version 49+, Chrome has supported styling <option> elements with font-weight. Source: https://code.google.com/p/chromium/issues/detail?id=44917#c22

New SELECT Popup: font-weight style should be applied.

This CL removes themeChromiumSkia.css. |!important| in it prevented to apply font-weight. Now html.css has |font-weight:normal|, and |!important| should be unnecessary.

There was a Chrome stylesheet, themeChromiumSkia.css, that used font-weight: normal !important; in it all this time. It was introduced to the stable Chrome channel in version 49.0.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 3
    This is why I make it a point to grep my codebase for strings like `!important` every couple of days. – BoltClock Nov 30 '15 at 03:29
-2

This question is really multiple questions in one. They are different ways of styling something. Here are links to the questions within this question:

Flimm
  • 136,138
  • 45
  • 251
  • 267