Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a hierarchy of "OPTION"s in a "SELECT" tag

Tags:

My problem is HTML and CSS related. I have a hierarchy type structure that I want to display inside a list. The hierarchy contains Countries, States and Cities (it is three levels deep).

I want to display the list inside a select list, each item type (Country, State, City) must be selectable. The items should appear indented as:

United States - Hawaii -- Kauai - Washington -- Seattle -- Chelan 

The problem is with the indentation. I am trying to use either margin-left or padding-left to indent the tags, which appear correct in FireFox but not in IE7. This is an example of the generated select list:

<select name="Something"> <option style="padding-left: 0">United States</option> <option style="padding-left: 20px">Hawaii</option> <option style="padding-left: 40px">Kauai</option> <option style="padding-left: 20px">Washington</option> <option style="padding-left: 40px">Seattle</option> <option style="padding-left: 40px">Chelan</option> </select> 

I want to achieve consistent indentation across browsers without using CSS hacks.

like image 441
Salman A Avatar asked Jul 18 '09 05:07

Salman A


People also ask

How do you style options in select tag?

You can use inline styles to add custome styling to <option> tags. For eg : <option style="font-weight:bold;color:#09C;">Option 1</option> This will apply the styles to this particular <option> element only. You can also use <option value="" disabled> <br> </option> to add a line-break between the options.

Which is the correct tag to be used for selecting multiple options?

The <select> element has some unique attributes you can use to control it, such as multiple to specify whether multiple options can be selected, and size to specify how many options should be shown at once.

Which tag is used for selection list?

Definition and Usage. The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input.


2 Answers

The rendering of SELECT elements is largely up to the browser, you have very little influence over their presentation. Some browsers obviously allow you more customization than others, IE happens to allow very little (gasp, who'd have thunk ;)). If you need very custom SELECT elements, you'll need to employ JavaScript or re-create something that behaves like a SELECT but is made of a bunch of DIVs and checkboxes or something to that extend.

Having said that, I think what you're looking for are OPTGROUPs:

<select>   <optgroup label="xxx">     <option value="xxxx">xxxx</option>     ....   </optgroup>   <optgroup label="yyy">     ...   </optgroup> </select> 

Every browser will display them differently, but they'll be displayed in a distinctive fashion in one way or another. Note though that officially in HTML4 you can't nest OPTGROUPs.

like image 167
deceze Avatar answered Nov 09 '22 23:11

deceze


deceze way is much better and was my first idea. As an alternative if that doesn't work is that you can use non-breaking spaces in the tag value:

<select>     <option>select me</option>     <option>&nbsp;me indented</option>     <option>&nbsp;&nbsp;even more indentation</option> </select> 

It's far from pretty but it might work for you if the optgroup doesn't.

like image 39
MacAnthony Avatar answered Nov 10 '22 01:11

MacAnthony