bootstrap|September 02, 2018|1 min read

How to Make Sticky Block in Sidebar using Bootstrap

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 GyanBlog Github

See this complete html example:

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

Related Posts

Twig Templating - Most useful functions and operations syntax

Twig Templating - Most useful functions and operations syntax

Introduction Twig is a powerful template engine for php. Drupal uses it heavily…

ReactJS - How to pass method to component and how to pass arguments to method

ReactJS - How to pass method to component and how to pass arguments to method

Introduction In the ReactJS project, you are having aa parent component and a…

Git shortcuts - Helpful shortcuts for git users

Git shortcuts - Helpful shortcuts for git users

Github is an awesome place of storing your code. Now, it also allows you to have…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

How to take Backup from MongoDB and Restore to MongoDB

How to take Backup from MongoDB and Restore to MongoDB

This will take backup of your passed database name, to the passed folder. It…

Python - How to apply patch to Python and Install Python via Pyenv

Python - How to apply patch to Python and Install Python via Pyenv

Introduction In this post, we will see how we can apply a patch to Python and…

Latest Posts

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…

Deep Dive on Caching: From Browser to Database

Deep Dive on Caching: From Browser to Database

“There are only two hard things in Computer Science: cache invalidation and…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…