Horizontal scrollbar appears with overflow content on right but not on left?
Asked Answered
L

4

12

I have an element with position: absolute in body with some part of it to the left from left side.

Demo fiddle

* {
  overflow: visible;
}

#test {
  position: absolute;
  height: 100px;
  width: 100px;
  left: -50px;
  background-color: lime;
}
<div id="test"></div>

I expect that horizontal scrollbar should appear allowing to scroll the document to the hidden part of the element, but it doesn't. So, I have some questions about the case:

  1. Why it happens so, why scrollbar doesn't appear (it works fine with symmetric layout on the right side)?
  2. Is it ever possible to make it appear and scroll to the left ?
  3. Am I missing anything very basic about scrollbars?
Laser answered 23/12, 2016 at 12:33 Comment(0)
T
7

In order to have scrollbars appear, the parent element must have a set width. For horizontal scrollbars using the property overflow-x:scroll; on the parent will make the scrollbar appear.

Sounds similar to this issue: Div with horizontal scrolling only

To try and answer your questions:

  1. The scrollbar appears when you use right: -50px because the standard flow of a HTML document is left to right. The CSS pushed the #test div out and the browser is able to continuing rendering to the right. It may look like part of the div is outwith the body at this point but it is not. Everything visible on an HTML page must be within the body.

  2. Using the CSS display: rtl; on a parent container or the body would make the scrollbars scroll left instead of right, however if you did this to the body the whole document would now work right to left, changing all positioning in the page. I'd suggest having a parent element with the CSS property display: rtl;.

#parent {
  position: static;
  height: 100px;
  width: 100px;
  overflow-x: scroll;
  overflow-y: hidden;
  background-color: red;
  direction: rtl;
}

#test {
  width: 100px;
  height: 100px;
  background-color: lime;
  position: relative;
  left: -50px;
}
<div id="parent">
  <div id="test">
  </div>
</div>

Of course this means the #parent element is always fully visible. If this was not wanted then the alternative is to set the direction:rtl; but ensure any content you want displayed correctly is then in a wrapper div which has CSS direction: ltr; to resume normal flow:

body {
  direction: rtl;
  overflow: scroll;
}

#allOtherContent {
  direction: ltr;
  width: 100%;
  background-color: red;
}

#test {
  width: 100px;
  height: 100px;
  background-color: lime;
  position: absolute;
  left: -50px;
}
<div id="test"></div>
<div id="allOtherContent"></div>
  1. Scrollbars appear when elements within another element are too big to fit. For this to work the browser needs to know the exact size of the parent element to determine that the test element is too big, and therefore needs to scroll.

In my example the #parent has a width of 100px, the same as the #test div, however the #test div has been pushed left 50px relative to #parent and therefore the #test div now requires 150px of space. The parent now has overflowing content and the CSS overflow-x:scroll; adds our horizontal scrollbar.

More on how overflow and scrollbars work can be found here: https://developer.mozilla.org/en/docs/Web/CSS/overflow.

Hope that helps answer your questions.

Typeset answered 23/12, 2016 at 12:36 Comment(1)
Nope, it doesn't work. Scrollbar is there, but no scrolling to the leftLaser
W
1

Adding overflow-x: scroll; to the #test div will give you a horizontal scrollbar.

Working Fiddle

Waers answered 23/12, 2016 at 12:40 Comment(1)
What are you trying to accomplish Vasily? Move the green div into view with a bottom scroll bar?Columbarium
L
1

You should be follow this css code

*{
  overflow: visible;
}
#test{
  position: absolute;
  height: 100px;
  width: 100px;
  overflow-y:scroll;
  left: -50px;
  background-color: lime;
}
Leper answered 23/12, 2016 at 12:48 Comment(5)
Nope, it's basically the same I haveLaser
What browser do you use? Chrome / Firefox / IE9 all do not work.Laser
I am using Chrome / Firefox.Leper
Sorry Vaslily. It's my mistake. You asking me about horizontal scrollbar and I am answering you about vertical scrollbar. I updated my code.Leper
For Horizontal scrollbar appears this is basic css structure.Leper
E
1

You need to change the direction from left to right (default) to right to left in the scrollable div.

direction: rtl;

This behaviour is due to the CSS direction property.

The direction property in CSS sets the direction of of content flow within a block-level element. This applies to text, inline, and inline-block elements. It also sets the default alignment of text and the direction that table cells flow within a table row.

That is why it is working when the negative value is in the right rather than the left because the flow goes from left to right (ltr) and the negative right makes the scrollable area adapt.

body {
  overflow: auto;
  direction: rtl;
}

#test {
  position: absolute;
  height: 100px;
  width: 100px;
  left:-50px;
  background-color: lime;
}
<div id="test"></div>
Elation answered 23/12, 2016 at 13:7 Comment(7)
Well, it's a trick that does show the scrollbar, but the target block here has no parts positioned outside of its container. I need scroll that shows element not fully contained inside the body.Laser
Ok, so please check the edited solution and let me know if it fits better.Elation
Same thing, #test is positioned partially outside of #container but both #test and #container are completely within body. There is nothing that is positioned outside of the body rectangle.Laser
You can not scroll in the same container outside of itself.Elation
Well, seems logical, but same thing with right worksLaser
Could you post a fiddle showing what you mean with "right works"?Elation
Let us continue this discussion in chat.Laser

© 2022 - 2024 — McMap. All rights reserved.