Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max-height for select element dropdown (option elements)

Tags:

html

css

How do I limit the height of the dropdown of a select element - so that if the total amount of options is greater than this height - I should get a scroll in the dropdown. I'd be satisfied if I could do this in terms of pixels or number of items.

So say I had the following html markup:

<select>
    <option selected>Select</option>
    <option>This is an option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
</select>

How would I display only say the first 4 options and the rest within a scroll.

This is what I have so far, and it's not working.

like image 620
Danield Avatar asked Nov 13 '12 14:11

Danield


People also ask

How do I increase dropdown height in CSS?

You just need to use the height CSS attribute for the select tag. Show activity on this post. Notice that you have to remove the "." before select and option. This will overwrite any select and option element.

How do I limit the size of a drop-down list in HTML?

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.

Is a select element a drop-down menu?

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).


1 Answers

Searching around on StackOverflow, I came across this. Sadly enough, if you want to keep it a dropdown-box, you cannot achieve what you want in CSS. JavaScript or jQuery will do the trick, as said in the link, or you could use the size attribute on your select tag, but this will break the dropdown-box look.

like image 74
Simon Carlson Avatar answered Nov 16 '22 03:11

Simon Carlson