December 4, 2026
To make an animated background pattern in SVG, define one small tile inside a <pattern> element, fill a
rectangle with it, and slide the pattern by exactly one tile on a loop. Since the tile repeats, the end of the loop looks the
same as the start, and the motion never seems to restart. The file stays tiny and sharp at any size.
What is the code for moving stripes?
<svg class="bg" width="100%" height="100%" aria-hidden="true">
<defs>
<pattern id="stripes" width="40" height="40"
patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
<rect width="20" height="40" fill="#c6f432" />
<animateTransform attributeName="patternTransform" type="translate"
from="0 0" to="40 0" dur="1.5s"
repeatCount="indefinite" additive="sum" />
</pattern>
</defs>
<rect width="100%" height="100%" fill="#0b3d2e" />
<rect width="100%" height="100%" fill="url(#stripes)" />
</svg>
.section { position: relative; isolation: isolate; }
.section .bg { position: absolute; inset: 0; z-index: -1; }
if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
document.querySelectorAll("svg.bg").forEach((svg) => svg.pauseAnimations());
}
How does it work?
- The tile.
width="40" height="40"sets the repeating unit. The rectangle fills half of it, which makes stripes 20 pixels wide with 20-pixel gaps. - The angle.
patternTransform="rotate(45)"turns the whole pattern, giving diagonal stripes. - The loop. The animation translates the pattern from 0 to 40, exactly one tile.
additive="sum"adds the movement to the rotation instead of replacing it, so the stripes slide along their own angle.
Change dur for speed and swap from and to to reverse the direction.
How do I make dots, zigzags or waves?
| Pattern | What goes in the tile |
|---|---|
| Dots | One <circle> in the centre of a square tile. |
| Checks | Two squares in opposite corners. |
| Zigzag | A <polyline> that goes up and down once across the tile. |
| Waves | A <path> with one full curve that starts and ends at the same height. |
| Triangles | Two <polygon> shapes that split the tile on the diagonal. |
The rule for all of them: whatever leaves one edge of the tile must enter at the same point on the opposite edge.
Why SVG and not a video or GIF?
- Size. The stripes above are under 600 bytes. A looping background video is often several megabytes.
- Sharpness. Vectors are crisp on every screen and at every zoom level.
- Control. Colours, speed and angle are attributes you can change from CSS variables or a script.
Which finished patterns can I copy?
| Element | What it does |
|---|---|
| Barber pole pattern | Diagonal stripes in an SVG pattern that slide along their angle. |
| Dot wave pattern | A grid of small dots that sway in a slow travelling wave while bands of colour drift across. |
| Layered wave generator | Stacked SVG waves rolling sideways along the bottom of a panel. |
| Triangle tile drift | Two-tone triangle tiles that slide slowly on the diagonal while their colours drift. |
| Zigzag stripe flow | Bold zigzag stripes that drift straight down in a seamless loop. |
| Zigzag section divider | Two bands joined by a wide zigzag edge drawn with one stretched SVG path. |
All are free in the Enrichr Library, with live controls for colour, speed and size, and the code to copy.
Where to go next
- Animated gradient backgrounds: CSS, canvas or WebGL?
- How to extract SVGs and images from a website.
- How to make an infinite marquee in pure CSS.
FAQ
How do I animate an SVG pattern?
Animate the patternTransform attribute with a translate equal to one tile, either with an SVG <animateTransform> element or from JavaScript. Because the pattern repeats, a shift of one tile looks identical to no shift.
Can I do the same with a CSS background?
Yes for simple shapes: a repeating-linear-gradient moved by background-position. It repaints on every frame, though. An SVG pattern, or a pattern layer moved with transform, is lighter.
Are animated SVG backgrounds bad for performance?
A simple pattern is light. Costs rise with the number of shapes per tile and the size of the area. Pause the animation when the section is off screen.
How do I pause an SVG animation for reduced motion?
Call svg.pauseAnimations() when prefers-reduced-motion: reduce matches, or do not add the animation element in that case.