Skip to content

Toast Notifications

Stacking Tailwind CSS toast notifications for success, error and info with auto-dismiss countdown, pause on hover and accessible live region.

Also known as: snackbar message, notification popup, toast messages, alert toast

Toast Notifications live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A stacking toast notification system built with Tailwind CSS and vanilla JavaScript, with dark success, error and info toasts that slide in from the corner, show a countdown bar and close themselves. Use it for save confirmations, failed uploads, new messages and other short, non-blocking feedback.

How it works

The root is .ttn-toaster. It holds the demo buttons, one <template data-ttn-template="…"> per toast type and the empty .ttn-region container, which is fixed to the bottom of the screen (full width on phones, bottom-right from sm up). Buttons with data-ttn-show clone the matching template, optionally replace the text of data-ttn-title and data-ttn-message with the button's own attributes, and append the toast to the region. When more than data-ttn-max toasts are open, the oldest ones are dismissed.

Auto-dismiss is driven by CSS instead of setTimeout: the .ttn-progress bar runs the ttn-countdown keyframes for --ttn-duration, and its animationend event closes the toast. Because .ttn-toast:hover and .ttn-toast:focus-within set animation-play-state: paused, hovering or tabbing into a toast pauses its timer exactly where it is. Closing adds .ttn-leaving, which plays ttn-out before the element is removed. The close button, Escape inside a toast and the data-ttn-clear button all dismiss toasts.

Other code can raise a toast without globals by dispatching a ttn:show CustomEvent on the root with detail: { type, title, message, duration }.

Customization

  • data-ttn-duration on .ttn-toaster: default lifetime in milliseconds (5000); the error template overrides it with its own data-ttn-duration="8000".
  • data-ttn-max on .ttn-toaster: how many toasts can stack before the oldest is removed.
  • data-ttn-welcome="info" on the root shows a toast on page load for the demo; delete the attribute in production.
  • data-ttn-title and data-ttn-message on a data-ttn-show button: per-trigger text, as on the Info button.
  • bottom-0 on .ttn-region: use top-0 for a top stack; replace sm:left-auto sm:items-end with sm:right-auto sm:items-start to move it to the left corner.
  • bg-slate-900 and sm:w-96 on .ttn-toast in each template: switch to bg-white text-slate-900 for light toasts or widen them.
  • Add a new type (for example warning) by copying a template, renaming data-ttn-template and swapping the emerald colors.

Use cases

  • Save confirmations: a success toast slides in and closes itself with a countdown bar
  • Failed uploads: error toasts report problems without blocking the page
  • Messaging apps: info toasts announce new messages, pausing when hovered
  • Dashboards: stacked toasts in the corner use a live region for screen readers

Accessibility

.ttn-region is a labelled role="region" with aria-live="polite", so success and info toasts (role="status") are read without interrupting, while the error template uses role="alert" to be announced immediately. Each close button has a visually hidden "Dismiss notification" label and a visible focus outline. Timers pause on keyboard focus, giving users time to read, and when a focused toast closes, focus moves to the next toast or back to the button that opened it. Under prefers-reduced-motion the slide animations are removed and the moving bar is hidden while the timer still runs.

Usage notes

Keep toast messages short and never put the only copy of important information in one. Each .ttn-toaster works independently.

Toast Notifications at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile
Category
Toasts
License
Free for personal and commercial use

Questions about this template

Can I use the Toast Notifications code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Toast Notifications – free Tailwind CSS toast template
Toast Notifications · free Tailwind CSS toast template

More Toasts

Bootstrap Toast Stack – free Bootstrap 5 toast template

Bootstrap Toast Stack

Bootstrap 5 toast notifications in a corner stack: success, info, warning and error buttons, selectable corner, auto-hide with a progress bar and a stack limit.

Toast with Progress Bar – free Pure CSS toast template

Toast with Progress Bar

Stacked toast notifications with a CSS countdown progress bar that pauses on hover or focus and closes the toast when it runs out.