views:

286

answers:

5

Here is what I got:

<select id="box1">
   <option>ABCDEFG</option>
</select>  

<select id="box2">
   <option>ABCDEFGHIJKLMNO</option>
</select>

I have 2 different Drop Down lists. Since the width of a drop down list depends on the width of the longest text in the option, I end up with 2 drop down lists with 2 different widths. This makes my webpage look goofy.

What I want is to set it so that both of my drop down lists will have the same width (I'd prefer the width to be very long, so that even the longest item won't be truncated).

+4  A: 

Length referring to number of options visible?? If that is the case use size. Here is the documentation. Let me know if you mean any different.

After EDIT: Set the width for the select. But it would be hard to set width to show only particular length. Second Edit: You can't do that using CSS, you got to use javascript. On every change of the select, you have to see which of the two elements has maximum width and set the same to the other.

Teja Kantamneni
just update my post ... sorry for being vague. It is not `size` that I want to use. I want to truncate the length of 1 options, instead of the number of option in the drop down list
Harry Pham
Just update my post again.
Harry Pham
Can I not just set the width to be really long? That way I wont have to worry about which element is longer
Harry Pham
You can do that too. But when the values are small, they may look ugly.
Teja Kantamneni
A: 

You need to use JavaScript after the page loads (give your list id="listA"):

document.getElementById('listA').length = 20

This will effectively truncate it at 20 items.

Diodeus
You don't *need* to use Javascript at all. In addition, length is the number of OPTIONS in the SELECT... not the number of displayed OPTIONS
Dancrumb
just update my code, sorry for being vague :(
Harry Pham
A: 
dev-null-dweller
I am not sure what you try to say here. Please reread my post. I think you misunderstand what I want.
Harry Pham
This is not what @HarryPham has requested (in either edit). Please read the question.
Pat
+2  A: 

You can use CSS to set the width of your <select> element, but it’ll be difficult to get the width to match a given number of characters exactly.

Why would you want the option’s text to be clipped?

Paul D. Waite
just updated my post.
Harry Pham
simple of effective. Thank you.
Harry Pham
Ah, gotcha. Yup, the solution is to set them both to a very wide width. Some browsers will even be clever and allow the expanded state (i.e. the bit that actually drops down) to be wider than the width you set, so that no text is clipped, whilst keeping the elements the same width when not expanded.
Paul D. Waite
+3  A: 

Use the following line.It will fix the width of the drop down list.

<select id='box1' style='width: 200px;'>

<select id='box2' style='width: 200px;'> 

If you want to mention the width using css,you can use the following code.

#container select {
    width: 150px;
}
rekha_sri
Yeah that is the answer. Thank you very much, however since Paul D Waite answer it first, I have to give credit to him. :)
Harry Pham