Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the width of select tag?

Tags:

html

css

I have a list of countries, some with a very long name:

<select name=countries>  <option value=af>Afghanistan</option>  <option value=ax>Åland Islands</option>  ...  <option value=gs>South Georgia and the South Sandwich Islands</option>  ... </select> 

By default, the select box would be as long as the longest option in the list. I want to create a select box such that it exhibits the default behaviour when viewed from a wide browser window, but fit in nicely to 90% of container width when viewed from a narrow browser window, smaller than the length of the longest option.

I tried min-width: 90%;, but it didn't work. Can this be done by CSS styling alone?

like image 786
Question Overflow Avatar asked May 20 '12 07:05

Question Overflow


People also ask

How do you set the size of a select tag in HTML?

Definition and UsageThe size attribute specifies the number of visible options in a drop-down list. If the value of the size attribute is greater than 1, but lower than the total number of options in the list, the browser will add a scroll bar to indicate that there are more options to view.

How do you increase the width of a selection 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 reduce the width of a dropdown menu?

. dropdown-menu has min-width: 160px; and min-width overrides width so you can not change width you can use min-width instead of width . It works. You should call bootstrap lib and jquery .


2 Answers

USE style="max-width:90%;"

<select name=countries  style="max-width:90%;">  <option value=af>Afghanistan</option>  <option value=ax>Åland Islands</option>  ...  <option value=gs>South Georgia and the South Sandwich Islands</option>  ... </select>   

LIVE DEMO

like image 178
Krishanu Dey Avatar answered Oct 07 '22 05:10

Krishanu Dey


You've simply got it backwards. Specifying a minimum width would make the select menu always be at least that width, so it will continue expanding to 90% no matter what the window size is, also being at least the size of its longest option.

You need to use max-width instead. This way, it will let the select menu expand to its longest option, but if that expands past your set maximum of 90% width, crunch it down to that width.

like image 29
animuson Avatar answered Oct 07 '22 05:10

animuson