Change css font-family for separate options in select tag

Dandy picture Dandy · Jan 12, 2011 · Viewed 31.4k times · Source

I don't know if this is possible, and if not, if someone can throw out optional ideas, but I'm attempting to display a drop down of different fonts (specifically, font's from Google's font directory) in a select tag. In the drop down, I'm trying to show a preview by styling each option with the font it represents

<option name="Tangerine" style="font-family:'Tangerine', verdana;">Tangerine</option>

This doesn't seem to be working, though. Any clues why or how to fix it?

Answer

Fakhr Alam picture Fakhr Alam · Aug 23, 2012

You should wrap each option tag in an optgroup tag and then style that as:

<select>

  <optgroup style="font-family:arial">
    <option>Arial</option>
  </optgroup>

  <optgroup style="font-family:verdana">
    <option>veranda</option>
  </optgroup>

  <optgroup style="font-family:other">
    <option>other</option>
  </optgroup>

</select>