简体   繁体   English

select2 没有响应,宽度大于容器

[英]select2 not responsive, width larger than container

I have a problem with the width of a select2 dropdown in my ASP.NET page.我的 ASP.NET 页面中的 select2 下拉菜单的宽度有问题。 When I try to view the page with the Chrome emulator of devices screen, the select2 is larger than the containing div, and on the right it goes out of the screen.当我尝试使用设备屏幕的 Chrome 模拟器查看页面时,select2 比包含的 div 大,并且在右侧它超出了屏幕。 I saw with code inspection that it adds automatically a style attribute style="width: 498px;"我通过代码检查看到它自动添加了一个样式属性style="width: 498px;" in the <span class="select2 select2-container select2-container--bootstrap select2-container--below"> element that I did not set anywhere.在我没有在任何地方设置的<span class="select2 select2-container select2-container--bootstrap select2-container--below">元素中。 The only operation that I did is to set $("#ContentPlaceHolderContent_mySelect").select2();我所做的唯一操作是设置$("#ContentPlaceHolderContent_mySelect").select2(); in the document.ready function().在 document.ready 函数()中。 My select2 dropdown is contained in a block:我的 select2 下拉列表包含在一个块中:

<div class="form-group">
   <label class="control-label col-md-3">Select structure</label>
   <div class="col-lg-5 col-md-9">
      <select class="form-control" id="mySelect" runat="server"></select>

   </div>
</div>

How can I remove that style="width" option?如何删除该 style="width" 选项?

Select2 adds class .select2 . Select2 添加类.select2 You can override what script does using css.您可以使用 css 覆盖脚本的作用。 Here I'm set select2 to have 100% width , using !important .在这里,我使用!important将 select2 设置为100% width If I would not do that select2 would have 24px width.如果我不这样做, select2 将有 24px 宽度。

You can further customize other classes that select2 generates using some principle.您可以使用某些原则进一步自定义select2生成的其他类。

 $(document).ready(function(){ $("#mySelect").select2(); });
 .select2 { width:100%!important; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script> <div class="form-group"> <label class="control-label col-md-3">Select structure</label> <div class="col-lg-5 col-md-9"> <select class="form-control" id="mySelect" runat="server"></select> </div> </div>

This works for me:这对我有用:

$('select').select2({
    width: '100%'
});

and add CSS:并添加 CSS:

.select2-selection { overflow: hidden; }
.select2-selection__rendered { white-space: normal; word-break: break-all; }

Add to CSS "!important" if you need.如果需要,添加到 CSS "!important"。

if you are using bootstrap ,use the below style如果您使用的是引导程序,请使用以下样式

.form-group > .select2-container {
    width: 100% !important;
}

try use dropdownParent尝试使用 dropdownParent

$('#mySelect2').select2({
  dropdownParent: $('#myModal')
});

https://select2.org/dropdown#dropdown-placement https://select2.org/dropdown#dropdown-placement

The <span> with the .select2 class is created by the Select2 plugin just after creation a new select2 on a given <select> .带有.select2类的<span>是由 Select2 插件在给定的<select>上创建新的 select2 之后创建的。

To make this span.select2 element responsive, we may have some possibilities, such as:为了使这个span.select2元素具有响应性,我们可能有一些可能性,例如:

  1. Use a % width (either on the original before creating the select2 element, or in the select2 width configuration option).使用 % 宽度(在创建 select2 元素之前的原始上,或在 select2宽度配置选项中)。
  2. Override via CSS (for example when we use @media() queries).通过 CSS 覆盖(例如,当我们使用@media()查询时)。 Note that this may affect the plugin's functionality or display.请注意,这可能会影响插件的功能或显示。

For the 2nd one, we can use the adjacent sibling combinator ( + ) with the !important on the width value to style our select2's <span> , since normally the select2's span is created and inserted right after our <select> .对于第二个,我们可以使用相邻的兄弟组合器( + ) 和!important的宽度值来设置我们的 select2 的<span>样式,因为通常 select2 的 span 是在我们的<select>之后创建和插入的。

An example below:下面是一个例子:

@media (max-width: 400px) {
  select.my-x-select + span.select2 {
    width: 200px !important;
  }
}

Note: select and span element selectors aren't needed, @media as an example use case.注意:不需要selectspan元素选择器,@ @media作为示例用例。

You don't need to use !important , you just can override it with max-width like:您不需要使用!important ,您只需使用max-width覆盖它,例如:

.select2{
    max-width: 100%;
}

Getting Overflow?获取溢出? Do it with JS also也用 JS 做

Using CSS width:100% sometimes get scrolling issue if some elements have overflow property.如果某些元素具有溢出属性,则使用 CSS width:100% 有时会出现滚动问题。 I would recommend using js also.我也建议使用 js。

Add below js在js下面添加

 $('select').select2({
        width: '100%'
 });

Add below CSS添加下面的CSS

.select2-selection { overflow: hidden; }
.select2-selection__rendered { white-space: normal; word-break: break-all; }

/*Copied from already given :) */ /*从已经给出的复制:) */

First of all add Select2 class .select2 in select tag and used these CSS首先在选择标签中添加 Select2 类.select2并使用这些 CSS

.select2-container{
        width: 100% !important;
    }
    .control-label {
        width: 100%;
    }

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

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