Animated website backgrounds with a working pause button
Published
Keep an animated background separate from the words. Put the text on a plain panel and add a button that pauses the motion. The download shows slowly moving colors with a working control. It also follows the visitor’s setting for less motion.
On this page
Open the original background demo. Save that HTML file to use the complete example: it contains the styles, the motion button and a short program, or script, that makes the button work.
Separate the decoration from the reading surface
The example has two layers. The background fills the window with gradients, areas where colors blend together. A solid cream panel holds the heading, text and button.
That separation gives you a useful design rule: change the moving colors while keeping the plain background behind the words.
Try the page at a narrow width. Keep space around the text, even when less of the background is visible.
Choose a quiet movement
The demo takes 24 seconds to move the background in one direction, then reverses it. The gradients keep their shapes and the text remains fixed. You can make the movement slower by increasing the time set for the animation.
Start by changing one thing: position, angle or color. One change is easier to judge than several effects at once. Watch the movement repeat before deciding whether it suits the subject.
An astronomy website might use slowly moving dark blues. A garden diary could use green and blue areas.
Give the visitor a working control
The example’s button starts or pauses the decorative motion. Its label changes when you press it. The aria-pressed value tells screen-reading software whether movement is on. The button sits inside the reading panel, where visitors can reach it with a keyboard or pointer.
This CSS styling code switches between moving and paused:
.sky { animation: drift 24s ease-in-out infinite alternate; }
html[data-motion="off"] .sky { animation-play-state: paused; }
The page starts with no movement. Its script reads the computer’s setting for less motion, then turns movement on or leaves it off. MDN documents prefers-reduced-motion as a way for a page to follow that computer setting.
MDN’s animation-play-state reference explains the pause setting used here. When movement restarts, it carries on from where it stopped. For an animated GIF image, the button must swap that file for a still picture.
Adapt the example to your own site
Open the file in a text editor and change the heading and sample paragraphs first. Next, alter the gradient colors. The dark base color fills the space between the lighter areas, so check that color as well as the lighter ones.
Find animation:drift 24s in the file and change 24s to 48s to halve the movement’s speed. In .sky, replace the two gradient colors, #447b8e and #5c537f, while leaving the cream main background unchanged. Save and reload to compare one full pass.
To bring the effect into an existing site, copy the background’s HTML code, its CSS and the button script. Keep the background separate from the main content. Check that the menu and other parts of your page still appear above it.
Use an image when the image earns its place
You can also use a tile, a small image that repeats across the background. Try an original star pattern, a texture of simple shapes or a small drawing. Check for visible lines where the image’s edges meet.
For a GIF tile, save a still PNG image at the same size. Use this code so the button chooses between the two files:
.sky { animation: none; background: url("tile-still.png") repeat; }
html[data-motion="on"] .sky { background-image: url("tile.gif"); }
Place both files beside the page. The button now chooses the animated or still file; the blinkie guide explains how to export the two versions.
Check the finished page
Read a full paragraph while the background moves, pause it, then read again. Use the keyboard to reach the control and check for an outline around the selected button. Resize the window and make sure the panel still fits inside it.
The 90s website starter gives you a small site to adapt, and the Neocities guide shows how to put it online.