Bootstrap 4 Sticky Sidebar on Scroll Event

Bootstrap 4 Sticky Sidebar on Scroll Event
Code Snippet:Basic Sticky Sidebar with Bootstrap 4
Author: Nicholas Cerminara
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 14,626
License: MIT
Edit Code online: View on CodePen
Read More

Do you want to make the sidebar widget item sticky on scroll? well! you can do it using this code snippet. This Bootstrap 4 code snippet creates a sticky sidebar item on scroll event.

If you are looking for the quick answer to make a sticky item in your existing sidebar, then you just need to add a class name “make-me-sticky” to that item. Then define the CSS style as follows:

.make-me-sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0; 
}

Follow the following guide if you want to make a basic Bootstrap page layout (from scratch) with a sticky sidebar.

How to create Bootstrap Sticky Sidebar

1. In the very first step, load the Bootstrap CSS by adding the following CDN link into your webpage.

<!-- Bootstrap CSS -->
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css'>

2. Create the basic page layout (header, content section, and sidebar) in HTML as follows:

<article>
    <div class="container-fluid">
        <div class="row">
            <div class="col">
                <div class="title-section">
                    <h1>Bootstrap 4 Sticky Sidebar on Scroll Example</h1>
                </div>
            </div>
        </div>
        <div class="container">
        <div class="row">
            <div class="col-7">

                <div class="content-section">
                    <h2>Content Section</h2>
                </div>
            </div>
            <div class="col-5">

              <div class="sidebar-item">
                  <div class="widget">
                  <h3> Item 1</h3> 
                  </div>
                  
                <div class="widget make-me-sticky">
                  <h3 style="background: pink">Item 2 (sticky)</h3>
                  
                </div>
                <div class="widget make-me-sticky">
                  <h3>Item 3 (sticky)</h3>
                </div>
              </div>
            </div>
        </div>
     </div>
    </div>
</article>

3. Add the following custom CSS to style the sidebar. The "make-me-sticky" class uses the CSS sticky property to fixed the (widget) item on the top on scroll.

.content-section {
  min-height: 2000px;
}

.sidebar-section {
  position: absolute;
  height: 100%;
  width: 100%;
}

.sidebar-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* Position the items */
}

.widget{
    padding: 0 15px;
    margin: 15px 0;
}

.make-me-sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0; 
}

/* Ignore This, just coloring */
body {
  background: #fff;
}

article {
  background: #f1f1f1;
  border-radius: 12px;
  padding: 25px;
}

.title-section, .content-section, .sidebar-section {
  background: #fff;
}

.title-section {
  text-align: center;
  padding: 50px 15px;
  margin-bottom: 30px;
}

.content-section h2 {
  text-align: center;
  margin: 0;
  padding-top: 200px;
}

.sidebar-item {
  text-align: center;
}
.sidebar-item h3 {
  background: gold;
  max-width: 100%;
  margin: 0 auto;
  padding: 50px 0 100px;
  border-bottom: solid 1px #fff;
}

Hopefully, you have successfully created Bootstrap 4 sticky sidebar on the scroll. If you have any questions let me know by comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X