【问题标题】:List overflowing flex container but not scrolling列出溢出的 flex 容器但不滚动
【发布时间】:2017-06-13 03:48:29
【问题描述】:

我正在制作一个交互式列表。我遇到的问题是,当项目数超过窗口高度时,列表应该可以向上/向下滚动以查看内容。

对于我的列表,我使用了一个弹性框,以便我可以根据需要拥有多列项目。此外,窗口的高度和宽度被缩放以填充屏幕。 (不过,我在此测试代码中设置了固定尺寸。)

HTML

<div class="container">
    <div class="list">
        <div class="input">
            <input type="text" placeholder="Add new item..."></input>
        </div>

        <div class="list-container">
            <ul>
                <li>Entry 1</li>
                <li>Entry 2</li>
                <li>Entry 3</li>
                <li>Entry 4</li>
                <li>Entry 5</li>
                <li>Entry 6</li>
            </ul>
        </div>
    </div>
</div>

CSS (.scss)

.container {
    margin: auto;
    height: 300px; //**NOTE: This is an arbitrary value for showcase. It really would just scale to the screen size.
    width: 780px;
    background-color: #2c3e50;
    border-radius: 10px;
    padding: 15px;
    font-family: sans-serif;
}

.list {
    .input {
        border-bottom: 1px solid white;

        input {
            color: white;
            background: transparent;
            border: none;
            width: 100%;
            font-size: 28px;
            padding-bottom: 10px;

            &:focus {
                outline: 0;
            }
        }

        input::-webkit-input-placeholder {
            color: white;
            opacity: 0.6;
            font-style: italic;
        }
    }

    .list-container {
        overflow-y: scroll; //**Doesn't work

        ul {
            margin: 0;
            margin-top: 10px;
            padding: 0;
            display: flex;
            flex-direction: column;
            flex-wrap: wrap;

            li {
                flex: 1 0 100%;
                background: #34495e;
                border-radius: 5px;
                box-shadow: 0px 2px 4px black;
                color: white;
                list-style: none;
                padding: 10px;
                margin: 10px 0px;
                white-space: nowrap;
                font-size: 22px;
            }
        }
    }
}

Full pen is here.

【问题讨论】:

    标签: html css flexbox overflow


    【解决方案1】:

    你只需要设置每个divheight

    html, body {
      height: 100%;
    }
    
    .container {
      height: 100%; 
      margin: auto;
      width: 90%;
      background-color: #2c3e50;
      border-radius: 10px;
      padding: 15px;
      font-family: sans-serif;
    }
    
    .list {
      height: 100%;
    }
    .list .input {
      border-bottom: 1px solid white;
    }
    .list .input input {
      color: white;
      background: transparent;
      border: none;
      width: 100%;
      font-size: 28px;
      padding-bottom: 10px;
    }
    .list .input input:focus {
      outline: 0;
    }
    .list .input input::-webkit-input-placeholder {
      color: white;
      opacity: 0.6;
      font-style: italic;
    }
    .list .list-container {
      height: 80%;
      overflow-y: scroll;
    }
    .list .list-container ul {
      margin: 0;
      margin-top: 10px;
      padding: 0;
      display: relative;
    }
    .list .list-container ul li {
      background: #34495e;
      border-radius: 5px;
      box-shadow: 0px 2px 4px black;
      color: white;
      list-style: none;
      padding: 10px;
      margin: 10px 0px;
      white-space: nowrap;
      font-size: 22px;
    }
    <div class="container">
        <div class="list">
            <div class="input">
                <input type="text" placeholder="Add new item..."></input>
            </div>
    
            <div class="list-container">
                <ul>
                    <li>Entry 1</li>
                    <li>Entry 2</li>
                    <li>Entry 3</li>
                    <li>Entry 4</li>
                    <li>Entry 5</li>
                    <li>Entry 6</li>
                </ul>
            </div>
        </div>
    </div>

    【讨论】:

      【解决方案2】:

      您需要将list 设为弹性容器

      .list {
          display: flex;
          flex-direction: column;
      

      【讨论】:

        【解决方案3】:

        尝试将以下属性添加到您的 .container

        max-height: 100vh;
        overflow-y: scroll;
        

        【讨论】:

        • 我已经能够让整个父容器滚动。是否可以只滚动列表内容(即将输入栏保持在顶部)?给它一个高度属性似乎可以解决问题,但是有没有办法解决这个问题?
        猜你喜欢
        • 2020-11-19
        • 1970-01-01
        • 2020-12-27
        • 1970-01-01
        • 2019-01-09
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多