This is code
<select class="selectpicker form-control" data-live-search="true" id="subject_teacher_drop_down">
<option>English</option>
<option>Methodology of social science with special reference to economics</option>
</select>
I want to make the width of the dropdown say for example 100 px . Now width is equal to the size of the biggest option.
This is code
<select class="selectpicker form-control" data-live-search="true" id="subject_teacher_drop_down">
<option>English</option>
<option>Methodology of social science with special reference to economics</option>
</select>
I want to make the width of the dropdown say for example 100 px . Now width is equal to the size of the biggest option.
Share Improve this question edited Jun 19, 2018 at 10:56 Rosh asked Jun 19, 2018 at 10:51 RoshRosh 4912 gold badges7 silver badges25 bronze badges 6-
You have to look at the rendered markups ( through developer tool for example ), then apply your style (
width:100px;
) on the element – Thum Choon Tat Commented Jun 19, 2018 at 11:00 -
Hi, wele to Stack Overflow, it appears your question is a duplicate. Have a look here. @ThumChoonTat applying the width to
<option>
won't work. – Ivan Commented Jun 19, 2018 at 11:00 - Possible duplicate of Set Width at Option of Select Box – Ivan Commented Jun 19, 2018 at 11:01
-
@Ivan OP is using third party library to render select field, and the output will not be
select
andoption
, so I think it's possible – Thum Choon Tat Commented Jun 19, 2018 at 11:06 - No simple answer? :( – Rosh Commented Jun 19, 2018 at 11:09
3 Answers
Reset to default 3Guyz, I came up with a simple answer. We can use the 'datacontent' attribute in the select option to display the values of select options thereby changing its width :) The code is:
<select class="selectpicker form-control" data-live-search="true" id="subject_teacher_drop_down">
<option data-content="English">English</option>
<option data-content="Methodology of social...">Methodology of social science with special reference to economics</option>
</select>
If you are passing the values dynamically, do like this :
<select class="selectpicker form-control" data-live-search="true">
<?php foreach ($subjectList as $subjects) : ?>
<option data-content=" <?php $a =$subjects->getName();$b = substr($a, 0, 35);$y = $b . "...";if($a > $b)echo $y; else echo $a;?>" id="<?php echo $subjects->getId(); ?>" name="<?php echo $subjects->getName(); ?>" value="<?php echo $subjects->getName(); ?>">
<?php echo $subjects->getName(); ?>
</option>
<?php endforeach; ?>
</select>
There would seem to be no way to do this using css styles and/or classes in the markup but if you are open to javascript -- in the example below, jQuery -- then you can hook the selectpicker's show.bs.select
event and modify the widget as follows:
$('.selectpicker').on('show.bs.select', function () {
var $dropdownMenu = $(this).nextAll('div.dropdown-menu').first();
if ($dropdownMenu.length > 0) {
$dropdownMenu.css('min-width', '').css('max-width', '100%');
var $inner = $dropdownMenu.find('div.inner');
if ($inner.length > 0) {
$inner.css('overflow-x', 'hidden');
}
}
});
Here I am accessing the div for the dropdown menu that it uses and removing whatever it is assigning for the min-width
and then setting max-width
to 100% and then to remove the horizontal scrollbar that might be shown if options are too wide I am setting the dropdown's 'inner' div overflow-x
to hidden
.
The result is a dropdown menu that is constrained to the width of the button that shows it with any options that have text that is too wide being truncated.
More work than I wanted to do on this but the default behavior makes dropdowns unusable on small-screen displays...
You can add
select,option
{
width : 150px(just give the width PX which it for screen size);
}