Tips for dynamically adjusting the height of the select2 input box (multiple)

In my project, I have implemented a select2 input box for multiple selections. The user can choose as many options as they want, but if the selected options take up more space than the available width, I need the select box to automatically increase in height to accommodate all selected options without using a scroll bar. Currently, when the options exceed the width, they appear on the next line but the height of the select box remains the same.

https://i.sstatic.net/NW21K.png

I noticed that removing the height property from the .select2-container--default .select2-selection--multiple class fixes the issue. However, I still want to use this height property to set the initial height of the select box.

Here is an example of the select box without the height property in the .select2-container--default .select2-selection--multiple class, where the auto height feature works perfectly. https://i.sstatic.net/KDbqC.png

https://i.sstatic.net/FZBlo.png

.select2-container--default .select2-selection--multiple {
  background-color: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  -webkit-border-radius: 2px;
  border-radius: 2px;
  cursor: text;
  height: 22px;
}

Here is the JSFiddle link for reference: https://jsfiddle.net/rd62bhbm/

Answer №1

To fix the issue, apply the following CSS to the select2-selection--multiple class:

.select2-selection--multiple{
    overflow: hidden !important;
    height: auto !important;
}

Check out the Updated Fiddle, I recommend trying this solution. Thank you.

Answer №2

It is recommended to use the CSS property min-height:22px instead of height:22px

When styling a select2-container with multiple selections, consider the following styles:
  background-color: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  -webkit-border-radius: 2px;
  border-radius: 2px;
  cursor: text;
  min-height: 22px;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using CSS to create a background-clip effect on text along with a smooth opacity

I've coded a unique effect for my <h1> heading text where each letter is enclosed in its own <span> element, like this: <h1 class='gradient-heading' id='fade'> <span>H</span> <span>e</span ...

Add a dollar sign to the output value of the jQuery UI slider to display currency symbols

I have been working on a slider feature that successfully displays values to the user. However, I am looking to modify it so that the displayed values always have a "$" symbol in front of them. $("#slider").slider({ min: 0, ...

Sending query parameters from one URL to another using jQuery

Greetings! I have implemented a load more feature using a script that loads additional content on the same page when the 'Load More' button is clicked. Below is the script: <script type="text/javascript"> var track_page = 1; load_c ...

Is there a way to customize the font size of Material UI Autocomplete?

I'm currently trying to customize the Material UI Autocomplete component with my own CSS. Specifically, I aim to adjust the font size of the input field. Below is my current implementation: <Autocomplete style={{ width: 200, height: 60, ...

Scaling JQuery Mobile UI in a webview

I am currently working on developing a web-based module within an Android app. I have implemented Jquery mobile design and integrated it into the Android activity using WebView. However, when I run the activity, the design is displayed without being scaled ...

A guide on passing an array parameter using JavaScript

I need to transfer an array of ids from one page to another. I have created the $ids variable in PHP, and I am using it with jQuery like this: var ids = <?php echo json_encode($ids); ?>; jQuery(ids).each(function() { filters.push('ids[]=&a ...

Exploring the world of Chrome extensions with jQuery

Starting my first Chrome extension and feeling a bit stuck. I've gone through the information on https://developer.chrome.com/extensions/getstarted I understand how to modify the pop-up page, but I'm unsure how to allow JavaScript to alter a tab ...

Is there a way to stop music when I click?

After implementing the code snippet I discovered online, my website now plays audio when a specific button is clicked. However, I am interested in finding out how to modify the code so that the audio can be paused or stopped when the same button is click ...

Delete the automatic height adjustment using CSS

I am experiencing an issue with images in HTML where the width and height are set, but a stylesheet is overriding them with "height: auto." Is there a way to reset or override this CSS property so that the specified height in HTML takes precedence? HTML: ...

How can I ensure perfect center alignment in CSS?

Is there a preferred method for centering elements using CSS? <div style="position: absolute; left: 50%;"> <div style="position: relative; left: -50%"> Centered content </div> </div> or simply <div style=" ...

Is the Bootstrap carousel not automatically sliding on mobile devices?

I have implemented a Bootstrap carousel on my website where the user can choose between sliding manually or automatically. While it works fine on my computer, there seems to be an issue on my iPhone when I select the auto slide option. Below is the carous ...

Determine in jQuery whether a Value is present within a JSON dataset

[ { "link" : "images/examples/image-3.png", "image" : "images/examples/image-3.png", "title" : "copy" }, { "link" : "images/examples/image-3.png", "video" : "video placeholder", "title" : "c ...

Replace image source with a seamless transition

I have successfully changed the src of an image using a series of other images, but the transition is not smooth. It seems that the new image loads abruptly after the initial one fades out rather than simultaneously. Is there a clever way to achieve a com ...

Guide on transferring data from input fields to PHP using AJAX

I am currently working on the following HTML structure: <td> <input type="hidden" class="old_name" name="old_name" value="folder1/image.jpg" /> <input type="text" class="new_name" name="new_name ...

Exploring the Application of CSS Styles in Selenium

I need help with a webpage that contains a configurable field. The values of this field can be set through a configuration panel, and it can be marked as required by checking a checkbox which applies a specific style to the field label. My question is, how ...

Animating with JQuery utilizing a dynamic attribute

We are facing a challenge with animating multiple divs that share the same class name and have different attribute values: <div class="chart-bar" style="width:10%;" bar-width="100%"></div> <div class="chart-bar" style="width:10%;" bar-wid ...

Incorporating Angular into the script code of the extension content

I am looking to customize text fields on a website using a chrome-extension. However, the website is built with Angular, so I need to modify the text fields using Angular code. To incorporate Angular in my extension's scope, I am attempting to declar ...

Angular 2's solution for a persistent footer at the bottom of the page

I'm currently working on a project using Angular 2, and I'm looking to implement a sticky footer that always stays at the bottom of the page without being fixed. For reference, you can check out an example here: http://codepen.io/chriscoyier/pen/ ...

By checking the active box and completing the fields, the button will be activated

I'm currently working on a form that requires all fields to be filled out and the acceptance checkbox to be checked before the Submit button becomes active. If any entry or the checkbox is subsequently removed, the button should become inactive again. ...

Effortless navigation fails to glide seamlessly

I have scoured through numerous resources on achieving smooth scrolling, but I am still unable to make it work. I am considering writing the code in a "js/script.js" file and then pasting the link to "script.js" at the bottom of the main page. Do I need to ...