webdevpuneet.com

Announcement Bar — Free HTML CSS JS Rotating Banner Snippet

Announcement Bar · Navigation · Plain HTML, CSS & JS · Live preview

CategoryNavigation

Copy & paste

Source Code

<div class="ab-bar" id="abBar" role="region" aria-label="Announcements">
  <button class="ab-nav ab-prev" id="abPrev" aria-label="Previous">‹</button>
  <div class="ab-viewport"><ul class="ab-track" id="abTrack"></ul></div>
  <button class="ab-nav ab-next" id="abNext" aria-label="Next">›</button>
  <button class="ab-close" id="abClose" aria-label="Dismiss">✕</button>
</div>
<div class="ab-page"><h1>Page content</h1><p>The rotating bar sits pinned at the top.</p></div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Sliding message track
One message at a time via translateX.
HTML messages
Links and bold text from a data array.
Auto-rotation
Advances every few seconds.
Pause on hover
Stops so messages can be read or clicked.
Looping prev/next
Modulo wrapping past the ends.
Collapsing dismiss
Height animates to zero and removes.
Shifting gradient
A subtle flowing background.
Sticky and layered
Pinned above the page on scroll.

About this UI Snippet

Announcement Bar — Rotating Sticky Banner With Controls

Screenshot of the Announcement Bar snippet rendered live

The announcement bar is the slim banner pinned to the top of a site that cycles through short messages — a launch, a promo code, an event — auto-advancing on a slide, with manual controls and a dismiss button. This snippet builds it with plain HTML, CSS, and vanilla JavaScript, including the details that make it usable rather than annoying.

A sliding message track

The messages are list items in a flex track, each flex: 0 0 100% so exactly one fills the viewport. Advancing translates the track by -idx * 100% with a cubic-bezier transition, sliding the next message in from the right. This is the same horizontal-carousel mechanic used for sliders, here applied to a single-line bar. Messages can contain links and bold text (a promo code, a "read more"), since they are rendered as HTML from the data array.

Auto-rotation that respects the reader

A setInterval advances the bar every 5 seconds, but it pauses on pointerenter and resumes on pointerleave — so a visitor can stop on a message to read it or click its link without it sliding away. Every manual navigation also restarts the timer (restart()), so the next auto-advance is a full interval after your interaction rather than firing immediately. These two behaviors are what separate a respectful announcement bar from a frustrating one.

Manual controls and looping

Previous and next buttons let users step through messages, and the go(n) function wraps the index with modulo arithmetic so next from the last message loops to the first and previous from the first loops to the last. The controls and the auto-rotation share the same go(), so they stay in sync.

Dismissable, and it stays dismissed-feeling

A close button collapses the bar: it adds a .closing class that transitions height, padding, and opacity to zero, then removes the element on transitionend. Collapsing the height (rather than just hiding) means the page content slides up to reclaim the space smoothly. In production you would set a cookie or localStorage flag here so it does not reappear on the next visit.

A living gradient

The bar background is a three-color gradient sized at 200% and slowly shifted via the abShift keyframe, so the banner subtly flows between colors — eye-catching without being loud. It is pure CSS and costs nothing.

Sticky and layered

The bar is position: sticky; top: 0 with a high z-index, so it stays pinned above the page as you scroll and over other content. The demo page below shows it holding its place at the top.

Customizing it

Edit the MSGS array (with links and emphasis), change the rotation interval, recolor or retime the gradient, swap the slide for a fade, or persist dismissal. Pair it with a sticky promo bar variant or a floating pill nav below it.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA gradient announcement bar pins to the top.
  2. 2
    Watch it rotateMessages slide in and out every few seconds.
  3. 3
    Use prev/nextStep through messages manually; they loop.
  4. 4
    Hover to readRotation pauses so you can read or click a link.
  5. 5
    Dismiss itThe close button collapses the bar away.
  6. 6
    Edit the messagesChange the MSGS array with links and emphasis.

Real-world uses

Common Use Cases

Launch announcements
Promote a release above a floating pill nav, with messages sliding in one at a time by translateX.
Promo codes
Offer a rotating cousin of a sticky promo bar, where messages are HTML from a data array so links and bold text work.
Event reminders
Drive webinar signups with a short message, pausing on hover so people have time to read and click.
Status notices
Surface service updates from a status dashboard as status notices, with previous and next controls for manual navigation.
Shipping offers
Pair with a free shipping bar in a store, and study respectful auto-rotation with a dismiss button.
Related: Product Compare Bar
See the Product Compare Bar for a related navigation pattern worth pairing with this one.
Related: Voice Command Navigation Menu
See the Voice Command Navigation Menu for a related navigation pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

The messages are flex items each sized to 100% of the viewport, and advancing translates the track by negative index times 100% with a cubic-bezier transition, sliding the next message in. It is the same horizontal-carousel mechanic as a slider, applied to a single-line bar, and go() wraps the index with modulo so it loops past the ends.

The auto-rotation pauses on pointerenter and resumes on pointerleave, so hovering stops the bar to let you read a message or click its link. Every manual navigation also restarts the timer, so the next auto-advance is a full interval after your interaction rather than firing right away.

The close button adds a closing class that transitions the height, padding, and opacity to zero, then removes the element on transitionend. Collapsing the height lets the page content slide up to reclaim the space smoothly. In production you would also set a cookie or localStorage flag so it stays dismissed on the next visit.

Yes. Messages are rendered as HTML from the MSGS array, so they can include anchors and bold text — a promo code, a read-more link, a save-a-seat call to action. Because hovering pauses rotation, users have time to actually click those links before the bar advances.

Render the messages from data and keep the active index in state, advancing it on an interval set up in a mount effect with cleanup, and pausing it via hover handlers. Drive the track transform from the index. For dismissal, toggle a state flag and persist it to storage. In Tailwind, build the track with flex and translate utilities and animate the gradient with a keyframe in the config.

Yes. While the Announcement Bar snippet gives you one pattern to copy, the free Responsive Navbar Builder at webdevpuneet.com/navbar-builder/ designs a whole navigation bar: logo, links and dropdown menus, a mobile hamburger menu (dropdown panel, slide-in drawer or full screen) and a scroll effect (sticky, shrink, hide on scroll or transparent). It has six presets, a live preview that runs the real code, and exports HTML + CSS + vanilla JS, React, Tailwind, Vue 3 or Angular — or you can click Fork & Edit to keep editing it in My Code.