WordPress JavaScript Free

WordPress: Add a Reading-Progress Bar to Posts Without a Plugin

Add a thin reading-progress bar to the top of WordPress posts with a few lines of code — no plugin needed.

24 lines · Updated Nov 26, 2026

Preview of WordPress: Add a Reading-Progress Bar to Posts Without a Plugin
wordpress-reading-progress-bar-no-plugin.js
<!-- Add to your theme, e.g. in single.php before the content -->
<div id="reading-progress"></div>

<style>
#reading-progress {
    position: fixed;
    top: 0; left: 0;
    height: 4px;
    width: 0;
    background: #8b5cf6;
    z-index: 9999;
}
</style>

<script>
window.addEventListener('scroll', function () {
    var article = document.querySelector('.entry-content');
    if (!article) return;
    var top = article.getBoundingClientRect().top;
    var height = article.offsetHeight - window.innerHeight;
    var progress = Math.min(100, Math.max(0, -top / height * 100));
    document.getElementById('reading-progress').style.width = progress + '%';
});
</script>

About this snippet

A thin bar at the top of the page that fills up as the reader scrolls is a small touch that makes long articles feel shorter and more manageable. Most plugins that add it also load extra CSS and JavaScript on every page. This snippet adds a lightweight reading-progress bar to single posts with a few lines of vanilla JavaScript and CSS — no plugin and no jQuery.

How it works

  1. A fixed-position bar is added at the top of the page with a width of 0%.
  2. On scroll, the script calculates how far the reader has scrolled compared with the total scrollable height.
  3. It updates the bar's width to that percentage. Using requestAnimationFrame or a passive scroll listener keeps it smooth and cheap.

How to use it

  • Load it only on single posts, for example by enqueueing the script when is_singular('post') is true.
  • Change the bar colour to your brand colour and keep the height around 3–4 px.
  • If your theme has a sticky header, set the bar's top value so it sits just below the header.

Customize it

  • Measure progress through the article content only (not comments and footer) by using the article element's position and height.
  • Combine it with estimated reading time for a complete reading experience.

Common problems

  • Bar hidden behind the header: increase its z-index.
  • Bar reaches 100% too early: you are measuring the whole page; measure the article container instead.

FAQ

Does it affect performance? Barely — it is a few lines of code that only run while scrolling and do not trigger layout-heavy work.

Related: Dark mode toggle · 5 code snippets every developer needs

How to install

Paste the HTML, CSS and JavaScript into your theme's single-post template (for example single.php), or into a code-snippets plugin as a frontend snippet. Change .entry-content to your theme's content class if needed, and the colour to match your site.

Share this snippet

See all
Chat with us