• 6
    i'll never understand how the browser makers are off building WebComponents and whatnot and they can't seem to work out something as simple as this. has any developer ever said 'hmm i'd like to limit the number of elements shown in my select dropdown... that ugly list-y thing will do just fine !' – Petrov Mar 16 '15 at 20:12
  • @Petrov: well, what’s the pressing need for web page authors to control the display of a drop-down list so precisely? Can’t the operating system do a fine job of working it out? – Paul D. Waite Jul 02 '15 at 09:06
  • The drop-down list can often be too long and makes the list completely unwieldy. And in some cases gets truncated by the visible screen because it doesn't do it right. – CaptainBli Sep 02 '21 at 17:38
  • 10 Answers10

    38

    You can try this

    <select name="select1" onmousedown="if(this.options.length>8){this.size=8;}"  onchange='this.size=0;' onblur="this.size=0;">
                 <option value="1">This is select number 1</option>
                 <option value="2">This is select number 2</option>
                 <option value="3">This is select number 3</option>
                 <option value="4">This is select number 4</option>
                 <option value="5">This is select number 5</option>
                 <option value="6">This is select number 6</option>
                 <option value="7">This is select number 7</option>
                 <option value="8">This is select number 8</option>
                 <option value="9">This is select number 9</option>
                 <option value="10">This is select number 10</option>
                 <option value="11">This is select number 11</option>
                 <option value="12">This is select number 12</option>
               </select>

    It worked for me

    Jordan.J.D
    • 7,999
    • 11
    • 48
    • 78
    Raj_89
    • 691
    • 6
    • 5
    21

    You can use the size attribute to make the <select> appear as a box instead of a dropdown. The number you use in the size attribute defines how many options are visible in the box without scrolling.

    <select size="5">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
        <option>8</option>
        <option>9</option>
        <option>10</option>
        <option>11</option>
        <option>12</option>
    </select>
    

    You can’t apply this to a <select> and have it still appear as a drop-down list though. The browser/operating system will decide how many options should be displayed for drop-down lists, unless you use HTML, CSS and JavaScript to create a fake dropdown list.

    Paul D. Waite
    • 96,640
    • 56
    • 199
    • 270
    • [spec docs](https://html.spec.whatwg.org/multipage/form-elements.html#attr-select-size) and [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#attributes) – starball May 13 '23 at 00:34
    8

    Raj_89 solution is the closest to being valid option altough as mentioned by Kevin Swarts in comment it is going to break IE, which for large number of corporate client is an issue (and telling your client that you won't code for IE "because reasons" is unlikely to make your boss happy ;) ).

    So I played around with it and here is the problem: the 'onmousedown' event is throwing a fit in IE, so what we want to do, is to prevent default when user clicks the dropdown for the first time. It is important this is only time we do this: if we prevent defult on the next click, when user makes his pick, the onchange event won't fire.

    This way we get nice dropdown, no flicker, no breaking down IE - just works... well at least in IE10 and up, and latest relases of all the other major browsers.

    <p>Which is the most annoing browser of them all:</p>
    <select id="sel" size = "1">
        <option></option>
        <option>IE 9</option>
        <option>IE 10</option>
        <option>Edge</option>
        <option>Firefox</option>
        <option>Chrome</option>
        <option>Opera</option>
    </select>
    

    Here is the fiddle: https://jsfiddle.net/88cxzhom/27/

    Few more things to notice: 1) The absolute positioning and setting z-index is helpful to avoid moving other elements when the options are displayed. 2) Use 'currentTarget' property - this will be the select element across all browsers. While 'target' will be select in IE, the rest will actually allow you to work with option.

    Hope this helps someone.

    Michal Ja
    • 252
    • 3
    • 8
    4

    Tnx @Raj_89 , Your trick was very good , can be better , only by use extra style , that make it on other dom objects , exactly like a common select option tag in html ...

    select{
     position:absolute;
    }
    

    u can see result here : http://jsfiddle.net/aTzc2/

    4

    the size attribute matters, if the size=5 then first 5 items will be shown and for others you need to scroll down..

    <select name="numbers" size="5">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
    </select>
    
    Pankaj Upadhyay
    • 12,966
    • 24
    • 73
    • 104
    shiva L
    • 81
    • 5
    3

    I used this code and it worked for me on Chrome, Firefox and IE.

    <select  onmousedown="if(this.options.length>5){this.size=5;}" onchange="this.blur()"  onblur="this.size=0;">
    <option>option1</option>
    <option>option2</option>
    <option>option3</option>
    <option>option4</option>
    <option>option5</option>
    <option>option6</option>
    <option>option7</option>
    </select>
    3

    It is not possible to limit the number of visible elements in the select dropdown (if you use it as dropdown box and not as list).

    But you could use javascript/jQuery to replace this selectbox with something else, which just looks like a dropdown box. Then you can handle the height of dropdown as you want.

    jNice would be a jQuery plugin which has such features. But there also exists many alternatives for that.

    Armin
    • 15,582
    • 10
    • 47
    • 64
    0

    I have made a simple solution for this in ReactJS you can use this in Vanilla Javascript aswell.

    Javascript code

    //props.options = [{value:'123',label:'123'},{value:'321',label:'321'},{value:'432',label:'432'}];
    <div>
    <div
      className="new-user-input"
      style={{ marginTop: '10px' }}
      onClick={() => {
        this.setState({
          showOptions: !this.state.showOptions,
        });
      }}
    >
      {selectedOption ? (
        <span className="txt-black-600-12">
          {' '}
          {selectedOption.label}{' '}
        </span>
      ) : (
        <span className="txt-grey-500-12">
          Select Option
        </span>
      )}
    
      //Font awesome icons
      <span className="float-right">
        {this.state.showOptions ? (
          <FaAngleUp />
        ) : (
          <FaAngleDown />
        )}
      </span>
      {this.state.showOptions && (
        <div className="custom-select mt-10">
          {this.props.options.map(ele => {
            return (
              <span
                className="custom-select-option"
                onClick={() => {
                  this.setState({
                    selectedOption: ele,
                    showOptions: false,
                  });
                }}
              >
                {ele.label}
              </span>
            );
          })}
        </div>
      )}
    </div>
    </div>
    

    CSS

    .new-user-input {
      border: none;
      background-image: none;
      background-color: #ffffff;
    
      box-shadow: 0px 1px 5px 1px #d1d1d1;
      outline: none;
      display: block;
      margin: 20px auto;
      padding: 10px;
      width: 90%;
      border-radius: 8px;
    }
    
    .new-user-input:focus {
      border: none;
      background-image: none;
      background-color: #ffffff;
      outline: none;
    }
        .custom-select{
      display: flex;
        flex-direction: column;
        max-height: 100px;
        overflow: auto;
        flex: 1 0;
    }
    .custom-select-option{
      padding: 10px 0;
      border-bottom: 1px solid #ececec;
      font-size: 10px;
      font-weight: 500;
      color: #413958;
    }
    
    • 1
      There is a major disadvantage to using custom DOM to replace a select element: lack of accessibility support. For example, your control will not support typing to select an option, using the spacebar to toggle the dropdown open or closed, arrow keys to select an option, enter to select an option, and likely more. This penalizes vision impaired users who do not use the mouse as well as "power users" who prefer to use a keyboard. – GregL Apr 29 '21 at 21:36
    0

    A minor but important modification to existing solutions aiming at preserving framework styling (i.e. Bootstrap): replace this.size=0 with this.removeAttribute('size').

    <select class="custom-select" onmousedown="if(this.options.length>5){this.size=5}"
        onchange='this.blur()' onblur="this.removeAttribute('size')">
        <option>option1</option>
        <option>option2</option>
        <option>option3</option>
        <option>option4</option>
        <option>option5</option>
        <option>option6</option>
        <option>option7</option>
    </select>
    
    Nicolae Iotu
    • 399
    • 3
    • 7
    -1
    <p>Which one true?</p>
    <select id="sel">
        <option>-</option>
        <option>A</option>
        <option>B</option>
        <option>C</option>
    </select>
    

    ( display just first option of all options)

    document.getElementById("sel").options.length=1; 
    
    harun ugur
    • 1,718
    • 18
    • 18
    • Actually, this cuts off the list. So while you do only see the number of options you have set, you lose the others as they are not visible nor can you select them with scrolling. – Monduiz Dec 21 '19 at 13:31