首页 > 解决方案 > Vertical overflow always showing scrollbar which never works

问题描述

I have a sidebar with a menu with icons. When it overflows vertically you should be able to scroll the menu vertically so I am using overflow-y: scroll and the parent has overflow-y auto. However, scrolling is not working at all.

Here is a working fiddle.

Note I've tried with overflow-y: auto but it doesn't work either.

Also, the last three links should be pushed to be bottom of the sidebar (if there is enough space). For that, I'm using margin-top: auto on the first item of the group.

HTML:

<div class="sidebar">
  <div class="sidebar-head">H</div>
  <ul class="menu">
    <li><a href="">1</a></li>
    <li><a href="">2</a></li>
    <li><a href="">3</a></li>
    <li><a href="">4</a></li>
    <li><a href="">5</a></li>
    <li><a href="">6</a></li>
    <li class="bottom"><a href="">7</a></li>
    <li><a href="">8</a></li>
    <li><a href="">9</a></li>
  </ul>
</div>

CSS:

.sidebar {
    display: flex;
    flex-direction: column;
    box-shadow: none;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    padding-top: 70px;
    width: 230px;
    overflow-x: visible;
    overflow-y: auto;
    z-index: 10;
    background-color: #222d32;
    transition: left 0.3s ease, width 0.3s ease;
    left: 0;
    width: 50px;
}

.sidebar-head {
    display: flex;
    align-items: center;
    padding: 10px;
    margin-bottom: 20px;
    color: #fff;
}

.menu {
    margin-bottom: 0;
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    justify-content: flex-start;
    flex: 1;
    padding-bottom: 10px;
  padding-left: 0;
  list-style: none;
  overflow-x: hidden;
  overflow-y: scroll;
}

.menu > li {
  width: 100%;
}

.menu > li > a {
    position: relative;
    display: flex;
    align-items: center;
    padding: 12px 15px;
    font-size: 1.08em;
    border-left: 3px solid transparent;
    transition: border-left-color 0.3s ease, background-color 0.3s ease;
    color: #fff;
    background: #0d1214;
    border-left-color: #009688;
    text-decoration: none;
    overflow: hidden;
}

.bottom {
    margin-top: auto;
}

标签: htmlcssscroll

解决方案


这是因为你已经给出overflow-y:scroll了 class 的列表menu。将其更改为overflow-y:auto将修复它。

这是更正的小提琴: 小提琴


推荐阅读