How would you get an element/div to stay in place (WITHOUT position: fixed;) while you scroll? (Actually, this page is a perfect example. The left side with the buttons do not move, while you can scroll this page where the content of this/my post is.)
Say, you have two divs, both are inline-block. The left div you want to stay in place. You only want to allow scrolling on the right div. (different tracks). position: fixed; doesn’t really give me the result I’m looking for. Open to any solutions in CSS, vanilla JS, and/or jQuery. Here’s some code that I’ve been trying:
HTML:
<div class="container">
<div class="left">
<!-- Content -->
</div>
<div class="right">
<!-- Content -->
</div>
</div>
CSS:
.left {
display: inline-block;
vertical-align: top;
background-color: #F9F9F9;
border-right: 2px solid $blueBorder;
padding: 0.5%;
overflow-x: hidden;
overflow-y: hidden;
}
.right {
display: inline-block;
vertical-align: top;
background-color: gray;
padding: 0.5%;
}
jQuery:
$(window).scroll(function() {
$('#left').css('top', $(this).scrollTop() + "px");
});
3
Answers
This can be done with pure css. As you pointed out, this page is an example. If you dig into it with your browser dev tools you will find something similar to this below.
Another way I have done things in the past would be to scroll just the right content div and include the footer over there (not full width). Like below.
Use sematic elements like
section
andaside
tags in html instead of div so it will make more friendly.Use w3 as reference
What’s cool is you can just inspect this website using Right-Clicking and just look at StackOverflow’s Code. They’re going the Sticky Method, I’ve done a barebones version below
edit, grammar/spelling