Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the max amount of items to show in an HTML select box

Tags:

html

css

I have a <select> box with some year numbers in them, ranging 50 years back, and currently when I click on the box the options will reach to the very bottom of the screen, is there any way to set the max amount of options a select box should show before resorting to a scroll bar to show the rest? I could not find anything about this on Google, the only thing that came close is the size attribute, which is really not what I want.

like image 420
Aistina Avatar asked Feb 26 '09 11:02

Aistina


People also ask

How do I increase the length of the 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 can I limit the visible options in an HTML select dropdown?

It is not possible to limit the number of visible elements in the select dropdown (if you use it as dropdown box and not as list). But you could use javascript/jQuery to replace this selectbox with something else, which just looks like a dropdown box. Then you can handle the height of dropdown as you want.

How do you select size 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.


1 Answers

I believe the reason why you can't do that (sorry) is that these are usually implemented as OS native controls, which also means they behave differently in different browsers/platforms.

like image 149
Assaf Lavie Avatar answered Oct 14 '22 09:10

Assaf Lavie