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?
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.
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).
. 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 .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With