HTML Select item with Icons in addition to just text labels – applying the CSS background-style to the HTML OPTION element
We have been using JIRA for our Incident Management the last six
months or so, and one of the UI features in JIRA that suddenly struck
me as being somewhat odd and primarily quite cool at the same time, was
the Issue Type select-item that not only contained text-labels, as HTML
Select items usually do, but also had icons in them:
For
me this was a more or less new HTML feature, and one that I would like
to use myself sometime. So I took a look at the HTML source and – of
course – it turned out to be a CSS trick, using the background-image
style on the OPTION elements in the SELECT. I would never had thought
that this CSS style could also be applied to OPTIONs, so once again it
is proved that you should try out everything, including things you know not to be possible. The HTML source from this JIRA item looked as follows:
<select name="issuetype" id="issuetype"><br /> <option value="1" class="imagebacked" style="background-image: url(/images/icons/bug.gif);" selected><br /> Bug<br /> </option><br /> <option value="2" class="imagebacked" style="background-image: url(/images/icons/newfeature.gif);"><br /> New Feature<br /> </option><br /> <option value="3" class="imagebacked" style="background-image: url(/images/icons/task.gif);"><br /> Task<br /> </option><br /> <option value="4" class="imagebacked" style="background-image: url(/images/icons/improvement.gif);"><br /> Improvement<br /> </option><br /></select><br />
The next step of course is actually using this feature myself in my
own applications and then making the icons dynamic along with the data;
typically the content of a SELECT item will be produced by some backing
bean or other dynamic data source and that bean or that source should
now provide an icon reference in addition to label and value. And it
would be nice to have a JSF component – an extension of UISelectMany or
UISelectOne – that can render the background icon too. The real
challenge is finding some time for this, obviously.
How did you do it ? Gimme the code