Select2: Dropdown not Exact width of Select Container (Simple Solution)

Created on 1 Mar 2016  路  3Comments  路  Source: select2/select2

I noticed when using this that the dropdown width would always be about 1px off either wider or narrower than the select. Not really a big deal but when using 1px borders, it just looks "wrong".

This seems to happen when you have the select set to a percentage in a responsive layout.

The solution is easy. Since .outerWidth rounds the pixels, you can use a different means of getting the exact width.

On line 4249 I changed this...

  AttachBody.prototype._resizeDropdown = function () {
    var css = {
      width: this.$container.outerWidth(false) + 'px'
    };

To this....

  AttachBody.prototype._resizeDropdown = function () {
    var css = {
      width: this.$container.get(0).getBoundingClientRect().width + 'px'
    };

This is an easy fix for those interested :)

4.x confirmed bug duplicate styling

Most helpful comment

Any chance to have this fix also in the distributed source code?

All 3 comments

Thanks for working that out! Looks now perfectly smooth!

Any chance to have this fix also in the distributed source code?

This is a duplicate of #1692.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mauroschiappo picture mauroschiappo  路  3Comments

pbkracker picture pbkracker  路  3Comments

ethanclevenger91 picture ethanclevenger91  路  3Comments

eved42 picture eved42  路  3Comments

ghost picture ghost  路  3Comments