Create a Reading Scroll Progress Bar for Your Blog in JavaScript and CSS

<!-- **This post is originally from [](, a blog about CSS, JavaScript, and just about anything programming.** -->

Go check out Daily Developer Jokes, my latest project!

Daily Developer Jokes Website Follow Daily Developer Jokes on DEV

Here's the joke from today:

Daily Developer Jokes, Friday Feb. 5, 2020

I just recently added a fun little feature on my website at a progress bar when reading blog posts. The bar would show how far users have progressed in reading a post, from 0% at the beginning to when a user finishes reading at 100%.

This little feature has become particularly popular among other blogs and Wordpress themes in recent years. For example, the popular tech publication TechCrunch uses a circular scroll progress bar, and many other sites have a similar feature. In fact, if you're reading this on, then you may be able to see this feature on the top of your screen!

Below is a quick tutorial and explanation of a horizontal scroll progress bar with a demo on CodePen.

Live Demo and Final CodePen

Before we start, here is a link to the final CodePen, and again, a live demo can be seen on my personal website, if you are on desktop. Here's the final result of this:

Final Demo

Writing the HTML & CSS

To start off, let's create a post container div, which will include the HTML contents of the blog post that your viewers will be reading. Within that div, we'll also include a progress bar element for the scroll progress bar.

1<div class="post_container"></div> 2

At the beginning of the post container element, let's add the progress bar HTML, which will look like this:

1<div class="post-container"> 2 <div class="progress-bar-container"> 3 <div class="progress-bar-container__progress"></div> 4 </div> 5</div> 6

Note that in this post, I'll be using the BEM Methadology for CSS classnames. You can read more about the BEM Methadology and what it is here: How I Moved a Step Closer to Clean CSS and How You Can Too (with the BEM Methodology).

The general idea here is to have the progress bar container fixed at the top of the post container, with a full width. Within that container, the actual progress bar can be resized to the correct width with JavaScript.

Progress Bar and Container Visual

Here is the basic CSS for this:

1/* default CSS variables */ 2:root { 3 --progress-color: #2ecc71; 4 --progress-height: .5rem; 5} 6 7/* post container */ { 9 overflow: scroll; 10} 11 12/* progress bar container */ 13.progress-bar-container { 14 position: fixed; 15 top: 0; 16 left: 0; 17 width: 100%; 18 height: var(--progress-height); 19} 20 21/* progress bar */ 22.progress-bar-container > .progress { 23 height: var(--progress-height); 24 background-color: var(--progress-color); 25 width: 0%; 26 float: left; 27} 28

Note that in this case, the CSS assumes that the .post-container element is the scrollable area in this case (as defined with the overflow: scroll line), but you can change this to be a different element or the body element yourself if you'd like.

I'm also using CSS variables for the progress bar height and color, so that it is easier to change the properties of the progress bar if you'd like. You can read more about CSS variables and what they are here: CSS Variables Explained in 2 Minutes with an Interactive Demo.

Here's what this looks like when I set the width to 50% for example (with example content in the post container):

50% Scroll Progress Bar Width Example

Let's Write the JavaScript for this!

For the JavaScript, I'll start by defining variables for each of the relevant HTML elements:

1// variables for progress bar and post container elements 2const progressContainerEl = document.querySelector(".post-container"); 3const progressBarEl = document.querySelector(".progress-bar-container__progress"); 4

Creating a Function to Update the Progress Bar Width

Let's create a function which checks the current scroll position and calculates the percentage of the post read, and then set the progress bar width accordingly.

To make the scroll percentage calculation, let's divide the current scroll position (from the scrollTop property) by the full scroll height of the element (calculated with a function I got from Stack Overflow).

I then set the width style of the progress bar element to that calculated percentage.

Here's the code for that:

1// function to check scroll position and update scroll progress bar accordingly 2const updateScrollProgressBar = () => { 3 // get full scroll height 4 const scrollHeight = progressContainerEl.scrollHeight - heightInViewport(progressContainerEl); 5 console.log(scrollHeight); 6 // get current scroll position 7 const scrollPosition = progressContainerEl.scrollTop; 8 9 // get scroll percentage and set width of progress bar 10 const scrollPercentage = (scrollPosition / scrollHeight) * 100; 11 = scrollPercentage + "%"; 12} 13 14// function to get visible height in viewport 15// some code taken from user Roko C. Buljan on 16function heightInViewport(el) { 17 var elH = el.offsetHeight, 18 H = document.body.offsetHeight, 19 r = el.getBoundingClientRect(),, b=r.bottom; 20 return Math.max(0, t>0? Math.min(elH, H-t) : Math.min(b, H)); 21} 22

Calling the Function While Scrolling

To put all of this together and make everything work, we'll need to call the function everytime a user scrolls and when the page is loaded. For that, it's possible bind the function to the onscroll event of the post container, and the onload event of the window.

1// bind window onload and onscroll events to update scroll progress bar width 2progressContainerEl.addEventListener("scroll", updateScrollProgressBar) 3progressContainerEl.addEventListener("load", updateScrollProgressBar) 4

We're Done!

And with that, we're finished. Here is the final CodePen:

<p class="codepen" data-height="386" data-theme-id="default" data-default-tab="result" data-user="xtrp" data-slug-hash="mdyYRQz" style="height: 386px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Scroll Progress Bar Demo"> <span>See the Pen <a href=""> Scroll Progress Bar Demo</a> by Gabriel Romualdo (<a href="">@xtrp</a>) on <a href="">CodePen</a>.</span> </p> <script async src=""></script> <p></p>

I hope you liked this post, and found this to be useful.

Thanks for scrolling.

<!-- **This post is originally from [](, a blog about CSS, JavaScript, and just about anything programming.** -->

— Gabriel Romualdo, February 5, 2020