Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set width of dropdown element in HTML select dropdown options

I am working on a website that involves automatically populating a select box using a PHP script. This all works fine except the problem is that what I am using to populate the text box have very long titles (they are journal articles and presentation titles). The dropdown box extends to the width of the longest element, which stretches off the edge of the screen, therefore making the scrollbar impossible to reach. I have tried various methods of trying to manually set the dropdown box using CSS to a particular width, but so far to no avail. The best I have accomplished it to set the Select box to a certain width but the dropdown menu itself is much, much wider.

Any tips on this would be greatly appreciated.

EDIT:

It turns out that the following CSS line works in all of the primary browsers except for Google Chrome (which is what I was testing the page in). If a solution for Chrome is known, that would be good to know about.

select, option { width: __; } 
like image 610
Greg Zwaagstra Avatar asked Nov 09 '09 16:11

Greg Zwaagstra


People also ask

How do you increase the width of a selection box in HTML?

Answer: Use the CSS :focus pseudo-class By default the size of the <select> element is depend on the size of the largest <option> text. However, sometimes it is useful to set a fixed width to the select box and increase its size back to the original size when the user tries to select some option (i.e. on focus).

How do I style a dropdown in HTML?

HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element. Use a container element (like <div>) to create the dropdown content and add whatever you want inside of it. Wrap a <div> element around the elements to position the dropdown content correctly with CSS.


2 Answers

I find the best way to handle long dropdown boxes is to put it inside a fixed width div container and use width:auto on the select tag. Most browsers will contain the dropdown within the div, but when you click on it, it will expand to display the full option value. It does not work with IE explorer, but there is a fix (like is always needed with IE). Your code would look something like this.

HTML

<div class="dropdown_container">   <select class="my_dropdown" id="my_dropdown">     <option value="1">LONG OPTION</option>     <option value="2">short</option>   </select> </div> 

CSS

div.dropdown_container {     width:10px; }  select.my_dropdown {     width:auto; }  /*IE FIX */ select#my_dropdown {     width:100%; }  select:focus#my_dropdown {     width:auto\9; } 
like image 160
None Avatar answered Sep 23 '22 03:09

None


You can style (albeit with some constraints) the actual items themselves with the option selector:

select, option { width: __; } 

This way you are not only constraining the drop-down, but also all of its elements.

like image 21
Josh Leitzel Avatar answered Sep 20 '22 03:09

Josh Leitzel