~/bootstrap/how-to-make-sticky-block-sidebar-bootstrap.md

How to Make Sticky Block in Sidebar using Bootstrap

1 min readSeptember 02, 2018by Gorav Singal#bootstrap#bootstrap-4-sticky#sticky-block
// TL;DR

Bootstrap 4's .sticky-top class handles most cases, but for cross-browser sticky sidebars you need a small JS fallback that calculates scroll position and toggles a fixed class.

How to Make Sticky Block in Sidebar using Bootstrap

Note: This is based on bootstrap-4

If you are using multi column design in your website, this may be applicable to you. Manytimes, if you have an article long enough to cover main column, but, the right or left column is just blank.

Example below shows the empty right bar. Webdesign Empty Column

Which doesn’t look a good user experience, you may want to fill some content.

Bootstrap Sticky Position

Bootstrap provides a solution to this problem, but this doesn’t work on some browser. See bootstrap documentation for sticky. But, the documentration is not having any example.

Make a block Sticky

Goto the “div” which you want to make sticky. And, add a class: “sticky-top”. Example:

<div class="card mb-2 sticky-top" style="top: 100px;">
...
</div>

I had to use style, as I’m using a fix static top navbar.

The beauty of this code is that, it is so simple yet powerful. Even, you have multiple blocks on top of this, it will wait for its turn, and make itself sticky when scrollbar goes past this. What I like more is, if you have a big footer, it stop itself going over there. As, in previous “affix or scrollspy” solution, you need to track the top and bottom positions. It work like a charm.

Code on GyanByte Github

See this complete html example:

Demo on CodePen To see things live, goto: GyanByte CodePen

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to Use Google Fonts in Website design with Bootstrap

How to Use Google Fonts in Website design with Bootstrap

If you are using Bootstrap, the default font that comes with the package is…

Claude Code Skills — Build a Better Engineering Workflow

Claude Code Skills — Build a Better Engineering Workflow

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

Making the leap from full-time employment to freelancing is one of the most…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…