20

I am trying to style a select option dropdown list. Is it possible to make the font-sizes of the options different from the default value? For example, the default:

-- Select Country --  

Would be size 7pt; and one of the options,

Georgia

Would be size 13pt.

This is my dropdown list:

.select_join {
  width: 170px;
  height: 28px;
  overflow: hidden;
  background: url('http://s24.postimg.org/lyhytocf5/dropdown.png') no-repeat right #FEFEFE;
  border: #FEFEFE 1px solid;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  -webkit-box-shadow: inset 0px 0px 10px 1px #FEFEFE;
  box-shadow: inset 0px 0px 10px 1px #FEFEFE;
}
.select_join select {
  background: transparent;
  width: 170px;
  font-size:7pt;
  color:grey;
  border: 0;
  border-radius: 0;
  height: 28px;
  -webkit-appearance: none;
}
.select_join select:focus {
  outline: none;
}
<div style="background-color:pink;height:150px; text-align:center;">
  <br/>
  <div class="select_join" style="margin-left:15px">
    <select name="txtCountry">
      <option>-- Select Country --</option>
      <option value="1">Georgia</option>
      <option value="2">Afghanistan</option>
    </select>
  </div>
</div>

See also my demo on JSFiddle.

Unfortunately, it works only on Firefox. Could it be that others browser don't support styling of <option> elements?

Browsers I tested on:

  • Chrome: Version 27.0.1453.116 m
  • IE: 10
  • Firefox: 22.0
TylerH
  • 20,799
  • 66
  • 75
  • 101
user1128331
  • 707
  • 4
  • 10
  • 20

6 Answers6

19

We need a trick here...

Normal select-dropdown things won't accept styles. BUT. If there's a "size" parameter in the tag, almost any CSS will apply. With this in mind, I've created a fiddle that's practically equivalent to a normal select tag, plus the value can be edited manually like a ComboBox in visual languages (unless you put readonly in the input tag).

A simplified example:

<style>

    /* only these 2 lines are truly required */
    .stylish span {position:relative;}
    .stylish select {position:absolute;left:0px;display:none}

    /* now you can style the hell out of them */
    .stylish input    { ... }
    .stylish select   { ... }
    .stylish option   { ... }
    .stylish optgroup { ... }

</style>
...
<div class="stylish">
    <label> Choose your superhero: </label>
    <span>
        <input onclick="$(this).closest('div').find('select').slideToggle(110)">
        <br>
        <select size=15 onclick="$(this).hide().closest('div').find('input').val($(this).find('option:selected').text());">

            <optgroup label="Fantasy"></optgroup>
            <option value="gandalf">Gandalf</option>
            <option value="harry">Harry Potter</option>
            <option value="jon">Jon Snow</option>

            <optgroup label="Comics"></optgroup>
            <option value="tony">Tony Stark</option>
            <option value="steve">Steven Rogers</option>
            <option value="natasha">Natasha Romanova</option>

        </select>
    </span>
    <!--

        For the sake of simplicity, I used jQuery here.
        Today it's easy to do the same without it, now
        that we have querySelector(), closest(), etc.

    -->
</div>

A live example:

https://jsfiddle.net/7ac9us70/1052/

Note 1: Sorry for the gradients & all fancy stuff, no they're not necessary, yes I'm showing off, I know, hashtag onlyhuman, hashtag notproud.

Note 2: Those <optgroup> tags don't encapsulate the options belonging under them as they normally should; this is intentional. It's better for the styling (the well-mannered way would be a lot less stylable), and yes this is painless and works in every browser.

dkellner
  • 8,726
  • 2
  • 49
  • 47
  • That's amazing! Do you know why size=15 is required? – Jay Jul 14 '16 at 04:38
  • It doesn't have to be 15, what matters is that it has a size - that's what makes it render as a listbox, not a single-line field with a button. – dkellner Jul 14 '16 at 06:27
  • Yeah, sorry, should've been clearer. What is it about the size attribute that makes it render as a listbox? Is this documented anywhere? Is it a CSS bug? (which means it might be fixed in the future and things relying on it would break). Is it only the size attribute, or will some other attribute (width or height etc) also work? (I can probably easily find out the answer to that last one myself by playing with the fiddle) – Jay Jul 14 '16 at 06:52
  • No, it's a well-documented feature of SELECT and it will definitely not go away. I don't know why the guys implemented it like this - size means how high the list should be when it's shown but merely the presence of "size" makes it shown by default. See here: https://developer.mozilla.org/en/docs/Web/HTML/Element/select – dkellner Jul 14 '16 at 07:03
16

Add a CSS class to the <option> tag to style it: http://jsfiddle.net/Ahreu/

Currently WebKit browsers don't support this behavior, as it's undefined by the spec. Take a look at this: How to style a select tag's option element?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Atropo
  • 12,231
  • 6
  • 49
  • 62
4

Tell the option element to be 13pt

select option{
    font-size: 13pt;
}

and then the first option element to be 7pt

select option:first-child {
    font-size: 7pt;
}

Running demo: http://jsfiddle.net/VggvD/1/

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

try this

http://jsfiddle.net/VggvD/2/

CSS add your code

.select_join option{
    font-size:13px;
}
Falguni Panchal
  • 8,873
  • 3
  • 27
  • 33
0

check this fiddle,

i just edited the above fiddle, its working

http://jsfiddle.net/narensrinivasans/FpNxn/1/

.selectDefault, .selectDiv option
{
    font-family:arial;
    font-size:12px;
}
0
select[value="value"]{
   background-color: red;
   padding: 3px;
   font-weight:bold;
}