721

This

<div id="" style="overflow:scroll; height:400px;">

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?

Naigel
  • 9,086
  • 16
  • 65
  • 106
Saswat
  • 12,320
  • 16
  • 77
  • 156

10 Answers10

855

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.

darkAsPitch
  • 1,855
  • 4
  • 23
  • 35
animuson
  • 53,861
  • 28
  • 137
  • 147
  • 7
    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 Oct 03 '16 at 23:26
364

Try like this.

<div style="overflow-y: scroll; height:400px;">
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Milap
  • 6,915
  • 8
  • 26
  • 46
179

For 100% viewport height use:

overflow: auto;
max-height: 100vh;
VVS
  • 2,147
  • 1
  • 13
  • 13
60

Use overflow-y: auto; on the div.

Also, you should be setting the width as well.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
37

You can use this code instead.

<div id="" style="overflow-y:scroll; overflow-x:hidden; height:400px;">


overflow-x: The overflow-x property specifies what to do with the left/right edges of the content - if it overflows the element's content area.
overflow-y: The overflow-y property specifies what to do with the top/bottom edges of the content - if it overflows the element's content area.

Values
visible: Default value. The content is not clipped, and it may be rendered outside the content box.
hidden: The content is clipped - and no scrolling mechanism is provided.
scroll: The content is clipped and a scrolling mechanism is provided.
auto: Should cause a scrolling mechanism to be provided for overflowing boxes.
initial: Sets this property to its default value.
inherit Inherits this property from its parent element.

divykj
  • 576
  • 6
  • 12
18

You can use overflow-y: scroll for vertical scrolling.

<div  style="overflow-y:scroll; height:400px; background:gray">
  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.
  
  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.
  
  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.
  
  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.
  
  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.
  
 </div>
Santosh Khalse
  • 12,002
  • 3
  • 36
  • 36
11

The problem with all of these answers for me was they weren't responsive. I had to have a fixed height for a parent div which i didn't want. I also didn't want to spend a ton of time dinking around with media queries. If you are using angular, you can use bootstraps tabset and it will do all of the hard work for you. You'll be able to scroll the inner content and it will be responsive. When you setup the tab, do it like this: $scope.tab = { title: '', url: '', theclass: '', ative: true }; ... the point is, you don't want a title or image icon. then hide the outline of the tab in cs like this:

.nav-tabs {
   border-bottom:none; 
} 

and also this .nav-tabs > li.active > a, .nav-tabs > li.active > a:hover, .nav-tabs > li.active > a:focus {border:none;} and finally to remove the invisible tab that you can still click on if you don't implement this: .nav > li > a {padding:0px;margin:0px;}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Post Impatica
  • 14,999
  • 9
  • 67
  • 78
2

Well the above answers have give a good explanations to half of the question. For the other half.

Why don't just hide the scroll bar itself. This way it will look more appealing as most of the people ( including me ) hate the scroll bar. You can use this code

::-webkit-scrollbar {
    width: 0px;  /* Remove scrollbar space */
    background: transparent;  /* Optional: just make scrollbar invisible */
}
Himanshu Tariyal
  • 264
  • 6
  • 13
1

Refer to this gist: https://gist.github.com/zeoneo/5f9fff92a12aa6d9d42065b5df68e9d5

It might help you to create a layout using flex and having self scrollable split panes.

body {
                background-color: aquamarine;
                margin: 0;
                padding: 0;
            }
            .container {
                height: 100vh;
                display: flex;
                flex-direction: column;
                background-color: bisque;
            }
            .left {
                width: 300px;
                background-color: lightblue;
                overflow: auto;
                scroll-behavior: smooth;

            }
            .right {
                flex:1;
                background-color: lightcoral;
                overflow: auto;
                scroll-behavior: smooth;
            }
            .sidebar-item {
                display: block;
                height: 100px;
                background-color: lightseagreen;
                margin: 5px;
            }
            .header {
                display: block;
                height: 100px;
                flex:none;
                background-color: aqua;
            }
            .content {
                flex:1;
                background-color: brown;
                display: flex;
                overflow: auto;
            }
<html>
    <head>
        <title>Hello World</title>

    </head>
    <body>
        <div class="container">
            <div class="header">
                Header
            </div>
            <div class="content">

            <div class="left myscroll">
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
            </div>
            <div class="right">
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
                <div class="sidebar-item"></div>
            </div>
        </div>

        </div>
    </body>
</html>
zero
  • 150
  • 1
  • 16
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/29199532) – Dominique Jun 15 '21 at 12:38
  • @Dominique Could you please check if this meets the quality requirement. – zero Aug 16 '21 at 09:05
1

I have added some comments in the code for explanations. And here is some reference : https://www.w3schools.com/css/css_overflow.asp

div {
  overflow-y: auto; /* the auto value is similar to scroll, but it adds scrollbars only when necessary */
  word-break: keep-all; /* this is optional, so the words keep in one line */
  white-space: nowrap; /* this is optional, so the div can expands to the side */
}
<div>
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.
</div>
dita
  • 136
  • 4