简体   繁体   English

设置select2的高度

[英]Set height of select2

How to increase the height of the select2 suggestion dropdown? 如何增加select2建议下拉菜单的高度

I tried the idea proposed at https://github.com/select2/select2/issues/144 : 我尝试了https://github.com/select2/select2/issues/144提出的想法:

.select2-results {
  max-height: 500px;
}

However, that does not work. 但是,这不起作用。 If I instead set the min-height property, the size is increased, however, I do not want to have a 500px huge dropdown if there are only 1 or 2 items...so min-height is not really an option. 如果我改为设置min-height属性,那么大小会增加,但是,如果只有1或2个项目,我不希望有500px的巨大下拉列表...因此min-height并不是真正的选择。

I want the dropdown to dynamically adjust its height to the content, but increase its height up to 500px before a scrollbar is displayed. 我希望下拉菜单动态调整其高度以适应内容,但是在显示滚动条之前将其高度增加到500px。

Try to use: 尝试使用:

.select2-results__options {
   max-height: 500px;
}

If that doesn't work try to add !important so it overwrite it's default max-height (try both methods, yours and mine). 如果这样不起作用,请尝试添加!important以覆盖默认的max-height(尝试使用您的方法和我的方法)。

I think you need to set both the .select2-drop and .select2-results classes as follows: 我认为您需要同时设置.select2-drop.select2-results类,如下所示:

.select2-drop {
    max-height: /* Your value here */;
    overflow: scroll;
}

.select2-results {
    max-height: 100%;
}

This ensures that the results are not limited in height and that the dropdown is still usable. 这样可以确保结果的高度不受限制,并且下拉菜单仍然可用。 If you test it you should see that it works for variable list lengths no matter how you set the max-height on .select2-drop . 如果您对其进行测试,则无论您如何在.select2-drop上设置max-height,都应该看到它适用于可变列表长度。 Shorter lists should fit content and long lists should scroll within that limit. 较短的列表应适合内容,而较长的列表应在该限制内滚动。

None of the answers above worked for me, but this did. 上面的答案都不对我有用,但是确实可以。 I'm putting it here for future reference on google : 我将其放在此处以供Google将来参考:

.select2-container--default .select2-results > .select2-results__options {
    max-height: 400px;
    min-height: 400px;
    overflow-y: auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM