This guide shows you how to build CSS animations that loop forever without jank or memory bloat, and how to record them as video or GIF for sharing. You’ll learn the core techniques—using infinite timing, controlling the loop with animation-iteration-count, and capturing the result with browser tools or screen recorders.
What You’ll Need
- A text editor (VS Code, Sublime, or any plain editor)
- A modern web browser (Chrome, Firefox, Edge, or Safari)
- Basic HTML and CSS knowledge
- Optional: a screen recorder (OBS, QuickTime, or built-in Windows Game Bar) for capturing
Step-by-Step
- Create the HTML structure. Start with a simple file:
index.htmlwith a<div class="box"></div>inside a container. This will be the element you animate. - Write the base CSS. In a
<style>block or separate CSS file, style the box with a width, height, background color, and any other properties you want to animate. For example:width: 100px; height: 100px; background: #3498db; - Define the keyframes. Use
@keyframesto specify the animation’s start and end states. For a simple pulse:@keyframes pulse { 0% { transform: scale(1); } 50% { transform: scale(1.2); } 100% { transform: scale(1); } } - Apply the animation with infinite iteration. On the box, add:
animation: pulse 2s infinite;. Theinfinitekeyword makes it loop forever. You can also useanimation-iteration-count: infinite;for clarity. - Add multiple animations (optional). To create complex loops, separate them with commas:
animation: pulse 2s infinite, spin 4s linear infinite;. Each animation runs independently. - Control the loop with percentages. For a seamless loop, ensure the 0% and 100% keyframes are identical. This prevents a visible jump. If you want a pause at the end, use
animation-timing-function: steps()or adjust the keyframe percentages to hold a state. - Optimize performance. Animate only
transformandopacityproperties, as they are GPU-accelerated. Avoid animatingwidth,height, ortop/leftbecause they cause layout thrashing and can make the loop choppy. - Test in the browser. Open the HTML file. The animation should loop smoothly. Use the browser’s DevTools (F12) to inspect the element and verify the animation is running (you’ll see an “Animations” tab in Chrome).
- Capture the animation as video. Use a screen recorder. In OBS, set the capture area to your browser window, record for a few seconds, then stop. Export as MP4 or WebM.
- Capture as GIF (alternative). Use a tool like GIPHY Capture (Mac) or a browser extension that records to GIF. Alternatively, record a video and convert it to GIF using an online converter or ffmpeg.
- Trim and loop the capture. In a video editor, trim the recording to exactly one loop (or a few loops). When exporting, set the video to loop seamlessly if the platform supports it (e.g., for GIF, the loop is automatic).
Common Mistakes
- Animating non-GPU properties. Animating
marginorwidthcauses reflows, making the animation stutter. - Missing identical start/end states. If 0% and 100% differ, the loop will jump.
- Forgetting vendor prefixes. While modern browsers don’t need them, older ones might. Add
-webkit-prefixes if you need legacy support. - Using too many simultaneous animations. This can overload the GPU, especially on mobile devices.
- Capturing with a visible cursor or browser chrome. This ruins the clean loop. Use a clean window or hide the cursor.
Tips
- Use
animation-direction: alternatefor a back-and-forth loop that doesn’t need identical start/end states. - Set
animation-fill-mode: forwardsif you want the final state to persist when the animation is paused or removed. - For a truly seamless loop, use
animation-timing-function: linearfor rotations or continuous movements. For easing that loops smoothly, usecubic-bezier()with matching start/end tangents. - When capturing, set the browser zoom to 100% and use a fixed viewport size to avoid resolution changes.
- Use the
prefers-reduced-motionmedia query to disable animations for users who prefer less motion—it’s good practice.
Troubleshooting
Animation doesn’t run. Check that the element is visible (not display: none) and that the animation property is spelled correctly. Verify the keyframes name matches exactly.
Animation jumps at loop point. Ensure the 0% and 100% keyframes are identical. If you’re using alternate, the jump may be due to timing function—try linear.
Animation is choppy. Switch to animating transform and opacity. Also, check if your browser’s hardware acceleration is enabled.
Capture is blank or black. Some screen recorders don’t capture hardware-accelerated content. Disable hardware acceleration in the browser or use a recorder that supports GPU capture.
GIF is too large. Reduce the frame rate (e.g., 15 fps instead of 30) or reduce the dimensions. Use a tool that optimizes GIFs.
Frequently Asked Questions
How do I make an animation loop exactly once then stop?
Use animation-iteration-count: 1 (or omit the value, as 1 is the default). The animation will play once and stop.
Can I pause an infinite animation on hover?
Yes. Add animation-play-state: paused; on :hover. For example: .box:hover { animation-play-state: paused; }
How do I create a continuous rotation?
Use @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } and apply animation: spin 2s linear infinite;. The linear timing makes it smooth.
What’s the difference between infinite and alternate?
infinite makes the animation repeat forever. alternate makes it reverse direction each cycle. They can be combined: animation: pulse 1s ease-in-out infinite alternate; gives a smooth back-and-forth.
Can I capture an animation without a screen recorder?
Yes, you can use browser DevTools. In Chrome, open DevTools, go to the “More tools” menu, and select “Capture node screenshot” or use the “Rendering” tab to capture a screenshot, but for video, you’ll need a recorder or a library like html2canvas (but that captures a single frame). For video, a screen recorder is the standard.
How do I make a GIF from a CSS animation?
Record the animation with a screen recorder, then convert the video to GIF using an online converter or a tool like ffmpeg. Ensure the video is short (a few loops) to keep the file size reasonable.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix msmpeg2vdec.dll Errors in Windows
- How to Update Sonos App and Speakers
- Unlock iPad Without Apple ID: Is It Possible?
- iOS 18 WhatsApp Notifications Not Working? Fix
- Fix Windows Cannot Locate Suitable Printer Driver
- How to Group Text and Pictures in Word