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.
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:
It's far from pretty but it might work for you if the optgroup doesn't.
I was able to accomplish this using the NO-BREAK SPACE unicode character. http://www.fileformat.info/info/unicode/char/00a0/index.htm
Copy-paste the character from that page into code and voila: https://jsfiddle.net/fwillerup/r9ch988h/
(
didn't work for me because I was using a library for fancy select boxes that would inject them verbatim.)Try using  
Prepending Non breaking space ( ) did not work for me.
I prepended the following:
String.fromCharCode(8194);
Just for the sake of visitors, I feel I should share this solution I devised: http://jsfiddle.net/n9qpN/
Decorate the options with the level class
We can now use jQuery to reformat the content of the
select
elementThis can be extended to any level
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 customSELECT
elements, you'll need to employ JavaScript or re-create something that behaves like aSELECT
but is made of a bunch ofDIV
s and checkboxes or something to that extend.Having said that, I think what you're looking for are
OPTGROUP
s: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
OPTGROUP
s.