About this snippet

Show your readers how long an article takes to read. This script counts the words in your text and calculates the time, which helps visitors decide if they have time to read your post now.

Size
1.6 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
247
<div class="article-meta">
  <span class="read-time-badge">
    <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>
    <span id="timeDisplay">0 min read</span>
  </span>
</div>
<article id="articleContent" class="content-area">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <!-- Add more text to see the time change -->
</article>
.article-meta {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  margin-bottom: 20px;
}
.read-time-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #f3f4f6;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  color: #4b5563;
  font-weight: 500;
}
.content-area {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.6;
  color: #1f2937;
  max-width: 600px;
}
const text = document.getElementById("articleContent").innerText;
const display = document.getElementById("timeDisplay");

const wpm = 200;
const words = text.trim().split(/\s+/).length;
const time = Math.ceil(words / wpm);

display.innerText = `${time} min read`;
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save