skip to Main Content

I tried to make my sidebar sticked at the top when everytime I scrolled but I dunno why it won’t stick at the top.

Here’s what I did and I expect it to sticked at the top when scrolled.

See my code below:

<section id="blog" class="blog">
      <div class="container">

        <div class="row">
          <div class="col-lg-8">...</div>

          <div class="col-lg-4">
            <div class="sidebar">
              I'm trying to stick this column at the top when scrolled.
            </div>
          </div>
        </div>

      </div>
    </section>

CSS:
.blog .sidebar {
  position: sticky;
  top: 0;
  right: 0;
  overflow-y: auto;
}

3

Answers


  1. The <section> needs to be sticky as well.

    .blog, .blog .sidebar {
      position: sticky;
      top: 0;
      right: 0;
      overflow-y: auto;
    }
    <section id="blog" class="blog">
      <div class="container" >
    
        <div class="row">
          <div class="col-lg-8">...</div>
    
          <div class="col-lg-4">
            <div class="sidebar">
              I'm trying to stick this column at the top when scrolled.
            </div>
          </div>
        </div>
    
      </div>
    </section>
    <div style="height: 200vh">
        For testing...
    </div>
    Login or Signup to reply.
  2. It really depends on what that row class is doing. For example if it’s a flex container that displays its child elements in a row like the example below then your sticky rule will work (with a minor adjustment).

    .row { display: flex; justify-content: space-between;}
    .main, .sidebar { padding: 0.25rem; border: 1px solid #555; border-radius: 5px; }
    .main { height: 200vh; width: 80%; top: 10px; }
    .sidebar { margin-left: auto; position: sticky; top: 10px; width: 80%; }
    <section id="blog" class="blog">
      <div class="container">
        <div class="row">
          <div class="col-lg-8 main">"Lorem ipsum dolor sit amet, consectetur adipiscing 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 class="col-lg-4">
            <div class="sidebar">
              I'm trying to stick this column at the top when scrolled.
            </div>
          </div>
        </div>
      </div>
    </section>
    Login or Signup to reply.
  3. your problem is you don’t have a parent element with position: relative. so to solve your problem you need to set:

    .blog {
      position: relative;
    }
    

    and sidebar:

    .sidebar {
      position: sticky;
      top: 0;
      right: 0;
      overflow-y: auto;
    }
    

    hope it will help you. Let me know the result

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search