0

I'm trying to hide the scrollbar on a list box unless it's necessary but can't get it to behave. It seems to always want to show. I am using Chromium, I haven't checked in other browsers whether it behaves.

HTML

<select size="2">
    <option>item</option>
    <option>item</option>
    <option>item</option>
    <option>item</option>
    <option>item</option>
</select>

CSS

select {
    width: 200px;
    min-height: 400px;
    overflow: auto;
}

http://jsfiddle.net/jmRmv/

The above code produces:

enter image description here

Lerp
  • 2,957
  • 3
  • 24
  • 43

1 Answers1

0

You can do it with a bit of CSS trickery and some JavaScript. It might not be the perfect solution but it works.

JSFiddle demo.

CSS

.wrapper {
    display: inline-block;
    vertical-align: top;
    overflow: hidden;
    border: solid black 1px;
}
.select {
    width: 200px;
    min-height: 50px;
    margin: 0 -20px 0 0;
}
.select.scrolling {
    margin: 0;
}

HTML

<div class="wrapper">
    <select id="select" class="select" size="2">
        <option>item 1</option>
        <option>item 2</option>
        <option>item 3</option>
        <option>item 4</option>
        <option>item 5</option>
        <option>item 6</option>
    </select>
</div>

JavaScript

var select = document.getElementById('select');

if (select.clientHeight < select.scrollHeight) {
    select.classList.add('scrolling'); // classList: IE9 and higher
}
Ex-iT
  • 1,479
  • 2
  • 12
  • 20