views:

126

answers:

4

Styling of items in a option list is easy: use a CSS definition and apply it:

  <select>
  <option>Option 1</option>
  <option style="color: #F00; font-weight: bold; padding-left:2em;">Option 2</option>
  <option>Option 3</option>
  <option style="color: #00F;">Option 4</option>
  <option>Option 5</option>
  </select>

But what is the trick if I just want to highlight parts of a string? For example in this list from the 5th item just the substring "ion 5"? My idea was to use a background image and offset it in a right way. But this seems to be tricky. Has anybody an nifty idea how to do it? Browser would be the current FF. The HTML and style can be be generated on the fly at the backend.

+5  A: 

You can't get that granular with your styles on a standard select list. This would require a dynamic replacement (created and controlled with and by Javascript) to stand in place of your standard select, and feed values back and forth.

For instance, the following illustrates my point a bit more clearly. You would have to have a completely different set of HTML elements that you can style in this fashion, for instance, a div containing an unordered list. Note that option 6 is styled with bold text.

<div id="list-simulation">
  <ul>
    <li>Option 4</li>
    <li class="selected">Opt<strong>ion 5</strong></li>
    <li>Option 6</li>
  </ul>
</div>

Attached Javascript-logic intercepts clicks on the list-items, and passes them on to the hidden select menu, which then selects the corresponding option so that form-submission carries over the users decisions.

<select name="real-list">
  <option value="1">Option 4</option>
  <option value="2" selected="selected">Option 5</option>
  <option value="3">Option 6</option>
</select>
Jonathan Sampson
Can you clarify a bit how we can replace the particular text with Javascript? Here only the string 'ion' must carry some styles and must be inside the list ..
infant programmer
You would have to create a completely new element that *looks* like a select list, but isn't. It would be controlled via Javascript. This is the only way you could achieve the visual results you are looking for.
Jonathan Sampson
ah!! ok .. it's clarified thanks ..
infant programmer
Also, if the list only allowed single selection, it would likely be acceptable to just use a hidden form value and update that instead of creating the entire select box, IMO.
stealthdragon
Usually, progressive-enhancement-style, you replace a real `<select>` box with a JavaScript proxy, so it still works when JS isn't available (just without the nice formatting).
bobince
@bobince You are right. I always forget about those non-js users ;)
stealthdragon
A: 

I don't believe this can be done with CSS alone. You'd have to use JavaScript.

Darrell Brogdon
I don't think it would be possible with javascript either.
stealthdragon
@stealthdragon, Darrell is likely talking about creating an alternative element in place of this `select`.
Jonathan Sampson
Right. You'd have to replace the option text with HTML. So 'Option 5' would become `Opt<span style="font-weight:bold;">ion 5</span>`.
Darrell Brogdon
@Darrel, That doesn't work ..Option doesn't allow Span or DIV as child elements ..
infant programmer
Yeah, I'm not sure what Darrell originally meant because "use JavaScript" is pretty vague. The creation of a pseudo-select box is likely the only solution that would work (as mentioned in Jonathan's answer). The only consideration is if it worth the effort to create such a control. Also, I would say that non-native controls are more likely to have usability problems (or, at the very least, usability differences).
stealthdragon
It *can be done* with javascript, but not simply inject style into `<option>`. You will require to tranform the entire `<select>` into a `<div>` tree that mimics behavior of a drop down list. This way you can further stylise your drop down list and save form submission when javascript is disabled.
rockacola
@rockacola :: Yes that is the conclusion ..
infant programmer
+1  A: 

This is not possible with HTML and CSS .. and neither with simple Javascript .. whatever you may use (I mean dynamic/static content) Ultimately .. you have to write style within a span or a div element .. here the restriction is style has to be done to the list and Option doesn't allow Span or DIV as child elements ..

The alternative is (as suggested by Jonathan) to use completely different javascript event .. which behaves like <Select> but isn't select ..

But in all the ways it requires more effort (burden on designer and also on browser) than usual because it is not the basic requirement of a drop-down list ..

infant programmer
+3  A: 

The closest I can come to anything near is still pathetic. I'll post it nevertheless.

Using the the positioning of a background image:

<select>
  <option></option>
  <option>Option 1</option>
  <option style="color: #000; 
        background: #ffffff 
            url(http://i49.tinypic.com/15ybyaw.jpg) 
            repeat-y 2em 0px"
  >Option 2</option>
  <option>Option 3</option>
</select>

It only renders in FF3.5 and not on IE, Chrome etc. And to get the highlight position accurate is another piece of tricky business. Probably works better with fixed-width fonts.

Here's the FF screenshot:

On FF

o.k.w
That's what I wrote: "My idea was to use a background image and offset it in a right way." :) But the problem is the right positioning and width of the image ...
hansi
+1 bonus point for laughs
bobince
@hansi: Yes, you did mention using background image though your codes didn't. Anyway my post is to share my findings, not to claim ownership of the idea. Just hope it helps in anyway. :P
o.k.w
@o.k.w yes I agree with you ..
infant programmer