Skip to content

Top Announcement Bar

Dismissible gradient announcement bar fixed to the top of the page, with a tag, link, close button, Escape support and remembered dismissal.

Also known as: announcement banner, top notification bar, site wide promo banner, dismissible alert bar

Top Announcement Bar live demo

Open in new tab

Pure CSS code (HTML, CSS, JS)

A dismissible announcement bar built with CSS and a small vanilla JavaScript file. The gradient strip is fixed to the top of the window and shows a "New" tag, a short message, a link and a close button. Use it for product launches, sale banners, maintenance notices or general site updates.

How it works

The wrapper .ab-shell contains the bar and your page content. .ab-bar is position: fixed with top: 0, left: 0 and right: 0, so it stays visible while the page scrolls. Inside, .ab-inner is a flex row with the .ab-tag pill, the .ab-text paragraph with an inline .ab-link, and the .ab-close button. From 640px the close button is absolutely positioned and equal side padding keeps the message centered; on phones the text wraps and stays left-aligned.

Because a fixed element is taken out of the flow, .ab-shell reserves space with padding-top: var(--ab-offset, var(--ab-height)). The script measures the real bar height on load and on resize and writes it to --ab-offset, so a two-line bar on a small screen never covers the content.

A click on the button with data-ab-close adds .is-dismissed to the shell. The bar slides up with translateY(-100%), becomes visibility: hidden, and the padding animates back to zero. The choice is saved under ab-dismissed: plus the value of data-ab-id; storage access is wrapped in try/catch, so the bar still works where localStorage is blocked. The .ab-demo block with its "Show the bar again" button (data-ab-reset) is only demo content.

Customization

  • --ab-bg on .ab-shell: the bar background, any color or gradient, e.g. #0f172a for a dark strip.
  • --ab-tag-bg and --ab-tag-text: colors of the "New" pill; delete the .ab-tag span for a plain message.
  • --ab-height: the minimum height of the bar and the fallback space reserved before the script runs.
  • --ab-speed: duration of the slide-up and padding transitions.
  • --ab-z: the stacking order; raise it if a sticky site header overlaps the bar.
  • data-ab-id on .ab-shell: change it for each new campaign (for example summer-sale) so visitors who closed an old message see the new one.
  • Remove writeDismissed(id, true) in script.js if the bar should come back on every page load.

Use cases

  • Product launches: a gradient bar with a New tag and link announces a feature at the top of every page
  • Sales and promotions: a banner promotes a discount, and a stored dismissal keeps it hidden after closing
  • Maintenance notices: a short fixed message warns visitors about planned downtime
  • General site updates: campaign messages can be swapped, with a new id so past closers see them again

Accessibility

The bar is a role="region" landmark labelled "Announcement", so screen reader users can find or skip it. The close button is a real button with aria-label="Dismiss announcement", and pressing Escape while focus is inside the bar also closes it. After closing, focus moves to the element marked data-ab-return instead of being lost. Links and buttons get a yellow :focus-visible outline from --ab-focus, which stands out on the gradient. White text on the darker gradient stops keeps good contrast. With prefers-reduced-motion: reduce all transitions are disabled.

Usage notes

Wrap your whole page (or its top part) in .ab-shell, and use one bar per page since it is fixed to the window. Inside a preview iframe "the window" is the iframe itself.

Top Announcement Bar at a glance

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

Questions about this template

Can I use the Top Announcement Bar 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?

There is nothing to install. 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

No dependencies. Pure HTML, CSS and JavaScript.

Top Announcement Bar – Pure CSS alerts component screenshot
Top Announcement Bar · Pure CSS Alerts

More Alerts

Dismissible Alerts with Icons – Bootstrap 5 alerts preview

Dismissible Alerts with Icons

Bootstrap 5 dismissible alerts with icons, headings, per-alert close buttons, dismiss all, restore and screen reader announcements.

Alerts Collection – Tailwind CSS alerts preview

Alerts Collection

Tailwind CSS alerts collection with success, warning, error and info variants, icons, action links and accessible dismiss buttons.