There is no need to scroll.
The browser will only scroll if the content within the div
needs to scroll in order to see more items in the ul
. To make the scroll bar appear only if it needs to be there, you can use overflow-y: auto
.
You implicitly tell the browser to show to the scroll bar even though it's not needed with such little ul
elements. Try adding more elements to see the scroll bar work properly.
.list {
margin: auto;
height: 285px;
width: 300px;
overflow-y: auto; /* This changed */
}
<div class="list">
<ul>
<li> hello world </li>
<li> hello jupiter </li>
<li> hello mars </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello world </li>
<li> hello jupiter </li>
<li> hello mars </li>
</ul>
</div>
Deleting some of those li
elements will cause the scroll bar to shrink until it is not needed anymore.