I have a position: fixed
div in a layout, as a sidebar. I've been asked to have part of it's content stay fixed to the top of it (internally), and the rest to scroll if it overflows the bottom of the div.
I've had a look at this answer, however the solution presented there doesn't work with position: fixed
or position: absolute
containers, which is a pain.
I've made a JSFiddle demonstration of my problem here. The large amount of text should ideally scroll, instead of overflowing into the bottom of the page. The height of the header can vary with content, and may be animated.
body { background: #eee; font-family: sans-serif; } div.sidebar { padding: 10px; border: 1px solid #ccc; background: #fff; position: fixed; top: 10px; left: 10px; bottom: 10px; width: 280px; } div#fixed { background: #76a7dc; padding-bottom: 10px; color: #fff; } div#scrollable { overlow-y: scroll; }
<div class="sidebar"> <div id="fixed"> Fixed content here, can be of varying height using jQuery $.animate() </div> <div id="scrollable"> Scrolling content<br><br> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </div>
Without a fixed header, I can simply add overflow-y: scroll
to div.sidebar
and I can happily scroll all it's content if it overflows the bottom of the container. However, I'm running into issues with having a fixed, variable height header at the top of the sidebar, and having any content underneath that scroll if it's too long to fit into the container.
div.sidebar
must stay position: fixed
, and I would very much like to do this without any hacks, as well as make it as cross browser as possible. I've attempted various things, but none of them work, and I'm unsure as to what to try from here.
How can I make a div inside a position: fixed
container scroll only in the Y direction when it's content overflows the containing div, with a fixed header of varying, indeterminate height? I'd very much like to stay away from JS, but if I have to use it I will.
For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.
To fit all the text inside the div, the bi-directional scrolling method will be used. You can apply it by placing the overflow:scroll and white-space:nowrap in the id lifestyle inside the style tag. Notice the scroll bar at the bottom and right side of the div .
// get the "Div" inside which you wish to scroll (i.e. the container element) const El = document. getElementById('xyz'); // Lets say you wish to scroll by 100px, El. scrollTo({top: 100, behavior: 'smooth'}); // If you wish to scroll until the end of the container El. scrollTo({top: El.
It seems to work if you use
div#scrollable { overflow-y: scroll; height: 100%; }
and add padding-bottom: 60px
to div.sidebar
.
For example: http://jsfiddle.net/AKL35/6/
However, I am unsure why it must be 60px
.
Also, you missed the f
from overflow-y: scroll;
What worked for me :
div#scrollable { overflow-y: scroll; max-height: 100vh; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With