How to scrollbar in html

How do you add a scroll bar to a div?

I have a popup that displays some results, and I want a scroll bar to be display since the results are being cutt off (and I don’t want the popup to be too long).

9 Answers 9

You need to add style=»overflow-y:scroll;» to the div tag. (This will force a scrollbar on the vertical).

If you only want a scrollbar when needed, just do overflow-y:auto;

Css class to have a nice Div with scroll

.DivToScroll < background-color: #F5F5F5; border: 1px solid #DDDDDD; border-radius: 4px 0 4px 0; color: #3B3C3E; font-size: 12px; font-weight: bold; left: -1px; padding: 10px 7px 5px; >.DivWithScroll

to add scroll u need to define max-height of your div and then add overflow-y

so do something like this

If you want to add a scroll bar using jquery the following will work. If your div had a id of ‘mydiv’ you could us the following jquery id selector with css property:

jQuery('#mydiv').css("overflow-y", "scroll"); 

@Chaki_Black great point. I added a similar answer to a question about dynamic scroll on a div here (stackoverflow.com/a/36314963/2512022)

This method is a bit outdated, even if dynamic selection is required. Modern DOM have more or less deprecated jQuery. See blog.garstasio.com/you-dont-need-jquery/selectors or any number of similar articles for supplemental information.

@webdestroya: It will only add scrolling to divs that have the classname of «scrollingDiv». That could be as few as 1, or even 0, or as many as all the divs the designer wishes to have scrollbars.

@Robusto — Yea, I meant gmcalab edited it. Originally it just said div in the spec, it didn’t have the class. Either way, I removed my comment because it is now fixed.

  div.scroll 
You can use the overflow property when you want to have better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better

If you have a CSS file, you can define a call for the element and assign the overflow: auto CSS property.

There are multiple ways to achieve a scroller on a div, here find a simple easy way one with an easy explanation .

Here is a bit explanation of the above code.

The overflow-y property of CSS adds a scroller when needed, If we want to add the scroller after a specific height (e.g 100px in our case) Then,

We add the max-height property of CSS so for example to add a scroller after 100px of height then max-height: 100px; is used.

I hope this explanation is helpful in clearing the concepts as well.

   div.scroll  
You can use the overflow property when you want to have better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better control of the layout. The default value is visible.better

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How TO — Custom Scrollbar

Note: Custom scrollbars are not supported in Firefox or in Edge, prior version 79.

How To Create Custom Scrollbars

Chrome, Edge, Safari and Opera support the non-standard ::-webkit-scrollbar pseudo element, which allows us to modify the look of the browser’s scrollbar.

The following example creates a thin (10px wide) scrollbar, which has a grey track/bar color and a dark-grey (#888) handle:

Example

/* width */
::-webkit-scrollbar width: 10px;
>

/* Track */
::-webkit-scrollbar-track background: #f1f1f1;
>

/* Handle */
::-webkit-scrollbar-thumb background: #888;
>

/* Handle on hover */
::-webkit-scrollbar-thumb:hover background: #555;
>

This example creates a scrollbar with box shadow:

Example

/* width */
::-webkit-scrollbar width: 20px;
>

/* Track */
::-webkit-scrollbar-track box-shadow: inset 0 0 5px grey;
border-radius: 10px;
>

/* Handle */
::-webkit-scrollbar-thumb background: red;
border-radius: 10px;
>

Scrollbar Selectors

For webkit browsers, you can use the following pseudo elements to customize the browser’s scrollbar:

  • ::-webkit-scrollbar the scrollbar.
  • ::-webkit-scrollbar-button the buttons on the scrollbar (arrows pointing upwards and downwards).
  • ::-webkit-scrollbar-thumb the draggable scrolling handle.
  • ::-webkit-scrollbar-track the track (progress bar) of the scrollbar.
  • ::-webkit-scrollbar-track-piece the track (progress bar) NOT covered by the handle.
  • ::-webkit-scrollbar-corner the bottom corner of the scrollbar, where both horizontal and vertical scrollbars meet.
  • ::-webkit-resizer the draggable resizing handle that appears at the bottom corner of some elements.

Источник

How To Create a Custom Scrollbar in CSS

By default, all browsers set a default scrollbar to a scrolling content box. In the below example, as you can see a default scrollbar at the right side of the scrolling box when you scroll the content.

Default Scrollbar Example.

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

In this article, we will create a custom scrollbar using css. The css ::-webkit-scrollbar pseudo class allows us to customize the browsers’ default scrollbar.

Custom Scrollbar Example.

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

The ::-webkit-scrollbar pseudo class is used to style the scrollbar (e.g. width).

textarea: :-webkit-scrollbar width: 15px; >

The -webkit-scrollbar-track is used to modify the look of the track (progress bar) of the scrollbar (we have set background-color to blue, in the above example.).

textarea: :-webkit-scrollbar-track background-color: green; >

We can use the ::-webkit-scrollbar-thumb pseudo class to customize the draggable scrolling handle (in the above example, we have set background-color to gold).

textarea: :-webkit-scrollbar-thumb background-color: red; border-radius:4px; >

Create a custom vertical scrollbar. #

The following example creates a 15px width scrollbar which has a gray track/bar and a black handle.

Css Custom Vertical Scrollbar Example. #

style> #textarea: :-webkit-scrollbar width: 15px; > #textarea: :-webkit-scrollbar-track background-color: gray; > #textarea: :-webkit-scrollbar-thumb background-color: black; > /style> div id="textarea" style="height:100px;background-color:#eee;overflow:auto;padding:4px;margin:8px auto"> div style="height:400px;background-color:#ddd;">/div> /div>

How to create a custom horizontal scrollbar. #

The following example creates a vertical and horizontal scrollbar. Click on the Try it Yourself » button to see how this looks like.

Custom Horizontal Scrollbar Example. #

style> #textarea: :-webkit-scrollbar width: 15px; height: 15px; > #textarea: :-webkit-scrollbar-track background-color:#ff7600; > #textarea: :-webkit-scrollbar-thumb background-color: blue; border-radius:50px; > /style> div id="textarea" style="height:100px;background-color:#eee;overflow:auto;padding:4px;margin:8px auto"> div style="height:400px;width:120%;background-color:#ddd;">/div> /div> 

Hide bottom right corner box of the scrollbar. #

You may see a square box of the scrollbar, where both horizontal and vertical scrollbars meet. To change its color or remove it, we can use the ::-webkit-scrollbar-corner pseudo class.

The following example creates a 10px width horizontal and vertical scrollbar which has linear-gradient background.

Источник

Читайте также:  Too many attempts php
Оцените статью