views:

19

answers:

3

I am trying to create the effect of columns in a dropdown by padding text with whitespace as in this example:

<select style="font-family: courier;">
<option value="1">[Aux1+1] [*]  [Aux1+1]              [@Tn=PP] </option>
<option value="2">[Main]   [*]  [Main Apples Oranges] [@Fu=$p] </option>
<option value="3">[Main]   [*]  [Next NP]             [@Fu=n]  </option>
<option value="4">[Main]   [Dr] [Main]                [@Ty=$p] </option>
</select>

According to this blog, it's possible.

The problem is the whitespace is contracted so that the columsn don't line up. SAme results in FF, IE6 and Chrome.

What am I missing?

+2  A: 

You'll need to use &nbsp; instead of just normal spaces.

For example, you would do this with your option:

<option value="2">[Main]&nbsp;&nbsp;&nbsp;[*]&nbsp;&nbsp;[Main Apples Oranges]&nbsp;[@Fu=$p] </option>
patmortech
Of course. How could I have missed that? Universal HTML rules apply.
Laramie
I was actually loading the dropdownlist in the codebehind of an ASP.Net page, and there is a trick to rendering the   correctly. Code Below.
Laramie
A: 

White space in HTML is contracted by default. Use CSS to select a monospace font (like in the example you're linking to) and style the white space as preformatted.

option {
    font-family: monospace;
    white-space: pre;
}
nikc
Does this work with Select options? I've tried it in Firefox 3 and IE7 and it doesn't appear to.
patmortech
Dangnabbit, it looks like you're right. Using non-breaking spaces seems to work though (` `), so you can go with @patmortech's suggestion.
nikc
A: 

Expanding on the answer by patmortech to render a ListItem in asp.net with &nbsp; instead of &amp;nbsp;

//Pad the columns
string spaceDecode = Server.HtmlDecode("&nbsp;");
for (int i = 0; i < ddl.Items.Count; i++)
{
    ListItem item = ddl.Items[i];
    item.Text = item .Text.Replace(" ", spaceDecode);
}
Laramie