cwk
cwk

Reputation: 43

List overflowing flex container but not scrolling

I am making an interactive list. The problem I have run into is that when the number of items exceeds the height of the window, the list should be able to be scrolled up/down to view content.

For my list, I am using a flex box so that I can have multiple columns of items as necessary. Also, the window height and width is scaled to fill the screen. (I set fixed dimensions in this test code, however.)

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.

Upvotes: 1

Views: 1475

Answers (3)

Pawan Kumar
Pawan Kumar

Reputation: 1374

you just need to set the height of every 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>

Upvotes: 1

Asons
Asons

Reputation: 87221

You need to make the list a flex container

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

Upvotes: 0

Blazemonger
Blazemonger

Reputation: 92943

Try adding the following properties to your .container:

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

Upvotes: 0

Related Questions