简体   繁体   中英

Why is the vertical scrollbar not working in HTML/CSS?

I am currently doing the FreeCodeCamp course and I am trying to replicate this website: https://codepen.io/freeCodeCamp/full/NdrKKL . The navigation bar on the left has a vertical scrollbar. On other posts I read that setting the parents' height element to 100% would fix it. Somehow, I can't seem to fix this in my code. This is the current state of my website: https://codepen.io/otapadar/full/VwKBvXB .

Any help would be greatly appreciated. See below my code:

PS: I have added borders around each list-element. They overlap, which makes the border twice as thick as I intend them to be. If someone also knows how to fix this, then that would also be greatly appreciated! Thanks for your time!

HTML (Only important stuff):

<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

<nav id="navbar">
  <header>
    <h1>JS Documentation</h1>
  </header>
  <ul>
    <li><a class="nav-link" href=#introduction>Introduction</a></li>
    <li><a class="nav-link" href=#what_you_should_know>What You Should Know</a></li>
    <li><a class="nav-link" href=#javascript_and_java>JavaScript and Java</a></li>
    <li><a class="nav-link" href=#hello_world>Hello World</a></li>
    <li><a class="nav-link" href=#variables>Variables</a></li>
    <li><a class="nav-link" href=#declaring_variables>Declaring Variables</a></li>
    <li><a class="nav-link" href=#variable_scope>Variable Scope</a></li>
    <li><a class="nav-link" href=#global_variables>Global Variables</a></li>
    <li><a class="nav-link" href=#constants>Constants</a></li>
    <li><a class="nav-link" href=#data_types>Data Types</a></li>
    <li><a class="nav-link" href=#if/else_statements>If/Else Statements</a></li>
    <li><a class="nav-link" href=#while_statements>While Statements</a></li>
  </ul>
</nav>

CSS (Everything):

@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');

* { 
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Open Sans', Arial;
  line-height: 1.5;
  height: 100%;
}

code {
  display: block;
  white-space: pre-wrap;
}

nav {
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  border-right: 2px solid #a9a9a9;
  min-width: 290px;
  min-height: 100%;
}

nav > header {
  margin: 1rem 0;
  text-align: center;
}

nav > ul {
  list-style-type: none;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
}

nav > ul > li {
  border-top: 1px solid #a9a9a9;
  border-bottom: 1px solid #a9a9a9;
  padding: 0.5rem 1rem;
}

.nav-link {
  text-decoration: none;
}

main {
  margin-left: 310px;
  padding: 0 20px;
}


I changed the ul height to: calc(100vh - 4em); . The Header of the navbar has a height of 2em (h1) and a padding top and bottom of 1em each equals 4em . That way the list has a definite height and can actually use a scrollbar to overflow. With height: 100% it will takes 100% of the contents height as height and therefor it will be impossible to actually overflow.

 * { margin: 0; padding: 0; } body { font-family: 'Open Sans', Arial; line-height: 1.5; } code { display: block; white-space: pre-wrap; } nav { box-sizing: border-box; display: block; position: fixed; top: 0; left: 0; border-right: 2px solid #a9a9a9; min-width: 290px; height: 100vh; } nav > header { margin: 1rem 0; text-align: center; } nav > ul { list-style-type: none; width: 100%; overflow-y: auto; height: calc(100vh - 4em); } nav > ul > li { border-top: 1px solid #a9a9a9; border-bottom: 1px solid #a9a9a9; padding: 0.5rem 1rem; }.nav-link { text-decoration: none; } main { margin-left: 310px; padding: 0 20px; }
 <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script> <nav id="navbar"> <header> <h1>JS Documentation</h1> </header> <ul> <li><a class="nav-link" href=#introduction>Introduction</a></li> <li><a class="nav-link" href=#what_you_should_know>What You Should Know</a></li> <li><a class="nav-link" href=#javascript_and_java>JavaScript and Java</a></li> <li><a class="nav-link" href=#hello_world>Hello World</a></li> <li><a class="nav-link" href=#variables>Variables</a></li> <li><a class="nav-link" href=#declaring_variables>Declaring Variables</a></li> <li><a class="nav-link" href=#variable_scope>Variable Scope</a></li> <li><a class="nav-link" href=#global_variables>Global Variables</a></li> <li><a class="nav-link" href=#constants>Constants</a></li> <li><a class="nav-link" href=#data_types>Data Types</a></li> <li><a class="nav-link" href=#if/else_statements>If/Else Statements</a></li> <li><a class="nav-link" href=#while_statements>While Statements</a></li> </ul> </nav>

make the height to 100% and for li remove border-bottom. Please check the following code.

nav {
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  border-right: 2px solid #a9a9a9;
  min-width: 290px;
  min-height: 100%;
  height: 100%;
}
nav > ul > li{
    border-top: 1px solid #a9a9a9;
    padding: 0.5rem 1rem;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM