How to Use an Animated Background in Your Website or App
Copy a background from the library into a plain HTML page, a React or Vue app, a site builder or a mobile app. Each step has code you can paste.
On this page
- Pick a background and get the code
- Add it as a full-screen layer behind your content
- Quick option: embed it with an iframe
- CSS-only backgrounds (gradients)
- Canvas and WebGL backgrounds
- React and Next.js
- Vue and Nuxt
- WordPress, Webflow, Wix, Shopify and Framer
- Mobile and desktop apps
- Keep text readable on top
- Performance and accessibility checklist
- Credit the author
- FAQ
1Pick a background and get the code
Browse the library of 657 backgrounds or open a collection. Click a card to open the live preview, then use:
- Get Code — opens the original pen on CodePen with its HTML, CSS and JavaScript.
- Embed — gives you a ready-to-paste iframe snippet.
- Fullscreen — shows how the background looks at full viewport size.
Before you ship, open the pen and check its license and any external libraries it loads (Three.js, GSAP, p5.js…). Most pens are MIT-licensed, but some authors ask for credit.
2Add it as a full-screen layer behind your content
The most reliable setup is a fixed layer that fills the viewport and sits behind everything else. Paste the pen's HTML inside the layer, its CSS into your stylesheet and its JavaScript before </body>.
<body>
<div class="animated-bg" aria-hidden="true">
<!-- paste the pen's HTML here (canvas, svg or divs) -->
</div>
<main class="content">
<h1>Your page content</h1>
</main>
<script src="background.js"></script>
</body>
.animated-bg {
position: fixed;
inset: 0;
z-index: -1; /* behind the page */
overflow: hidden;
pointer-events: none; /* clicks go to your content */
}
.animated-bg canvas,
.animated-bg svg {
width: 100%;
height: 100%;
display: block;
}
.content { position: relative; }
Pens often style body or html directly (for example body { background: #000; overflow: hidden; }). Move those rules onto .animated-bg, otherwise your page will stop scrolling.
Want the background only inside a hero section instead of the whole page? Use position: absolute and give the parent section position: relative; overflow: hidden;.
3Quick option: embed it with an iframe
If you just want to try a background, the Embed button gives you an iframe that loads the pen from CodePen:
<iframe
src="https://codepen.io/USER/embed/PEN_ID?default-tab=result&theme-id=dark"
title="Animated background"
loading="lazy"
style="position:fixed;inset:0;width:100%;height:100%;border:0;z-index:-1"
aria-hidden="true" tabindex="-1"></iframe>
It is fast to set up, but it depends on CodePen being online, adds their scripts to your page and can show CodePen branding. For production sites, copy the code into your own files instead.
4CSS-only backgrounds (gradients)
Pure CSS backgrounds are the lightest option — no JavaScript at all. You can build one with the gradient creator and copy the result:
.hero {
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
background-size: 400% 400%;
animation: gradient-move 15s ease infinite;
}
@keyframes gradient-move {
0%, 100% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
}5Canvas and WebGL backgrounds
Particle, fluid and 3D backgrounds draw on a <canvas>. Three things make them behave well on a real site:
- Resize with the window and respect
devicePixelRatio(cap it at 2 to save battery). - Pause when the tab is hidden —
requestAnimationFramealready does, but stop timers and audio too. - Clean up when the page or component is removed, so the loop does not keep running.
const canvas = document.querySelector('.animated-bg canvas');
const ctx = canvas.getContext('2d');
let raf;
function resize() {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = innerWidth * dpr;
canvas.height = innerHeight * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
function frame(t) {
// ...draw one frame of the animation...
raf = requestAnimationFrame(frame);
}
addEventListener('resize', resize);
resize();
raf = requestAnimationFrame(frame);6React and Next.js
Wrap the background in a component, start the animation in useEffect and stop it in the cleanup function. In the Next.js App Router, mark the file as a client component.
'use client';
import { useEffect, useRef } from 'react';
export default function AnimatedBackground() {
const ref = useRef(null);
useEffect(() => {
const canvas = ref.current;
const ctx = canvas.getContext('2d');
let raf;
const resize = () => { canvas.width = innerWidth; canvas.height = innerHeight; };
const frame = () => {
// paste the pen's drawing code here
raf = requestAnimationFrame(frame);
};
resize();
addEventListener('resize', resize);
raf = requestAnimationFrame(frame);
return () => { cancelAnimationFrame(raf); removeEventListener('resize', resize); };
}, []);
return <canvas ref={ref} className="animated-bg" aria-hidden="true" />;
}
Put the CSS from the pen into a CSS module or your global stylesheet. If the pen uses a library such as Three.js, install it from npm (npm i three) instead of loading it from a CDN.
7Vue and Nuxt
<template>
<canvas ref="bg" class="animated-bg" aria-hidden="true" />
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
const bg = ref(null);
let raf;
onMounted(() => {
const ctx = bg.value.getContext('2d');
const frame = () => { /* pen drawing code */ raf = requestAnimationFrame(frame); };
raf = requestAnimationFrame(frame);
});
onBeforeUnmount(() => cancelAnimationFrame(raf));
</script>
In Nuxt, wrap the component in <ClientOnly> because canvas code needs the browser.
8WordPress, Webflow, Wix, Shopify and Framer
- WordPress — add a Custom HTML block for the markup, put the CSS in Appearance → Customize → Additional CSS, and load the JavaScript with a code-snippets plugin or your theme's footer.
- Webflow — add an Embed element for the HTML and paste the CSS and script into Page settings → Custom code (before
</body>). - Wix — use Embed Code → Embed HTML and paste the whole pen as one HTML document (HTML, a
<style>tag and a<script>tag), then stretch the element to full width. - Shopify — create a Custom Liquid section, or add the code to
theme.liquidfor every page. - Framer — add an Embed component with the HTML, or wrap the code in a Code Component.
9Mobile and desktop apps
- React Native / Expo — render the pen as an HTML string in
react-native-webview, absolutely positioned behind your views, withpointerEvents="none". - Flutter — use
webview_flutterfor the live version, or rebuild simple gradients with anAnimatedContainer. - Electron, Tauri and PWAs — they are web pages, so the full-screen layer from step 2 works as is.
- Native iOS / Android — record the background as a short looping MP4 or Lottie file and play it muted on loop. This uses far less battery than a WebView.
10Keep text readable on top
Moving, high-contrast backgrounds make text hard to read. Add a dark or light scrim between the background and your content, and check contrast against the brightest frame.
.animated-bg::after {
content: "";
position: absolute;
inset: 0;
background: rgba(10, 8, 6, .55); /* darken the animation */
}11Performance and accessibility checklist
- Respect
prefers-reduced-motion: show a still frame or slow the animation down. - Lower the particle count or resolution on small screens.
- Stop the animation when it is off screen with an
IntersectionObserver. - Mark the layer
aria-hidden="true"so screen readers skip it. - Test with Lighthouse — an animated background should not push your Largest Contentful Paint past 2.5 s.
@media (prefers-reduced-motion: reduce) {
.animated-bg * { animation: none !important; }
}
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!reduce) start(); // only animate when the user allows motion
new IntersectionObserver(([e]) => e.isIntersecting ? start() : stop())
.observe(document.querySelector('.animated-bg'));12Credit the author
Every background on AnimatedBackgrounds.me was made by a CodePen author. Keep the license comment from the pen in your code, and add a credit link if the license asks for one, for example:
<!-- Background: "Pen name" by Author — https://codepen.io/USER/pen/PEN_ID (MIT) -->FAQ
Are the animated backgrounds free to use?
They are free to browse and preview. Each background belongs to its CodePen author, and most pens are MIT-licensed, which allows commercial use with the license notice. Always check the pen before using it.
Will an animated background slow down my website?
CSS-only backgrounds are very light. Canvas and WebGL backgrounds use the GPU, so cap the pixel ratio, pause them when hidden or off screen, and test with Lighthouse.
Can I use these backgrounds in WordPress without coding?
Yes. Paste the HTML in a Custom HTML block, the CSS in Additional CSS and the JavaScript in a footer-code plugin. CSS-only gradients from the creator only need the CSS.
How do I put text on top of an animated background?
Place the background in a fixed or absolute layer with z-index: -1 and pointer-events: none, then add a semi-transparent scrim so the text keeps enough contrast.
Can I use an animated background in a mobile app?
Use a WebView in React Native or Flutter for the live version, or export a short looping video or Lottie file for native apps.