[英]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
元素具有响应性,我们可能有一些可能性,例如:
@media()
queries).@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.注意:不需要
select
和span
元素选择器,@ @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.