How do I make the scrollbar on a div only visible when necessary?
Use overflow: auto
. Scrollbars will only appear when needed.
(Sidenote, you can also specify for only the x, or y scrollbar: overflow-x: auto
and overflow-y: auto
).
CSS hide scroll bar if not needed
Set overflow-y
property to auto
, or remove the property altogether if it is not inherited.
how to hide a vertical scroll bar when not needed
overflow: auto
(or overflow-y: auto
) is the correct way to go.
The problem is that your text area is taller than your div. The div ends up cutting off the textbox, so even though it looks like it should start scrolling when the text is taller than 159px
it won't start scrolling until the text is taller than 400px
which is the height of the textbox.
Try this: http://jsfiddle.net/G9rfq/1/
I set overflow:auto on the text box, and made the textbox the same size as the div.
Also I don't believe it's valid to have a div
inside a label
, the browser will render it, but it might cause some funky stuff to happen. Also your div
isn't closed.
Make scrollbars only visible when a Div is hovered over?
div { height: 100px; width: 50%; margin: 0 auto; overflow: hidden;}
div:hover { overflow-y: scroll;}
<div> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p></div>
make a scrollbar always visible in a div - chrome
Just having the scrollbar visible will not allow you to react to the user trying to scroll down. So you will need to actually make the content flow outside of the area and detect the scroll.
Try this:
#scrollarea-invalid {
overflow-y: scroll;
height: 350px;
}
#scrollarea-content{
min-height:101%;
}
<div id='scrollarea-invalid'>
<div id='scrollarea-content'></div>
</div>
Css overflow-y: how to show only when needed
You can set it to auto
overflow-y : auto;
It will only show scroll bar in case of overflow.
Refer : What browsers support `overflow-y`?
Hide scroll bar, but while still being able to scroll
Just a test which is working fine.
#parent{
width: 100%;
height: 100%;
overflow: hidden;
}
#child{
width: 100%;
height: 100%;
overflow-y: scroll;
padding-right: 17px; /* Increase/decrease this value for cross-browser compatibility */
box-sizing: content-box; /* So the width will be 100% + 17px */
}
Working Fiddle
JavaScript:
Since the scrollbar width differs in different browsers, it is better to handle it with JavaScript. If you do Element.offsetWidth - Element.clientWidth
, the exact scrollbar width will show up.
JavaScript Working Fiddle
Or
Using Position: absolute
,
#parent{
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
#child{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: -17px; /* Increase/Decrease this value for cross-browser compatibility */
overflow-y: scroll;
}
Working Fiddle
JavaScript Working Fiddle
Information:
Based on this answer, I created a simple scroll plugin.
Related Topics
CSS Media Queries and Size of Images
Sass Image CSS Cache Busting (Via Compass)
Sass "Error: Can't Find Stylesheet to Import."
CSS "Height: [Percentage]" Behaviour Using "Margin" And/Or "Padding"
CSS Table and Max-Width in Chrome Not Working
How to Overlay a Div Over a Canvas CSS
Accessing The Shadow Dom Elements in Cypress
How to Use Non Standard CSS Selectors in a CSSresource
Stylesheet Not Parsed Because Non CSS Mime Types Are Not Allowed in Strict Mode
Very Different Font Sizes Across Browsers
Where to Add Global CSS in an Angular 2 Application Made with Visual Studio 2017 Angular Template
Why Is My CSS3 Animation Not Working in Chrome or Safari
Angular2 Sub Component Break CSS Relationship
Ckeditor Add CSS Styling to Preview and Editor