列出溢出的Flex容器但不滚动

时间:2017-06-12 19:05:59

标签: html css css3 flexbox overflow

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

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

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.

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您需要将list设为灵活容器

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

答案 2 :(得分:0)

您只需设置每height

div即可

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>