【问题标题】:Wrong scroll bar when using column-count (CSS/HTML)使用列数 (CSS/HTML) 时滚动条错误
【发布时间】:2018-03-03 09:58:03
【问题描述】:

当使用 column-count: 2 时,如果有溢出,我希望出现一个垂直滚动条,但出现一个水平滚动条。如果没有列数,则会按预期显示垂直滚动条。如何使用 column-count: 2 并显示垂直滚动条?

.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

【问题讨论】:

    标签: html css css-multicolumn-layout


    【解决方案1】:

    这种行为的原因是您使用 fixed widthheight 来限制该元素的大小。 要获得您所期望的,请在其周围放置一个包装器元素并将宽度和高度设置移动到此包装器。然后您将获得所需的 scolling 行为:

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

    小提琴中的完整解决方案:https://jsfiddle.net/jgLxc2fu/3/

    【讨论】:

    • 我今天早上早些时候遇到了同样的解决方案。男孩,这对我来说确实像是一种黑客行为。不怪你。真正归咎于 CSS/HTML。我不知道为什么我不能指定宽度、高度、列数,并通过垂直滚动条进行额外显示。不幸的是,这弄乱了我的总宽度,而且我在这个上面/下面还有其他 div 没有多列,并且使用这个解决方案,我必须修改这个的宽度以与其他的对齐。感谢您的宝贵时间,因此我将其标记为解决方案,但我真的不喜欢它。再说一遍,这不是你的错。
    【解决方案2】:

    您是否尝试过使用溢出控制来包装列并在包装器上设置高度,例如: https://codepen.io/anon/pen/YrGPNM

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

    【讨论】:

    • 抱歉,已更正。如果我正确理解您的问题,那么现在应该可以了。而不是“twentyone”和“twentytwo”仅在向右滚动时可见,所有数字都通过向下滚动显示。
    • 它似乎仍然不适合我。我没有看到任何滚动条。也许我有缓存问题。对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2021-11-03
    • 2011-01-16
    相关资源
    最近更新 更多