views:

13366

answers:

6

The issue:

Some of the items in the select require more than the specified width of 145px in order to display fully.

Firefox behavior: clicking on the select reveals the dropdown elements list adjusted to the width of the longest element.

IE6 & IE7 behavior: clicking on the select reveals the dropdown elements list restricted to 145px width making it impossible to read the longer elements.

The current UI requires us to fit this dropdown in 145px and have it host items with longer descriptions.

Any advise on resolving the issue with IE?

The top element should remain 145px wide even when the list is expanded.

Thank you!

The css:

select.center_pull {
    background:#eeeeee none repeat scroll 0 0;
    border:1px solid #7E7E7E;
    color:#333333;
    font-size:12px;
    margin-bottom:4px;
    margin-right:4px;
    margin-top:4px;
    width:145px;
}

Here's the select input code (there's no definition for the backend_dropbox style at this time)

<select id="select_1" class="center_pull backend_dropbox" name="select_1">
<option value="-1" selected="selected">Browse options</option>
<option value="-1">------------------------------------</option>
<option value="224">Option 1</option>
<option value="234">Longer title for option 2</option>
<option value="242">Very long and extensively descriptive title for option 3</option>
</select>

Full html page in case you want to quickly test in a browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>dropdown test</title>

<style type="text/css">
<!--
select.center_pull {
    background:#eeeeee none repeat scroll 0 0;
    border:1px solid #7E7E7E;
    color:#333333;
    font-size:12px;
    margin-bottom:4px;
    margin-right:4px;
    margin-top:4px;
    width:145px;
}
-->
</style>
</head>

<body>
<p>Select width test</p>
<form id="form1" name="form1" method="post" action="">
<select id="select_1" class="center_pull backend_dropbox" name="select_1">
<option value="-1" selected="selected">Browse options</option>
<option value="-1">------------------------------------</option>
<option value="224">Option 1</option>
<option value="234">Longer title for option 2</option>
<option value="242">Very long and extensively descriptive title for option 3</option>
</select>
</form>
</body>
</html>
+2  A: 

Here is a little script that should help you out:

http://www.icant.co.uk/forreview/tamingselect/

zac
Thank you. I am wondering if anyone is aware of a javascript-free solution?
aaandre
A: 

Not javascript free i'm afraid, but I managed to make it quite small using jQuery

$('#del_select').mouseenter(function () {

 $(this).css("width","auto");

});

$('#del_select').mouseout(function () {

 $(this).css("width","170px");

});
stukerr
A: 

Hi stukerr,

I like your idea, but it doesn't work fine, when the select makes bigger and I want to select one option, the control lost the focus and goes little again, so I can't select any option.

Do you know how can I solve this behaviour.

Thank you very much. Regards.

I had the same problem... this worked better.http://stackoverflow.com/questions/73960/dropdownlist-width-in-ie/912435#912435
davekaro
A: 

I found a pretty straightforward fix for this. In the html element add these properties:

onmouseover="autoWidth(this)" onblur="resetWidth(this)"

So whenever user clicks on that the width will automatically expand, and user moves out of the select box, the width will be reset to original.

TheOnlyOne
Thank you for the reply.
aaandre
A: 

Not javascript free, I am afraid too and my solution do require a js library, however, you can only use those files which you need rather than using them all, maybe best suited for those who are already using YUI for their projects or deciding which one to use. Have a look at: http://ciitronian.com/blog/programming/yui-button-mimicking-native-select-dropdown-avoid-width-problem/

My blog post also discusses other solutions as well, one is referenced back to here on stackoverflow, why I went back to create my own SELECT element is because of simple reason, I don't like mouseover expand events. Maybe if that helps anyone else too!

Hammad Tariq
A: 

A different approach: 1) instead of a select make it an edit box, disabled so noone can enter anything manually or change contents after selection 2) another hidden edit to contain an id of a selected option (explained below) 3) make a button [..] and script it to show that div below 4) make a hidden div with absolute position under or near the edit box 5) make that div to contain a select with style size="6" (to show 6 options and a scrollbar rather than a drop-down list) and a button "select" and maybe "cancel" 6) Do not style width so the whole thing will assume width of the widest option or the button plus maybe some padding of your choice 7) script the "select" button to copy id of the selected option to the hidden edit box and it's value to the visible one, also to hide the div again.

4 simple javascript commands total.

Kitet