Selected Options disappear in a multiple drop-down select tag with scroll bar in HTML

后端 未结 2 1959
走了就别回头了
走了就别回头了 2021-01-18 18:05

I have the following multiple drop down select tag

相关标签:
2条回答
  • 2021-01-18 18:34

    Horizontal scrolling for <select> elements is buggy in Edge/Chrome, and completely unsupported in Firefox.

    A work-around supported all browsers would be to simply wrap it in a <div> and apply some of your CSS there instead:

    .Something {
      overflow-x: auto;
      overflow-y: auto;
      width: 20%;
      height: 100px;
    }
    
    .Something > select {
      overflow-y: hidden;
    }
    <div class="Something">
        <select multiple size="6">
          <option>Optionfghfghfgdgdffyuujkyujg 1</option>
          <option>Optionfghfghfgdgdffyuujkyujg 1</option>
          <option>Option n fgnfn ghdnghd ngdh 2</option>
          <option>Optionfghfghfgdgdffyuujkyujg 1</option>
          <option>Option n fgnfn ghdnghd ngdh 2</option>
          <option>Option n fgnfn ghdnghd ngdh 2</option>
        </select>
    </div>

    Some changes had to be made for this to work. The size attribute of your <select> must match the number of options, and your <div> must have a set height.

    0 讨论(0)
  • 2021-01-18 18:34

    Not sure why it does that, I can reproduce in Chrome.

    This seems to fix it. Setting float: left; min-width: 100%; on the <option> element style.

    float: left destroys the default block formatting context behaviour of the <option> tags in the <select>. min-width: 100% just makes it a little more aesthetically pleasing, it ensures that even the <option> tags which have content shorter than the width of the <select> are "fully highlighted" when selected.

    P.S. This fixes the issue for Chrome and IE11, won't fix it for IE10- and Firefox as they don't support horizontal scrolling on a <select> element at all :)

    .Something {
      overflow-x: scroll;
      width: 16%;
    }
    
    option {
      float: left;
      min-width: 100%;
    }
    <select multiple size="5" class="Something">
      <option>Optionfghfghfgdgdffyuujkyujg 1</option>
      <option>Optionfghfghfgdgdffyuujkyujg 1</option>
      <option>Option n fgnfn ghdnghd ngdh 2</option>
      <option>Optionfghfghfgdgdffyuujkyujg 1</option>
      <option>Option n fgnfn ghdnghd ngdh 2</option>
      <option>Option n fgnfn ghdnghd ngdh 2</option>
    </select>

    0 讨论(0)
提交回复
热议问题