How to Force a Vertical Scrollbar to Appear

How do I force a vertical scrollbar to appear?

Give your body tag an overflow: scroll;

body {
overflow: scroll;
}

or if you only want a vertical scrollbar use overflow-y

body {
overflow-y: scroll;
}

Force show vertical scroll bar on table

Some version of OSX hide scrollbars when not needed.
They probably hope to make the webpage slicker.
Try including this in your CSS file:

::-webkit-scrollbar {
-webkit-appearance: none;
width: 7px;
}
::-webkit-scrollbar-thumb {
border-radius: 4px;
background-color: rgba(0,0,0,.5);
-webkit-box-shadow: 0 0 1px rgba(255,255,255,.5);
}

JSFiddle

Source

How to always show the vertical scrollbar in a browser?

jQuery shouldn't be required. You could try adding the CSS:

body    {overflow-y:scroll;}

This works across the latest browsers, even IE6.

Div vertical scrollbar show

What browser are you testing in?

What DOCType have you set?

How exactly are you declaring your CSS?

Are you sure you haven't missed a ; before/after the overflow-y: scroll?

I've just tested the following in IE7 and Firefox and it works fine

<!-- Scroll bar present but disabled when less content -->

<div style="width: 200px; height: 100px; overflow-y: scroll;">

test

</div>

<!-- Scroll bar present and enabled when more contents -->

<div style="width: 200px; height: 100px; overflow-y: scroll;">

test<br />

test<br />

test<br />

test<br />

test<br />

test<br />

test<br />

test<br />

test<br />

test<br />

</div>

Force vertical scrollbar to display in IE8

Oh figured it. Its

body {
overflow-y: scroll;
}

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.

Why vertical scrollbar doesn't appear although vertical overflow is set?

Setting the value to scroll should do the trick:

$('body').css('overflowY', 'scroll');

It essentially forces a scrollbar to appear regardless of overflowing content or not.

EDIT - Alternatively, try adding a class when the window height goes below:

https://jsfiddle.net/0Lhwfjk6/

I've simplified the examle slightly for this case. You'll need to resize the actual browser window.

jQuery:

/**
* Created by eduard on 07.01.2016.
*/
console.log("Height 1 ", sidebar_height);

if ($(window).height() < 300) {
$('.content').addClass('scrollbar');
//$("#sidebar").css('overflowY', 'auto');
console.log("Scrollbar should appear", $(window).height() - sidebar_height);
}
else {
console.log("Scrollbar should not appear", $(window).height() - sidebar_height);
}

CSS:

.scrollbar {
overflow-y: scroll;
}


Related Topics



Leave a reply



Submit