4

my code is simple like this:

.list {
    margin: auto;
    height: 285px;
    width: 300px;
    overflow-y: scroll;
}
<div class="list">
  <ul>
    <li>hello world</li>
    <li>hello jupiter</li>
    <li>hello mars</li>
  </ul>
</div>

it shows style of a scroll on the side but lacking that scroll you use to move up and down?

lumio
  • 7,428
  • 4
  • 40
  • 56
gpbaculio
  • 5,693
  • 13
  • 60
  • 102

3 Answers3

1

You can get it by reducing height like following

.list {
  margin: auto;
  height: 70px;  /* Reduse height */
  width: 300px;
  overflow-y: scroll;
}
<div class="list">
  <ul>
    <li> hello world </li>
    <li> hello jupiter </li>
    <li> hello mars </li>
  </ul>
</div>
LKG
  • 4,152
  • 1
  • 11
  • 21
1

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.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56
  • 1
    my items was overflowing already and I cannot see some of it, I thought it would affect this div and I have to have same height as its' parent, but reducing height worked. thanks – gpbaculio Aug 21 '17 at 17:59
  • @iamnewbie Well, thanks for keeping your example code simple then. Glad we helped. – Jimenemex Aug 21 '17 at 18:04
0

you set a height of 285 pixels, which is more than enough for three list elements.

if you reduce the height to, say, 50 px or simply add more content inside the div, the scrollbar becomes active.

see https://jsfiddle.net/ppk10zf9/

<div class="list">
<ul>
<li> hello world </li>
<li> hello jupiter </li>
<li> hello mars </li>
<li> hello world </li>
<li> hello jupiter </li>
<li> hello mars </li>
<li> hello world </li>
<li> hello jupiter </li>
<li> hello mars </li>
</ul>
</div>

and

.list {
    margin: auto;
    height: 85px;
    width: 300px;
    overflow-y: scroll;
}
user3141593
  • 1,114
  • 1
  • 8
  • 7