Source Code

<div class="container py-5 d-flex justify-content-center">
  <div class="card bssess-card">
    <div class="card-body p-4 text-center">
      <h6 class="fw-bold mb-1">Dashboard</h6>
      <p class="small text-muted mb-2">Demo session (sped up): expires after 18 seconds of inactivity.</p>
      <p class="fw-semibold mb-0" id="bssessTimer">18s remaining</p>
    </div>
  </div>
</div>

<div class="modal fade" id="bssessModal" tabindex="-1" data-bs-backdrop="static" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title fw-bold">Your session is about to expire</h5>
      </div>
      <div class="modal-body">
        <p class="mb-0">You've been inactive for a while. You'll be signed out in <strong id="bssessCountdown">8</strong> seconds.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-secondary" id="bssessLogout">Log out now</button>
        <button type="button" class="btn btn-dark fw-bold" id="bssessStay">Stay signed in</button>
      </div>
    </div>
  </div>
</div>

<div class="bssess-expired d-none" id="bssessExpired">
  <div class="text-center text-white">
    <p class="fw-bold mb-2">Session expired</p>
    <button type="button" class="btn btn-light btn-sm fw-bold" id="bssessRestart">Log in again</button>
  </div>
</div>

Bootstrap Session Expiry Warning — Free HTML CSS JS Snippet

Bootstrap Session Expiry Warning · Modals · Plain HTML, CSS & JS · Live preview

What's included

Features

One single interval drives the background timer, the warning modal, and the final expired state
The warning modal uses a real static backdrop, preventing an outside click from dismissing an unanswered warning
"Log out now" intentionally skips straight to the expired state instead of running its own countdown
The expired state is a full-page overlay, not a dismissible modal, correctly blocking further interaction
Every path (stay, log out, or timeout) correctly clears the interval so no stray timer keeps running

About this UI Snippet

Bootstrap Session Expiry Warning — HTML, CSS & JavaScript

Screenshot of the Bootstrap Session Expiry Warning snippet rendered live

One setInterval ticking once a second drives every state this snippet shows — the quiet background countdown on the page itself, the warning modal that appears once remaining crosses WARN_AT, and the final expired overlay once it hits zero — rather than three separate timers that would need to stay manually synchronized with each other.

The modal is deliberately configured with data-bs-backdrop="static", one of Bootstrap's own real modal options, so clicking outside it can't dismiss it the way a normal informational modal would — an expiry warning that can be casually clicked away without addressing it defeats its own purpose. "Stay signed in" and "Log out now" both stop the current countdown, but only "Stay signed in" calls start() again to reset remaining back to TOTAL; "Log out now" intentionally goes straight to the expired overlay instead, since choosing to log out shouldn't need its own separate countdown to actually happen.

The expired state is modeled as a full-page overlay rather than another modal — a session that has actually expired should block interaction with the underlying page entirely until the user restarts (in a real app, redirects to a login page), which a dismissible Bootstrap modal isn't designed to guarantee on its own.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet to an AI coding assistant like Claude and ask it to reset the countdown automatically on real user activity (mousemove, keydown, click) instead of only via the "Stay signed in" button, or to make an actual authenticated API call on "Stay signed in" to refresh a real backend session token rather than just resetting a client-side timer.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a Bootstrap 5.3 session expiry warning, using the real Bootstrap CDN framework (bootstrap.min.css and bootstrap.bundle.min.js), not custom CSS made to resemble it.

Requirements:
- A single interval-driven countdown (sped up for demo purposes, e.g. 18 seconds total) with a visible remaining-time indicator on the page.
- Once the countdown crosses a warning threshold (e.g. 8 seconds remaining), automatically show a Bootstrap modal with its own live countdown and two actions: "Stay signed in" and "Log out now". The modal must use a static backdrop so it cannot be dismissed by clicking outside it.
- "Stay signed in" must reset the full countdown from the beginning and close the modal. "Log out now" must immediately end the session instead of running any further countdown.
- If the modal's countdown reaches zero with no action taken, replace it with a full-page overlay indicating the session has expired, blocking interaction with the underlying page until a "Log in again" action resets the whole demo.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Step by step

How to Use

  1. 1
    Load the snippetA countdown begins immediately, sped up for this demo to 18 seconds total.
  2. 2
    Wait until 8 seconds remainA warning modal appears automatically, itself counting down from 8, with backdrop clicks disabled.
  3. 3
    Click "Stay signed in"The modal closes and the full 18-second countdown restarts from the beginning.
  4. 4
    Let the modal's countdown reach zero without clicking anythingThe modal is replaced by a full-page "Session expired" overlay blocking the page.
  5. 5
    Click "Log in again"The whole demo resets and the countdown starts over from 18 seconds.

Real-world uses

Common Use Cases

Banking, healthcare, and other compliance-sensitive dashboards
Session timeout warnings are frequently a genuine security or compliance requirement, not just a UX nicety.
Any app with a real backend session or auth token expiry
Pairs with bootstrap-reauthentication-modal for the related but distinct pattern of confirming identity before a specific sensitive action.
Learning to coordinate multiple UI states from one timer
A clean example of driving several dependent visual states from a single source of truth instead of separate competing timers.

Got questions?

Frequently Asked Questions

This demo runs on a fixed countdown so the behavior is fully visible without waiting for a real idle period; a production version should reset the timer on genuine activity signals (mousemove, keydown, or an actual API heartbeat) rather than running unconditionally.

It's configured with Bootstrap's data-bs-backdrop="static" option specifically so an accidental outside click can't make an unanswered expiry warning disappear without the user actually choosing to stay or log out.

This snippet only models the timing and lockout behavior — a real implementation should pair this with something like bootstrap-unsaved-changes-alert's dirty-tracking to warn about (or attempt to save) unsaved work before a session lockout occurs.

Yes. Keep the remaining seconds in a ref-backed interval (to avoid restarting it on every re-render) mirrored into component state for display, and drive the modal and overlay's visibility from that same state.