Its a beautiful day go be nice to someone!

Converting Gif to Animated SVG using CSS *NO CODING KNOWLEDGE NEEDED*

🐧Peng1.eth/tez🐧
4 min readFeb 2, 2024

--

Lets dive straight into it.

First you will need to convert the frames of your GIF to individual SVG’s. There are a number of tools available online to do this for free, or you can use Photoshop/Illustrator to perform this task. Feel free to contact me if you get stuck and need some pointers.

Now the beauty of SVG.

This is the full code with notations. Below is a brief explanation of the components, but you can skip ahead to How to use it if you just want to get straight to it.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

<svg version=”1.1" baseProfile=”tiny” id=”Layer_1" xmlns=”http://www.w3.org/2000/svg" xmlns:xlink=”http://www.w3.org/1999/xlink"
x=”0px” y=”0px” viewBox=”0 0 1024 1024" overflow=”visible” xml:space=”preserve”>
<style>
.frame {
opacity: 0; /* Start with all frames hidden */
animation: frameAnimation 9s infinite; /* Total animation time */
}
@keyframes frameAnimation {
/* Define keyframes for each frame’s visibility */
0%, 100% { opacity: 0; }
11.11%, 33.33% { opacity: 1; }
33.34%, 55.55% { opacity: 0; }
55.56%, 77.77% { opacity: 1; }
77.78%, 99.99% { opacity: 0; }
}
.frame1 { animation-delay: 0s; }
.frame2 { animation-delay: 3s; } /* Delay so frames appear in sequence */
.frame3 { animation-delay: 6s; }
</style>
<! — Frame 1 →
<g class=”frame frame1">
<! — Embed your first SVG frame content here →
<rect x=”100" y=”100" width=”200" height=”200" fill=”red”/>
<! — Additional SVG elements for frame 1 →
</g>
<! — Frame 2 →
<g class=”frame frame2">
<! — Embed your second SVG frame content here →
<circle cx=”512" cy=”512" r=”100" fill=”green”/>
<! — Additional SVG elements for frame 2 →
</g>
<! — Frame 3 →
<g class=”frame frame3">
<! — Embed your third SVG frame content here →
<polygon points=”924,100 824,300 1024,300" fill=”blue”/>
<! — Additional SVG elements for frame 3 →
</g>
</svg>

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

This simple code uses CSS to create the animation using frames.

How This Works:

  • CSS Animation: Each frame (<g> element) starts with opacity: 0 (invisible). The @keyframes named frameAnimation controls the opacity of each frame, making one frame visible at a time in a sequence.
  • Animation Delay: By setting different animation-delay values for each frame, the frames will appear in sequence, simulating an animation effect where each frame is visible for a portion of the animation cycle before it hides and the next frame appears.
  • Embedded SVG Frames: Each <g> element represents a frame with its SVG content. You can replace the simple shapes (<rect>, <circle>, <polygon>) with your detailed SVG markup for each frame.
  • version, baseProfile, xmlns, xmlns:xlink: These attributes define the SVG's version, profile, and the XML namespaces it uses. They're standard for ensuring compatibility and proper rendering across different platforms.
  • viewBox="0 0 1024 1024": Specifies the coordinate system and aspect ratio for the SVG. It defines a logical canvas size of 1024x1024 units, allowing your SVG content to scale responsively while maintaining its aspect ratio.
  • overflow="visible", xml:space="preserve": Controls how content is displayed if it extends beyond the viewBox and how whitespace in the SVG's text elements is handled.

CSS for Animation

Within the <style> element, CSS rules and keyframes define how the frames will be animated:

  • .frame Class: Applied to each group of SVG elements representing a frame. Initially, all frames are invisible (opacity: 0).
  • @keyframes frameAnimation: Describes the animation sequence. It controls the opacity of elements, making each frame visible in turn over a total animation duration of 9 seconds (9s).
  • Animation Delays: By assigning different animation-delay values to each frame, the frames are made visible sequentially, creating the animation effect.

Frame Elements

Each frame is wrapped in a <g> (group) element with a class of frame and an additional class to target it individually (frame1, frame2, frame3). These groups contain the SVG content for each frame:

  • Frame 1 (frame1): Contains a rectangle. This is just an example; you can replace it with any SVG content.
  • Frame 2 (frame2): Contains a circle. Again, this is illustrative, and you should replace it with your own content for the second frame of your animation.
  • Frame 3 (frame3): Contains a polygon. This serves as an example of a third frame, which you can customize as needed.

How to Use This Code

  1. Customize Frames: Replace the placeholder shapes (<rect>, <circle>, <polygon>) in each frame with your own SVG content. Each <g> element can contain any number of SVG elements to form complex scenes or illustrations.
  2. When pasting your code be sure not to include the <svg> opening and closing tags. Example after pasting it should look like this — <g class=”frame frame1"> <g transform=”translate(0.000000,1024.000000) scale(0.100000,-0.100000)”> <! — <paths etc etc etc→ </g> </g> Be sure to include your <g> tags with transforms.
  3. Adjust Animation Timing: If the default timing doesn’t fit your needs, you can modify the duration in the .frame class's animation property and adjust the percentages in the @keyframes to change when each frame becomes visible.
  4. Add or Remove Frames: To add more frames, duplicate a <g> element, give it a new class (e.g., frame4), and adjust the CSS accordingly. If you want fewer frames, remove the unnecessary <g> elements and update the CSS.
  5. Responsive Design: The SVG’s viewBox ensures responsiveness. However, you might need to adjust the SVG container's width and height attributes or styles depending on how it's integrated into your website or application.

All of this can be done directly in any text editor, visual studio, notepad, etc. Save file as *.svg and open in browser. Tested in multiple browsers. If you find issues in any browser be sure to let me know.

--

--

🐧Peng1.eth/tez🐧

Peng1.art | Multidisciplinary Artist and Graphic Designer and entrepreneur geeking out with web3 and nfts