Blog
Scrolling Text with a Close Button Using CSS and jQuery
To create scrolling text with a close button using jQuery, animate the message with CSS and bind a button that hides its announcement container. Include a way to pause the motion and show a static version for visitors who prefer reduced motion.
This tutorial rebuilds the older marquee-text example as a dismissible announcement bar. The demo supports pause, resume, close, show, editable sample text, and reduced-motion preview, with a complete downloadable source project.
Use CSS animation instead of the marquee element
The HTML <marquee> element is deprecated. MDN’s marquee reference recommends avoiding it. A regular HTML container with a CSS transform animation gives you clearer control over layout, motion, and static fallbacks.
| Feature | Implementation in this demo |
|---|---|
| Scrolling message | A CSS transform animation inside a clipped viewport. |
| Close button | A real button that hides the announcement. |
| Pause control | Switches to complete static text for reading. |
| Reduced motion | Disables animation and allows the text to wrap. |
| Restore announcement | A Show announcement button outside the hidden bar. |
Live demo: scrolling text with a close button
Try Pause motion, close and restore the bar, or edit the sample text. The reduced-motion checkbox previews a static mode without changing your system settings. All demo changes stay in this page.
Try a scrolling announcement
Pause the motion to read the full message as static text, close the bar, or show it again. Demo changes are kept only in this page.
Up to 180 characters. Empty text uses a readable fallback.
Your system’s reduced-motion preference is respected too. In reduced-motion mode the message stays static.
Static announcement. JavaScript and jQuery enable the demo controls.
The message remains readable without JavaScript. Motion and controls are optional enhancements.
1. Create the announcement and close button
Use a container with a descriptive label, a visual message area, and a real button. Give the close icon an accessible name so its purpose does not depend on recognizing the multiplication symbol.
<section class="announcement" aria-label="Site announcement">
<div class="announcement-viewport">
<span class="announcement-text">Your announcement text</span>
</div>
<button type="button" class="announcement-close"
aria-label="Close announcement">×</button>
</section>
This is a structural excerpt. The complete project also exposes a single static copy of the message to assistive technology, while the moving visual copy is hidden from accessibility APIs. It does not create a repeating alert announcement on every animation loop.
2. Add a CSS scrolling animation
.announcement-viewport {
overflow: hidden;
min-width: 0;
}
.announcement-text {
display: block;
width: max-content;
padding-inline-start: 100%;
white-space: nowrap;
animation: announcement-scroll 18s linear infinite;
}
@keyframes announcement-scroll {
from { transform: translateX(0); }
to { transform: translateX(-100%); }
}
The leading padding starts the message outside the viewport, and the transform moves it across. The duration sets a lap time, not a constant pixels-per-second speed: a longer message travels farther in the same duration. This single-message design has a gap between laps; it is not a seamless duplicated-content ticker.
3. Close and restore the bar with jQuery
Bind the close control to its own component, then show a restoration button outside the hidden container. Hiding preserves the existing element so it can be shown again.
const $bar = jQuery('#announcement');
const $show = jQuery('#show-announcement');
jQuery('#close-announcement').on('click', function () {
$bar.prop('hidden', true);
$show.prop('hidden', false).trigger('focus');
});
$show.on('click', function () {
$bar.prop('hidden', false);
$show.prop('hidden', true);
jQuery('#close-announcement').trigger('focus');
});
These IDs illustrate the event flow; the ZIP contains the full matching markup and controller. The jQuery .on() documentation explains binding the click handlers. The demo closes immediately without a fade, avoiding an extra motion effect.
4. Provide a readable pause mode
A close button alone does not let someone read a moving message at their own pace. The demo’s Pause motion button removes the animation and displays the full message as wrapping text. Resume motion starts a fresh pass from the beginning.
.announcement[data-paused="true"] .announcement-text {
animation: none;
transform: none;
width: auto;
padding-inline-start: 0;
white-space: normal;
overflow-wrap: anywhere;
}
W3C’s Pause, Stop, Hide explanation describes the need for user control over nonessential automatically moving content that continues alongside other content. The complete sample provides pause and hide controls, but you should still test your full page and audience needs.
5. Respect reduced-motion preferences
@media (prefers-reduced-motion: reduce) {
.announcement-text {
animation: none;
transform: none;
width: auto;
padding-inline-start: 0;
white-space: normal;
overflow-wrap: anywhere;
}
}
MDN’s prefers-reduced-motion reference explains this user preference. The project also listens for preference changes so the control label stays accurate. It does not let the demo’s Resume button override an active system reduced-motion preference.
Without JavaScript or jQuery, the default bar stays static and readable. Motion and controls are enhancements rather than prerequisites for displaying the announcement.
Keep announcement content clear and safe to render
Use concise text and insert edited values through textContent, not HTML concatenation. The demo editor accepts up to 180 characters and uses a fallback for blank input. It does not send or save the edited message.
For urgent instructions or long information, a static banner is usually easier to read than a continuously moving ticker. Keep essential information available elsewhere on the page, and do not use a moving announcement as the only place to explain an important action.
Download and run the complete project
Extract the ZIP and open index.html in a current browser. The local jQuery file and license are included, so no CDN, backend, or build step is needed.
scrolling-announcement-demo/
├── index.html
├── style.css
├── script.js
├── README.md
└── vendor/
├── jquery-3.7.1.min.js
└── LICENSE-jquery.txt
The sample pins jQuery 3.7.1 to match this site’s WordPress version. On WordPress, enqueue your script with the existing jquery dependency rather than adding a second library copy. The public demo editor is for learning; production announcement text should come from your own content system.
Common announcement-bar problems
| Problem | Fix |
|---|---|
| The close control is hard to use with a keyboard | Use a button, a clear accessible name, and a visible focus outline. |
| Closing hides every announcement | Scope selectors to the intended component. |
| Pause freezes text outside the viewport | Offer a static wrapping reading mode. |
| Reduced-motion users still see scrolling | Disable the animation with the media query and respect it in the controller. |
| The bar cannot be shown again | Keep a restoration control outside the hidden container. |
| The message reappears after refresh | This demo keeps dismissal in page memory only; persistent dismissal needs a separately designed storage policy. |
Frequently asked questions
Should I use the HTML marquee tag?
No. Use ordinary HTML and CSS animation with appropriate user controls and static fallbacks.
Can jQuery remove the announcement instead of hiding it?
Yes, but removing the element means a Show button cannot simply reveal it again. This example hides it to support restoration.
Does Pause motion freeze the current animation frame?
No. It shows the full message as static text. Resuming starts the scrolling pass again.
Does closing the bar persist after a refresh?
No. The demo uses current-page state only and does not write cookies or localStorage.
Can I change the scrolling speed?
Change the CSS animation duration. A longer duration makes each pass slower; test readability with the actual message length.
Does the demo publish the text I type?
No. Text changes affect only the preview in your current page and are not transmitted by the demo.
Try the complete announcement controls
Test pause and resume, close and show, blank or long text, reduced-motion mode, and reset. The ZIP contains the complete HTML, CSS, jQuery controller, and setup notes.