Sheldon Chapel Ruins near Yemassee, South Carolina.
760
This
gives a div that the user can scroll in both in horizontally and vertically. How do I change it so that the div is only scrollable vertically?
htmlcss
Share
Improve this question
Follow
edited Dec 17, 2015 at 7:49
Naigel's user avatar
Naigel
9,7241717 gold badges7373 silver badges110110 bronze badges
asked Mar 14, 2012 at 17:56
Saswat's user avatar
Saswat
12.9k1818 gold badges8484 silver badges161161 bronze badges
57
There's overflow-x and overflow-y in CSS3, which do what you want. –
Marc B
Commented Mar 14, 2012 at 17:57
make sure your element doesn't have pointer-events: none; because it will disable the scrolling –
kafinsalim
Commented Mar 16, 2021 at 4:46
Add a comment
10 Answers
Sorted by:
887
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.
Share
Improve this answer
Follow
edited Mar 18, 2014 at 19:29
darkAsPitch's user avatar
darkAsPitch
1,87544 gold badges2323 silver badges3737 bronze badges
answered Mar 14, 2012 at 18:12
animuson's user avatar
animuson♦
54.8k2828 gold badges142142 silver badges150150 bronze badges
8
using overflow: auto somehow creates a gigantic empty block of space at the bottom of the page. Is this a common occurrence? –
Stupid.Fat.Cat
Commented Oct 3, 2016 at 23:26
Add a comment
396
Try like this.
Share
Improve this answer
Follow
edited Jun 17, 2022 at 13:44
ChrisF's user avatar
ChrisF
137k3131 gold badges265265 silver badges335335 bronze badges
answered Mar 14, 2012 at 17:59
Milap's user avatar
Milap
7,23988 gold badges2727 silver badges4747 bronze badges
2
Good solution, but the scroll is always visible regardless of height –
FindOutIslamNow
Commented Jun 25, 2018 at 13:46
13
if you set the overflow-y value to 'auto', the scroll will be visible afte
Umair Gul
Commented Oct 21, 2018 at 8:19
5
You shouldn't be plugging your youtube channel in answers if it's not related to the topic at hand. –
rebane2001
Commented Jun 17, 2022 at 13:28
Add a comment
189
For 100% viewport height use:
overflow: auto;
max-height: 100vh;
Share
Improve this answer
Follow
edited Oct 21, 2015 at 12:43
answered May 29, 2015 at 12:57
VVS's user avatar
VVS
2,24711 gold badge1515 silver badges1313 bronze badges
1
Thanks for the max-height. It's needed more often in my use case. –
FarhanShares
Commented Nov 8, 2020 at 7:33
Add a comment
63
Use overflow-y: auto; on the div.
return to the entrance