css select dropdown bold on some <option>'s

梦想的初衷 提交于 2019-12-12 07:47:22

问题


On a select dropdown, I need to make certain items 'strong/bold'.

How can I do this?

Example of what I ideally require:

<option value="#"><strong>Andorra</strong></option> 
<option value="#">--Grandvalira</option> 
<option value="#">--Vallnord</option> 
<option value="#"><strong>Austria</strong></option> 
<option value="#">--Amadé</option> 
<option value="#">--Bad Kleinkirchheim</option> 
<option value="#">--Mallnitz</option>

回答1:


you could use :nth-child(N)

option:nth-child(1), option:nth-child(4) {
    font-weight:bold;
}

Demo: http://jsfiddle.net/Sotiris/sqshN/

Find more info and browser support for this pseudo-class at http://reference.sitepoint.com/css/pseudoclass-nthchild




回答2:


You actually can't.

The closest thing (you can't choose a bold item)

 <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>

Which gives you this:

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_optgroup

You can also build one of your own but it won't be input an input tag (you should use inputs of your own).




回答3:


Using css in the works as a quicker, easier alternative

<option value="value" style="font-weight: bold;">SELECT ME</option>



回答4:


You could do it with jQuery.

  $('#Your select').append($("<option></option>").attr.css("font-weight" , "bold").html("Bold Text"));

You'd have to add some logic to determine which options to bold, obviously.




回答5:


This also works (Firefox 50 and Chrome 55). Items 3 and 5 are shown in bold

   <style>
    .bld {font-weight:bold;}
   </style>
   <select>
    <option value = "1">Kanakangi
    <option value = "2">Ratnangi
    <option class = "bld" value = "8">Hanumatthodi
    <option value = "9">Dhenuka
    <option class = "bld" value = "15">Mayamalavagowla
    <option value = "16">Chakravaaham
  </select>


来源:https://stackoverflow.com/questions/9078927/css-select-dropdown-bold-on-some-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!