Bold second item of a selectOneMenu

天大地大妈咪最大 提交于 2019-12-02 02:14:08

The HTML <option> element as generated by <f:selectItems> allows for very little fine-grained styling and the CSS support is browser-dependent. You could use the CSS3 :nth-child pseudoselector. E.g.

<h:selectOneMenu value="#{someBean.cityId}" styleClass="cities">
    <f:selectItems value="#{addressBean.stateList}" />
</h:selectOneMenu>

with

.cities option:nth-child(2) {
    font-weight: bold;
}

But this doesn't work in all browsers. Only Firefox eats that, but MSIE and Chrome not. The latter two doesn't do that because they don't allow setting font-weight on the option. But they allows you to change the (background) color by color or background-color:

.cities option:nth-child(2) {
    background-color: pink;
}

This works in all CSS3 capable browsers so far (i.e. thus not in MSIE8 or older).

If you want best cross browser compatibility, you'd need to replace the <select> by an <ul><li> along with a good bunch of CSS/JS code to make it look like a real dropdown. You could then style the <li> elements individually. You could throw in some jQuery plugin or to look for a 3rd JSF component library. PrimeFaces 3.0 has a <p:selectOneMenu> component which does exactly that.

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