Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change css font-family for separate options in select tag

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?

like image 909
Dandy Avatar asked Jan 12 '11 19:01

Dandy


4 Answers

Give the font name to option value. Then you can set them all with jquery.

Here is an example:

You can add new font easily like this:

<option value="myFontName">My Font Name</option>

$("#selectfont").each(function(){
  $(this).children("option").each(function(){
    $(this).css("fontFamily",this.value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<select id="selectfont">
<option value="tangerine">Tangerine</option>
<option value="tahoma">Tahoma</option>
<option value="times new roman">Times New Roman</option>
</select>
like image 153
Nezih Avatar answered Nov 06 '22 05:11

Nezih


Maybe you can use javascript? Can you try this ?

<script>
myObj = document.getElementById("tahoma");
myObj.style.font = "Tahoma";
myObj.style.color = "red";

// Change anything else that you like!
</script>

<option id="tahoma .....>
like image 38
Eray Avatar answered Sep 25 '22 18:09

Eray


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>
like image 9
Fakhr Alam Avatar answered Nov 06 '22 06:11

Fakhr Alam


You cannot set the font of the <option> tag but you could create a list with specific styles (as you would expect)

Here's everything I've been trying:

$("select.js option:eq(0), ul li:eq(0)").css("fontFamily", "tangerine");
$("select.js option:eq(1), ul li:eq(1)").css("fontFamily", "tahoma");
$("select.js option:eq(2), ul li:eq(2)").css("fontFamily", "times new roman");
h1 {
  font-size: 1.2em;
  margin-top: 10px;
  color: #777;
}
.tangerine {
  font-family: tangerine;
}
.tahoma {
  font-family: tahoma;
}
.timesnewroman {
  font-family: times new roman;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<h1>Set with jQuery</h2>
<select id="js">
    <option>Tangerine</option>
    <option>Tahoma</option>
    <option>Times New Roman</option>
</select>
<div>&nbsp;</div>
<h1>Set with CSS</h2>
<select id="css">
    <option class="tangerine">Tangerine</option>
    <option class="tahoma">Tahoma</option>
    <option class="timesnewroman">Times New Roman</option>
</select>
<div>&nbsp;</div>
<h1>List</h2>
<ul>
    <li>Tangerine</li>
    <li>Tahoma</li>
    <li>Times New Roman</li>
</ul>
like image 7
hunter Avatar answered Nov 06 '22 06:11

hunter