I have an element with position: absolute
in body
with some part of it to the left from left side.
* {
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:
- Why it happens so, why scrollbar doesn't appear (it works fine with symmetric layout on the right side)?
- Is it ever possible to make it appear and scroll to the left ?
- Am I missing anything very basic about scrollbars?
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:
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.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 propertydisplay: rtl;
.Of course this means the
#parent
element is always fully visible. If this was not wanted then the alternative is to set thedirection:rtl;
but ensure any content you want displayed correctly is then in a wrapper div which has CSSdirection: ltr;
to resume normal flow:In my example the
#parent
has a width of100px
, the same as the#test
div, however the#test
div has been pushed left50px
relative to#parent
and therefore the#test
div now requires150px
of space. The parent now has overflowing content and the CSSoverflow-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.