Search code examples
htmlcsscss-multicolumn-layout

Wrong scroll bar when using column-count (CSS/HTML)


When using column-count: 2, if there is overflow, I want a vertical scroll bar to appear, but a horizontal one appears instead. Without column-count, the vertical scroll bar appears, as expected. How can I use column-count: 2 and have the vertical scroll bar appear?

.searchCriteriaPanel {
    border-radius: 0 0 5px 5px;
    width: 300px;
    height: 200px;
    background-color: lightgrey;
    padding: 10px;
    overflow-y: auto;
    column-count: 2;
}

<div id="MarketContent" class="searchCriteriaPanel">
    <label><input type="checkbox">one</label><br />
    <label><input type="checkbox">two</label><br />
    <label><input type="checkbox">three</label><br />
    <label><input type="checkbox">four</label><br />
    <label><input type="checkbox">five</label><br />
    <label><input type="checkbox">six</label><br />
    <label><input type="checkbox">seven</label><br />
    <label><input type="checkbox">eight</label><br />
    <label><input type="checkbox">nine</label><br />
    <label><input type="checkbox">ten</label><br />
    <label><input type="checkbox">eleven</label><br />
    <label><input type="checkbox">twelve</label><br />
    <label><input type="checkbox">thirteen</label><br />
    <label><input type="checkbox">fourteen</label><br />
    <label><input type="checkbox">fifteen</label><br />
    <label><input type="checkbox">sixteen</label><br />
    <label><input type="checkbox">seventeen</label><br />
    <label><input type="checkbox">eighteen</label><br />
    <label><input type="checkbox">nineteen</label><br />
    <label><input type="checkbox">twenty</label><br />
    <label><input type="checkbox">twentyone</label><br />
    <label><input type="checkbox">twentytwo</label>
</div>

JS-Fiddle


Solution

  • The reason for that behaviour ist that you are restricting the size of that element with a fixed width and height. To get what you expect, put a wrapper element around it and move the width and height settings to this wrapper. Then you'll get the desired scolling behaviour:

    .wrapper {
          width: 300px;
          height: 200px;
          overflow: auto;
    }
    .searchCriteriaPanel {
          border-radius: 0 0 5px 5px;
          background-color: lightgrey;
          padding: 10px;
          column-count: 2;
    }
    

    The complete solution in a fiddle: https://jsfiddle.net/jgLxc2fu/3/