How do you set the max height of an expanded "Chosen" element (jQuery plugin)

Migwell picture Migwell · Jan 14, 2014 · Viewed 26.5k times · Source

At the moment I'm using a Chosen JQuery widget inside a JQuery dialog box, and as soon as you open the dropdown box, the size of the dropdown causes the dialog box to overflow, causing this: (notice that there are two scroll bars, one for the drop down box, and one for the dialog box, which renders scrolling pretty much useless:

http://i.imgur.com/bCTCDCq.png?1

Is there a way to set:

  • The max number of items displayed before you have to scroll,
  • The max height of the select/chosen drop down box, or
  • The max height of one of the containing elements

Such that it will appear more like this (it's a photoshop, this is how I want it to look):

http://i.stack.imgur.com/M27ul.png

i.e. no overflow in the dialog window, because there are only 8 items displayed before you have to scroll.

Answer

Migwell picture Migwell · Jan 14, 2014

tl;dr: Add this CSS rule to your styles.css:

.chosen-container .chosen-results {
    max-height:100px;
}


Initially I didn't understand this, but this selector is actually applying to classes inside the Chosen (the library generates its own html elements that are seperate from the <select> element) They aren't referring to the container that you, the designer, have put the Chosen in, so they will work in any situation.

However even when I realised that, this selector wouldn't work as chosen.css has the exact same selector, and because the last declared rule wins, my rule had no effect. There are two solutions to this:

  • Add !important to your custom rule (probably the incorrect way)

OR

  • Make sure your styles.css is declared after chosen.css. My links were in this order:

    <link rel="stylesheet" href="css/main.css">

    <link rel="stylesheet" href="chosen/chosen.css">

    If you change the order so that your main.css (or whatever you've called your custom styles file) is declared after, the problem will also be solved.