How do I make a sidenav scrollbar?

Viewed 39

I have a sidenav using a <ul> tag. How to make sidenav show scrollbar auto?

An Example of what my sidenav looks like

html:

<section class="sticky-top">
    <ul class="nav flex-column bg-secondary p-3 rounded-right col-md-3 col-lg-2 text-center min-vh-100">
        <li class="nav-item">
            <a class="nav-link" href="#">Test 1</a>
        </li>
        <li class="nav-item">
            <a class="nav-link " href="#">Test 2 </a>
        </li>
        <li class="nav-item">
            <a class="nav-link " href="#">Test 3</a>
        </li>
    </ul>
</section>

css:

.nav {
    position: absolute;
    z-index : 3;
}


.navbar {
    z-index         : 3;
    background-color: var(--white);
}
1 Answers

When I applied the sidebar styling by enclosing the <ul> element in a <div> element, I got the desired result. Hovering the sidebar (when the hover event occurs) opens the scrollbar.

enter image description here

<div class="sidebar">
    <ul class="nav flex-column bg-secondary p-3 rounded-right col-md-3 col-lg-2 text-center min-vh-100">
        <!-- sidebar items -->
    </ul>
</div>
.sidebar{
    position: fixed;
    top: 0;
    bottom: 0;
    background: #6C757D;
    width: 140px;
}

.sidebar:hover{
    overflow-y: auto
}

The executable snippet is available below. The overflow-y: auto; style comment line defined in the .sidebar:hover block has been made to automatically display the scrollbar; Added overflow-y: auto; style to .sidebar block.

.sidebar {
  position: fixed;
  top: 0;
  bottom: 0;
  background: #6C757D;
  width: 140px;
  overflow-y: auto;
}
.sidebar:hover {
/* overflow-y: auto */
}

.nav {
  position: absolute;
  z-index: 3;
}
.navbar {
  z-index: 3;
  background-color: var(--white);
}
.nav-link {
  color: #ffffff;
  font-size: 12px;

  &:hover {
    transition: color 0.4s ease-in;
    background: rgba(255, 255, 255, 0.04);
    color: #ffffff;
  }
  
  &.active {
    background-color: #007bff;
  }
}
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
  <section class="sticky-top">
    <div class="sidebar">
      <ul class="nav flex-column bg-secondary p-3 rounded-right col-md-3 col-lg-2 text-center min-vh-100">
        <li class="nav-item"><a class="nav-link" href="#">Test</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Test</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Test</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Test</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Test</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Test</a></li>
      </ul>
    </div>
  </section>
</body>

Related