Jquery 包装:断字; 文本溢出:继承; 空白:正常; } $('.select2\u extend\u height')。select2({containercsclass:“wrap”});
我使用以下方法动态调整select2下拉元素的高度,使其与大量选项完美匹配:Jquery 包装:断字; 文本溢出:继承; 空白:正常; } $('.select2\u extend\u height')。select2({containercsclass:“wrap”});,jquery,jquery-select2,Jquery,Jquery Select2,我使用以下方法动态调整select2下拉元素的高度,使其与大量选项完美匹配: $('select').on('select2:open', function (e) { var OptionsSize = $(this).find("option").size(); // The amount of options var HeightPerOption = 36; // the height in pixels every option should be var DropDow
$('select').on('select2:open', function (e) {
var OptionsSize = $(this).find("option").size(); // The amount of options
var HeightPerOption = 36; // the height in pixels every option should be
var DropDownHeight = OptionsSize * HeightPerOption;
$(".select2-results__options").height(DropDownHeight);
});
确保取消设置css中的(默认)最大高度:
.select2-container--default .select2-results>.select2-results__options {
max-height: inherit;
}
(仅在select2的第4版中测试)如果这里有人试图将输入样式和bootstrap4的表单组高度与select2匹配,我就是这么做的
.select2-container{
.select2-selection{
height: 37px!important;
}
.select2-selection__rendered{
margin-top: 4px!important;
}
.select2-selection__arrow{
margin-top: 4px;
}
.select2-selection--single{
border: 1px solid #ced4da!important;
}
*:focus{
outline: none;
}
}
这也将删除v4.0.7的轮廓。
您可以通过覆盖CSS类来增加高度,如以下示例所示:
.select2-container .select2-selection--single {
height: 36px;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 36px;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
height: 36px;
}
我想到了:
.select2,
.select2-search__field,
.select2-results__option
{
font-size:1.3em!important;
}
.select2-selection__rendered {
line-height: 2em !important;
}
.select2-container .select2-selection--single {
height: 2em !important;
}
.select2-selection__arrow {
height: 2em !important;
}
定制Select 2渲染组件的非常简单的方法,只需几行CSS样式:
// Change the select container width and allow it to take the full parent width
.select2
{
width: 100% !important
}
// Set the select field height, background color etc ...
.select2-selection
{
height: 50px !important
background-color: $light-color
}
// Set selected value position, color , font size, etc ...
.select2-selection__rendered
{
line-height: 35px !important
color: yellow !important
}
应用此替代CSS以增加结果框高度
.select2-container--bootstrap4 .select2-results>.select2-results__options {
max-height: 20em;
}
只需添加select2.css
/* Make Select2 boxes match Bootstrap3 as well as Bootstrap4 heights: */
.select2-selection__rendered {
line-height: 32px !important;
}
.select2-selection {
height: 34px !important;
}
如果有多个
选择2
,只需调整其中一个的大小即可。这样做:
获取元素的id:
<div id="skills">
<select class="select2" > </select>
</div>
添加样式
.select2-container .select2-selection--single{
height:0%;
}
我完全按照你说的做了,但是在选择了孩子之后,选择框的高度仍然是一样的。如果选择了一个子项,我希望它展开。我希望使select2框变细。为此,我尝试了几种方法,包括上面的方法,但都没有得到想要的结果。`/$(“#主题”).select2({dropdownAutoWidth:true,dropdownCssClass:“slim select”});//$(“#主题”)。选择2({dropdownAutoWidth:true,containerCss:{'height':'16px','line-height':'16px'});$(“#主题”).select2({dropdownAutoWidth:true,container类:'slim-select');`--这些都不管用。这对我来说很管用,因为我使用了bootstrap,它使它与其他输入完全匹配!实际上,我不希望选项的高度有所不同,我希望在选择一个子项后,实际选择框的高度发生变化。设置
。选择2个选项似乎没有效果。设置ul.select2 results
确实有效。在版本4中,它看起来应该是。select2 results、.select2-results\u选项
现在,使用!重要信息
flag对于v4,您需要_选项,但不需要!重要信息
如果添加容器,例如:.my-select2-wrapper.select2 results>.select2-results\u options
这是最正确的答案,因为它将更改容器的高度,而不是选项。添加以下内容就足够了:.select2 container.select2 choice{height:200px;}若要仅在选择对象后应用它,请在更改事件或类似事件上添加/修改相应的css。这将更改select2输入字段的高度,这不是要求的。请不要进入并修改其他库的源代码文件。每次你想得到一个新版本时,它都会中断。只需在第三方库之后为您的站点更改您自己的样式,并允许CSS覆盖以前的样式。这不是这个问题的内容。我知道这并没有解决OP的具体问题,但当我搜索“select2 height”时,Google似乎将这篇文章排在第一位,所以这可能会帮助其他人。如果您正在使用引导,并且只希望select2输入与其他输入具有相同的高度,那么现在有一个可用的主题:这就是主题。我改变了var-bottomPadding=70代码>帮助我选择2 v4.0.3!“旧”选项dropdownCssClass:“bigdrop”似乎不再适用于Select2V4.x。我使用的是Select2V3.5.2,这段代码对我很有用。在他们的文档中找不到如何改变高度。另外,在自定义css中添加select2也是非常有效的。css。select2-results\uu选项
是其他答案缺失的部分。我使用的是Select2 4.0.3。此代码将用于使选择本身变大,但不会使选择后显示的带有选项的框变大,这就是问题所解决的问题。这个答案是不正确的,这就是问题的作者添加了澄清的原因。这对一个被选中的人来说很好。如果您有多个,它将应用为其应用的最新select2计算的最新高度to@Tainmar正确的。也许你已经对这个插件做了一些修改。
.select2-container .select2-selection {
height: 34px;
}
.select2-selection {
min-height: 10px !important;
}
.select2-selection__rendered {
line-height: 31px !important;
}
.select2-container .select2-selection--single {
height: 35px !important;
}
.select2-selection__arrow {
height: 34px !important;
}
.select2-container--default .select2-results>.select2-results__options{
max-height: 500px !important;
}
.select2-selection {
height: auto !important;
}
.select2-container .select2-selection,
.select2-selection__rendered,
.select2-selection__arrow {
height: 48px !important;
line-height: 48px !important;
}
$("#my-dropdown").select2().maximizeSelect2Height();
/* Height fix for select2 */
.select2-container .select2-selection--single, .select2-container--default .select2-selection--single .select2-selection__rendered, .select2-container--default .select2-selection--single .select2-selection__arrow {
height: 35px;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 35px;
}
<select name="select_name" id="select_id" class="select2_extend_height wrap form-control" data-placeholder="----">
<option value=""></option>
<option value="1">test</option>
</select>
.wrap.select2-selection--single {
height: 100%;
}
.select2-container .wrap.select2-selection--single .select2-selection__rendered {
word-wrap: break-word;
text-overflow: inherit;
white-space: normal;
}
$('.select2_extend_height').select2({containerCssClass: "wrap"});
$('select').on('select2:open', function (e) {
var OptionsSize = $(this).find("option").size(); // The amount of options
var HeightPerOption = 36; // the height in pixels every option should be
var DropDownHeight = OptionsSize * HeightPerOption;
$(".select2-results__options").height(DropDownHeight);
});
.select2-container--default .select2-results>.select2-results__options {
max-height: inherit;
}
.select2-container{
.select2-selection{
height: 37px!important;
}
.select2-selection__rendered{
margin-top: 4px!important;
}
.select2-selection__arrow{
margin-top: 4px;
}
.select2-selection--single{
border: 1px solid #ced4da!important;
}
*:focus{
outline: none;
}
}
.select2-container .select2-selection--single {
height: 36px;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 36px;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
height: 36px;
}
.select2,
.select2-search__field,
.select2-results__option
{
font-size:1.3em!important;
}
.select2-selection__rendered {
line-height: 2em !important;
}
.select2-container .select2-selection--single {
height: 2em !important;
}
.select2-selection__arrow {
height: 2em !important;
}
// Change the select container width and allow it to take the full parent width
.select2
{
width: 100% !important
}
// Set the select field height, background color etc ...
.select2-selection
{
height: 50px !important
background-color: $light-color
}
// Set selected value position, color , font size, etc ...
.select2-selection__rendered
{
line-height: 35px !important
color: yellow !important
}
.select2-container--bootstrap4 .select2-results>.select2-results__options {
max-height: 20em;
}
/* Make Select2 boxes match Bootstrap3 as well as Bootstrap4 heights: */
.select2-selection__rendered {
line-height: 32px !important;
}
.select2-selection {
height: 34px !important;
}
<div id="skills">
<select class="select2" > </select>
</div>
#skills > span >span >span>.select2-selection__rendered{
line-height: 80px !important;
}
.select2-container .select2-selection--single{
height:0%;
}