最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML select dropdown menu with max-height of 100% and size automatic? - Stack Overflow

programmeradmin3浏览0评论

A dropdown menu made using an HTML select tag has 152 options, but this large number forces some options off the webpage view on most monitors if I make the size 152. I set the max-height of the select to 100% guessing that would limit the size to the maximum number that can fit in the div. However, for reasons I do not understand, it still displays all 152 options with some below the screen view.

<select name="school" size="152"  style="max-height: 100%;">
...
</select>

Are there ways to have select tags display as many options as they can fit onto the screen ( 100% height ) listed out, but force scrolling to get to any options that would be displayed below the screen ( past 100% height ) ?

A dropdown menu made using an HTML select tag has 152 options, but this large number forces some options off the webpage view on most monitors if I make the size 152. I set the max-height of the select to 100% guessing that would limit the size to the maximum number that can fit in the div. However, for reasons I do not understand, it still displays all 152 options with some below the screen view.

<select name="school" size="152"  style="max-height: 100%;">
...
</select>

Are there ways to have select tags display as many options as they can fit onto the screen ( 100% height ) listed out, but force scrolling to get to any options that would be displayed below the screen ( past 100% height ) ?

Share Improve this question edited Mar 24, 2024 at 12:41 Pat Myron asked Jan 2, 2015 at 8:15 Pat MyronPat Myron 4,6582 gold badges27 silver badges51 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

If you can limit the required support to IE9+, you can use viewport units (vh) to force the select to always be exactly the height of the viewport, using this CSS:

select {
    display: inline-block;
    height: 100vh;
}

You also have to set a size attribute of at least 2 on the select itself. As long as you set a reasonable fallback size option on the select, it will work in a limited fashion on older browsers, which will simply ignore the 100vh value and render the select at it's default height.

That's it! The only real drawback is that the list will always be the height of the screen, it won't automatically shrink if there are fewer items.

Working fiddle showing it in action.

I would say no without some sort of jQuery replacement plugin. I've just checked Firefox and also Safari and FF sets a height on the opened list and safari just goes to the bottom of the browser window, both by default.

Try Chosen.js, it really is good and then you can style it from there: http://harvesthq.github.io/chosen/ It even provides options search and other cool features.

发布评论

评论列表(0)

  1. 暂无评论