Free Web Design Code & Scripts

Bootstrap 4 Sticky Sidebar On Scroll Event

Code Snippet:Basic Sticky Sidebar with Bootstrap 4
Author: Nicholas Cerminara
Published: 2 months ago
Last Updated: October 27, 2025
Downloads: 100
License: MIT
Edit Code online: View on CodePen
Read More

Here’s a guide on how to implement a Bootstrap 4 Sticky Sidebar On Scroll Event. This effect allows a sidebar to remain fixed on the screen as the user scrolls down the page, improving navigation and user experience. Follow these steps to create your own sticky sidebar.

Step 1: Include Bootstrap 4 CSS

First, you need to include the Bootstrap 4 CSS in the <head> section of your HTML document. This is essential for utilizing Bootstrap’s grid system and styling.

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

Step 2: Create the HTML Structure

Next, create the basic HTML structure for your page. This includes a container, a row, and columns for both the main content area and the sidebar.

<article>
    <div class="container-fluid">
        <div class="row">
            <div class="col">
                <div class="title-section">
                    <h1>Basic Sticky Sidebar with Bootstrap 4</h1>
                </div>
            </div>
        </div>
        <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="make-me-sticky">
                  <h3>Item 1</h3>
                </div>
              </div>
          

            </div>
        </div>
    </div>
</article>

Step 3: Add CSS Styling

Now, add the following CSS styles to control the positioning and appearance of the sidebar and its content. This includes making the sidebar sticky using position: sticky.

.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 */
}

.make-me-sticky {
  position: sticky;
  top: 0;
  padding: 0 15px;
}

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

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

.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;
}

That’s it! By following these steps, you should now have a fully functional Bootstrap 4 Sticky Sidebar that activates on the scroll event.

Loading... ...

Loading preview...

Device: Desktop
Dimensions: 1200x800
Lines: 0 Characters: 0 Ln 1, Ch 1

Leave a Comment

About W3Frontend

W3Frontend provides free, open-source web design code and scripts to help developers and designers build faster. Every snippet is reviewed before publishing for quality. Learn more.