How to Add a Scroll Bar to a Div

Making a div vertically scrollable using CSS

You have it covered aside from using the wrong property. The scrollbar can be triggered with any property overflow, overflow-x, or overflow-y and each can be set to any of visible, hidden, scroll, auto, or inherit. You are currently looking at these two:

  • auto - This value will look at the width and height of the box. If they are defined, it won't let the box expand past those boundaries. Instead (if the content exceeds those boundaries), it will create a scrollbar for either boundary (or both) that exceeds its length.

  • scroll - This values forces a scrollbar, no matter what, even if the content does not exceed the boundary set. If the content doesn't need to be scrolled, the bar will appear as "disabled" or non-interactive.

If you always want the vertical scrollbar to appear:

You should use overflow-y: scroll. This forces a scrollbar to appear for the vertical axis whether or not it is needed. If you can't actually scroll the context, it will appear as a"disabled" scrollbar.

If you only want a scrollbar to appear if you can scroll the box:

Just use overflow: auto. Since your content by default just breaks to the next line when it cannot fit on the current line, a horizontal scrollbar won't be created (unless it's on an element that has word-wrapping disabled). For the vertical bar,it will allow the content to expand up to the height you have specified. If it exceeds that height, it will show a vertical scrollbar to view the rest of the content, but will not show a scrollbar if it does not exceed the height.

add scrollbar to div instead of scrolling the whole page

Try this code, to make the height fixed and show the scrollbar:

.logsContainer {
height: 200px;
overflow-y: scroll;
overflow-x:hidden;
}

Add scroll bar in div inside a table

You could use the overflow property:

#wrapper {
height: 100%;
width: 200px;
overflow: scroll;
}

The overflow property specifies what happens if content overflows an element's box.

This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area.

Note: The overflow property only works for block elements with a specified height.

Further reading - W3Schools
Further reading - MDN

Bootstrap4 adding scrollbar to div

Yes, It is possible,
Just add a class like anyclass
and give some CSS style. Live

.anyClass {
height:150px;
overflow-y: scroll;
}

.anyClass {

height:150px;

overflow-y: scroll;

}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet"/>

<div class=" col-md-2">

<ul class="nav nav-pills nav-stacked anyClass">

<li class="nav-item">

<a class="nav-link active" href="#">Active</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li><li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li><li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li><li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li><li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#">Link</a>

</li>

<li class="nav-item">

<a class="nav-link disabled" href="#">Disabled</a>

</li>

</ul>

</div>

How to add scrollbar to parent div in html css?

It does make sense.
The CSS code you have provided renders a page like this

Sample Image

Here width of both parent and child are same, so you are not able to observe the overflow
Make a slight change to the width of the child so that the parent is visible like this

.parentDiv {
background-color: coral;
height: 50px;
width: 200px;
}

.childDiv {
background-color: crimson;
height: 100px;
width: 100px;
}

Now the output will be

Sample Image

As we can see the 'pink colored element' is the child which has a greater height property value than the parent is overflowing.

By default the overflow value is visible. So we can see the element overflowing the parent.

Because the child is overflowing the parent.
We must define a overflow property on the parent.

As the document you have linked from w3 says, if the overflow property is set to auto, it will add a scrollbar if the content is overflowing

Here is a codepen demonstrating the same.

For better understanding I have altered the width and margin of the child.

Please let me know, If this justifies the statement

How to make a scroll bar in separate div in body of html

You can use overflow:scroll in you css for your scroll_div div.
The overflow property specifies what happens if content overflows an element's box.

This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area.

Note: The overflow property only works for block elements with a specified height.

div.scroll

{

width:50%;

height:100px;

overflow:scroll;

float: left;

}
<div class="scroll">The overflow property specifies what happens if content overflows an element's box.

<br/>

This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area.

<br/>

Note: The overflow property only works for block elements with a specified height. </div>


Related Topics



Leave a reply



Submit