To animate an image in HTML, put it in an <img> element and use CSS keyframes for a repeating effect such as floating, fading, or rotating. Use the Web Animations API when JavaScript must start, pause, reverse, or otherwise control playback. For a pre-rendered sequence, use an animated image file; for motion applied to individual vector shapes, use inline SVG. HTML displays the image, but the animation comes from CSS, JavaScript, the image format, or SVG.
Choose the right way to animate an image
| Method | Use it when | Main trade-off |
|---|---|---|
| CSS keyframes | You want a repeatable visual effect such as moving, fading, scaling, or rotating an image. | Concise and declarative; timing and behavior are defined in CSS. |
| Web Animations API | JavaScript needs to start or manage the animation in response to an action or application state. | Provides playback control, but requires JavaScript. |
| Animated image file | The sequence is already rendered and should play as one image resource. | Simple to embed; the frames and timing are part of the file, not individually controllable DOM states. Format support varies among browsers. |
| Inline SVG | You need to animate vector artwork or individual shapes and attributes. | Lets you target SVG elements; an SVG embedded as an image is not interactive. |
The HTML <img> element embeds an image resource; the HTML standard does not prescribe one fixed list of supported image formats for every user agent. See MDN’s image embed element reference. For animated images, MDN lists APNG, AVIF, GIF, and WebP among common formats. APNG can suit lossless animation, GIF simple animations, and WebP still or animated images; choose based on compatibility needs, file size, and image quality rather than assuming one format always wins.
Animate an image with CSS keyframes
For a fixed decorative effect, CSS is usually the simplest option. Add a class to the image, define the animation’s intermediate states with @keyframes, then set its duration, timing, repetition, and direction with the animation property.
<img class="float" src="kite.png" alt="A kite floating in the sky">
.float {
animation: float 2s ease-in-out infinite alternate;
}
@keyframes float {
from { transform: translateY(0); }
to { transform: translateY(-12px); }
}
@media (prefers-reduced-motion: reduce) {
.float {
animation: none;
}
}
The shorthand in this example sets the animation name (float), duration (2s), timing function (ease-in-out), iteration count (infinite), and direction (alternate). CSS’s animation properties also cover delay, fill mode, play state, and timeline-related behavior; see MDN’s animation property reference and CSS animations guide.
Recommended Free Tools
#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Change the effect without changing the HTML
Adjust the keyframe values to change the movement: a negative translateY moves the image up, while a positive value moves it down. Replace the transform with another suitable CSS property to scale or rotate the image, or animate opacity for a fade. The CSS approach is useful when the effect should run on its own and does not need application logic to control it.
Control image animation with JavaScript
Use Element.animate() when code needs to trigger an effect, for example after a user clicks a button. It starts an animation on the selected element and returns an Animation object that can be used for playback control.
<img id="photo" src="photo.jpg" alt="A mountain lake">
<button id="move" type="button">Animate</button>
document.querySelector("#move").addEventListener("click", () => {
document.querySelector("#photo").animate(
[
{ transform: "translateX(0)" },
{ transform: "translateX(80px)" }
],
{ duration: 600, easing: "ease-out", fill: "forwards" }
);
});
Place the JavaScript after the image and button in the document, or load it from a script that runs after the HTML has been parsed. The keyframes describe the start and end transforms; the options set a 600-millisecond duration, easing, and a fill mode that retains the final visual state after the animation finishes. The Web Animations API is designed to create and manage animations on DOM elements; see MDN’s Web Animations API guide and Element.animate() reference.
When to pick CSS instead
If the animation is a fixed effect that should run automatically, CSS generally requires less code and keeps its timing declarative. Use JavaScript when an interaction or program state needs to start or manage playback. A user-triggered button can make motion optional, but it does not remove the need to consider reduced-motion preferences.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #2
Use an animated image file for a pre-rendered sequence
If the image sequence has already been created, save it in a suitable animated format and use it as the source of an ordinary <img> element:
<img src="sparkle.webp" alt="A sparkling star">
The browser plays the sequence packaged in the image resource; you do not define its frames with CSS or JavaScript. This is convenient for a self-contained animation, but it is not the right choice when the page needs to control each frame or animate separate shapes. Common choices include GIF, APNG, and WebP, but browser support and the trade-offs in file size and quality differ. Check the formats your target browsers support before choosing an asset. MDN’s image element reference describes the element that embeds the resource.
Animate vector artwork with inline SVG
When motion needs to affect individual vector shapes or SVG attributes, put the SVG markup inline in the HTML so its elements are part of the document. SVG provides animation elements such as <animate>, which can animate an SVG attribute over time. MDN documents the SVG animate element.
An SVG loaded through an image element is treated as an image, not as an interactive SVG document whose internal elements can be targeted. SVG’s <image> element can display raster or other SVG files, but embedding artwork that way does not make its internal shapes interactive; see MDN’s SVG image element reference. Use inline SVG when you need to animate its internal elements.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Rank #3
Make motion accessible and keep it purposeful
Some people experience discomfort from motion, including people with vestibular disorders or other sensitivities. Respect the prefers-reduced-motion preference by removing or reducing non-essential movement, as in the CSS example above. MDN explains the prefers-reduced-motion media feature and broader media-query accessibility considerations.
- Consider a pause or disable control for ongoing animation, especially when it is not essential to understanding the page.
- Avoid flashing or continuous attention-grabbing motion unless it conveys necessary information.
- Be particularly cautious with large objects that pan or scale, since those movements can be uncomfortable.
- Keep the number and size of animations purposeful: larger animations and more simultaneous animations can use more processing power.
- Test the result on the devices and browsers your audience uses, including the reduced-motion experience.
MDN recommends CSS animations where possible for essential DOM animations, while the Web Animations API is available when JavaScript needs to connect to CSS-like animation behavior or manage it directly. See its CSS performance optimization guide.
Or skip the browser setup
If you need a screenshot of a page containing an image animation, you can request a capture from ScreenshotNeo, a website screenshot API and MCP server for developers. Its API returns an image or PDF from one GET request. See the ScreenshotNeo API documentation for available parameters.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
- Cookie and consent banners are accepted like a visitor, and more than 60 known consent platforms, newsletter popups, and chat widgets can be removed before capture; each step can be turned off.
- Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. Response headers report the page verdict and billing status.
- An MCP server offers
take_screenshot,get_page_info, andcapture_pdftools for Claude, Cursor, and other MCP clients. - The Free plan includes 1,000 screenshots per month with no card required; paid plans start at $5 for 3,000 screenshots.
Sign up for ScreenshotNeo’s free plan and get 1,000 screenshots a month with no card.
Rank #4
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Troubleshooting image animations
The image does not move or fade
- Confirm the CSS class in the HTML matches the selector in the stylesheet.
- Check that the
animationproperty names a keyframe rule that exists and that the stylesheet is loaded. - For JavaScript, check that the selected element exists when the event listener is attached, and that the button click runs the handler.
- If movement is deliberately disabled by a reduced-motion media query, check the device or browser’s motion preference before treating that as a failure.
The image moves, but it does not stay at the end
In the CSS animation shorthand, consider a fill mode if the final keyframe should remain applied after playback. The JavaScript example uses fill: "forwards" for that reason. Choose the behavior that fits the effect rather than retaining a final state by default.
An animated file appears static
- Verify that the source file actually contains multiple frames and is the intended animated asset.
- Check the format against the browsers you need to support; support can vary by user agent.
- If the requirement is to control playback or individual visual states from the page, use CSS or the Web Animations API instead of relying on an asset’s built-in sequence.
SVG shapes cannot be targeted
An SVG embedded with <img> is treated as an image rather than an interactive document. Put the SVG markup inline if animation needs to operate on its internal elements.
Animation makes the page feel heavy or uncomfortable
Reduce the number of simultaneous effects, avoid unnecessary large-scale movement, and honor reduced-motion preferences. Test on your target devices; animation workload depends on the effect and the device, so do not assume a decorative effect is free.
Frequently Asked Questions
Does the HTML <img> element animate an image by itself?
No. It embeds the image resource. CSS, JavaScript, an animated file format, or SVG provides the animation behavior.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Best Value
Can I animate an image with CSS without JavaScript?
Yes. CSS keyframes can animate properties on an image element without JavaScript.
Which animated image format is best?
There is no universal winner. Choose among formats such as GIF, APNG, and WebP based on browser compatibility, file size, and quality needs.
Can I animate shapes inside an SVG loaded with <img>?
Not interactively through the embedding document. Use inline SVG when you need to target and animate its internal elements.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

