How to enable CSS scroll both X and Y

Viewed 190

I've searched few hours, but cannot got an answer, So I need some help

There is a div element, provides scroll-x

<div id='parent'>
   <div id='el1'></div>
   <div id='el2'></div>
   <div id='el3'></div>
</div>

When my mouse cursor locates on that div area, scrolling y does not work

Had tried =>

#id overflow: auto
#id overflow-x: auto, overflow-y: auto
#id overflow-x: scroll, overflow-y: scroll

How can I solve it?

3 Answers

Here, this can give you some idea:

#el1, #el2, #el3 {
  width: 200px;
  height: 200px;
  background: lightblue;
  display:inline-block;
}

.parent {
  width:100px;
  height:100px;
  border: 2px dashed grey;
  overflow: auto;
}
<div class="parent">
   <div id='el1'>a</div>
   <div id='el2'>b</div>
   <div id='el3'>c</div>
</div>

I this snippet working the way you want?

The key is on setting the height and width of your parent and children elements.

A horizontal scroll (X-axis) can be achieved if the total width of the children's elements is bigger than the width of the parent element plus using overflow: auto to the parent element.

A vertical scroll (Y-axis) can be achieved if the total height of the children's elements is bigger than the height of the parent element plus using overflow: auto to the parent element.

Here is the working code: https://codesandbox.io/s/kind-gates-djhnx?file=/src/styles.css

html file:

<div id="parent">
  <div id="child-1">
    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>
  <div id="child-2">
    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>
  <div id="child-3">
    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>
</div>

css file:

#parent {
  overflow: auto;
  height: 300px;
  width: 300px;
  background-color: gainsboro;
}

#child-1 {
  overflow: auto;
  width: 500px;
  background-color: lightblue;
}

#child-2 {
  overflow: auto;
  width: 500px;
  background-color: lightgreen;
}

#child-3 {
  overflow: auto;
  width: 500px;
  background-color: lightcoral;
}

Here is a working example on overflow, you have to set height and width property in #parent plus you have to set overflow property to auto

if the width of any children is greater than parent's width, then it will automatically scroll on x direction

if the total height of all children is greater than parent's height, then it will automatically scroll on y direction

#parent{
  background:#333;
  width:300px;
  height:300px;
  overflow:auto;
}

#el1{
  background:#999;
  height:200px;
  width:200px;
}

#el2{
  background:green;
  height:400px;
  width:400px;
}

#el3{
  background:yellow;
  height:400px;
  width:400px;
}
<div id='parent'>
   <div id='el1'></div>
   <div id='el2'></div>
   <div id='el3'></div>
</div>

Related