Animating SVG elements using CSS

After learning how to use the <animate> attribute, I successfully created a captivating animation featuring an elastic line. However, I am now determined to transform this into CSS for better control: my goal is to activate the animation only upon hovering over the line element. Unfortunately, I am facing difficulties in achieving this.

If you want to see a demonstration of what I have described, please check out this Live Example.

Here you can find my HTML and SVG code:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" class="cntr" id="grid">
      <defs>
        <style>
          path {
            stroke: red;
            stroke-width: .2;
            stroke-linecap: round;
            fill: none;
          }
        </style>
      </defs> 
      <g class="drawing">
        <g class="path-wrap">
          <path d="M 30,40 Q 50,40 70,40">
            <animate
              attributeName="d"
              dur="1.5s"
              begin="1s"
              repeatCount="indefinite"
              values="M 30,40
                      Q 50,40 70,40; 

                      M 30,40
                      Q 50,50 70,40;
                      
                      M 30,40
                      Q 50,35 70,40;
                      
                      M 30,40
                      Q 50,45 70,40;
                      
                      M 30,40
                      Q 50,38 70,40;
                      
                      M 30,40
                      Q 50,42 70,40;
                      
                      M 30,40
                      Q 50,39 70,40;
                      
                      M 30,40
                      Q 50,41 70,40;
                      
                      M 30,40
                      Q 50,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;"
              fill="freeze"
            />          
          </path>
        </g>
      </g>
</svg>

Is there a straightforward method for converting <animate> attributes to CSS? Or is this task deemed impossible?

Answer №1

According to @Robert Longson, it has been noted that

Only Chrome supports CSS animation of path d attributes

If you aim to effectively manage SVG animations, you can utilize a robust set of attributes and parameters similar to those in CSS animations. For instance, if you wish to create the start and end points of an animation triggered by hover events, you would need to specify:

begin="grid.mouseover"
end="grid.mouseout"

The animation within the SVG canvas will continue as long as the cursor is hovering over it, but will cease when the cursor moves outside the canvas.

<svg id="grid" xmlns="http://www.w3.org/2000/svg" width="30%" height="30%" viewBox="0 0 100 100" class="cntr" style="border:1px solid gray;">
      <defs>
        <style>
          path {
            stroke: red;
            stroke-width: 1;
            stroke-linecap: round;
            fill: none;
          }
        </style>
      </defs> 
      <g class="drawing">
        <g class="path-wrap">
          <path d="M 30,40 Q 50,40 70,40">
            <animate
              attributeName="d"
              dur="1.5s"
              begin="grid.mouseover"
              end="grid.mouseout"
              repeatCount="indefinite"
              values="M 30,40
                      Q 50,40 70,40; 

                      // additional animated values...

                      M 30,40
                      Q 60,40 70,40;

                      M 30,40
                      Q 40,40 70,40;"
              fill="freeze"
            />          
          </path>
        </g>
      </g>
</svg>

Answer №2

For the "begin" attribute, simply switching it to begin="mouseover" will make it function properly.

                 attributeName="d"
                 dur="1.5s"
                 begin="mouseover"
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that the text is wrapped properly when implementing innerHTML functionality

Within my Angular 5 application, I am faced with a requirement to display HTML content retrieved from a database. An example of the text stored in the database is: <div><u>Documents and Files</u></div><ul><li>These docu ...

What is the best method to ensure a TALL table header is repeated on every page in Chrome?

First time poster =) I am currently facing an issue while trying to print a long table and have the table header repeat on each page. The problem arises when the height of the <thead> reaches a certain threshold, specifically 242px in Chrome 87.0.42 ...

What is the procedure for inserting comments into inline CSS?

You know, I've been wondering how to effectively comment out inline CSS styles within HTML tags. Is it best to use the /* */ comments from CSS? Or should we opt for <!-- --> in HTML? <span class="color_dark" style="top:20px;font-size: 17px;l ...

In HTML, specify that the height of both the `html` and

I am facing an issue with the container-about div. I have set its height to 100% so that the div occupies the entire width and height after the header div. The problem is that currently, I cannot scroll to view the full text. It would be great if there wa ...

Organizing content on a webpage with specific pixel measurements

I have a div and am utilizing Bootstrap 5. I'd like to have the Description and Length elements on separate lines when the page width is <=1024px. <div class="col-3 col-md-4 col-lg-3"> <div class="card d- ...

Tips for applying/styling "All Items Center" and "Space Between Items" in CSS and ReactJS

justify-content: space-evenly; seems to be working correctly, but the desired output is not achieved I am aiming for something like this: https://i.stack.imgur.com/iQoWK.png However, this is what I currently have: https://i.stack.imgur.com/1PxGR.png ...

Swapping link positions with jQuery

Is it possible to rearrange links using jQuery? Under the navigation menu, there will be content div for each tab. The selected link should always be positioned next to the first "sixrevision" link. The code snippet is shown below: <ul id="crumbs" ...

Modify the display of multiple divs within a main div

I am facing an issue with a parent div that contains multiple child divs. When there are many children, they all remain in a single row without adjusting into columns. Below is my current HTML: item1 item2 item3 ... <mat-card class="hove ...

What is the best way to position this span on the right side of the div?

Here is some HTML code snippet: <div class="title"> <span>Cumulative performance</span> <span>20/02/2011</span> </div> This is the corresponding CSS: .title { display: block; border-top: 4px solid #a7a ...

"Enhance your listening experience with an audio player featuring album covers as captivating

Is there a way to create an audio player with the album cover image serving as the background, while ensuring all control buttons are displayed on top of that same image? I attempted using the following code snippet: <audio controls = 'controls&ap ...

Guide to creating a continuous loop for CSS3 animation

I'm looking to create a setup that can play on repeat, but I'm not very familiar with jQuery. Can anyone lend a hand with this? Check out the DEMO here $('.title1').fadeIn('fast', function() { $('div').addClass ...

The functionality for bold and italics does not seem to be functioning properly in either Firefox

Text formatted with <b></b> and <i></i> tags appears correctly on iPhone and Internet Explorer, but lacks formatting in Firefox or Chrome. I have included the .css files below. Additionally, I attempted to add i { font-style:italic ...

Bringing elements into perfect harmony in Angular

Seeking guidance on HTML alignment using bootstrap for prebuilt components like buttons. Struggling with aligning divs and looking to learn more about grouping them together for proper alignment. Goal is to center elements on screen one above the other. ...

I am unable to apply CSS to style my <div> element

I've run into a snag with my coding project, specifically when attempting to style my div. Here is the code I have so far: All CSS rules are applying correctly except for the .chat rule. Can someone help me figure out what I'm doing wrong? var ...

Establish limits for draggable item

I am working on a project where I have a draggable element generated dynamically using jQuery. The div containing the element also has a background image. How can I ensure that the draggable element never goes outside of the boundaries of the div? If you ...

Attempting to change the primary color in Bootstrap is ineffective following a button click

I have been attempting to customize the primary color of Bootstrap 5 using SCSS. Everything works perfectly until I click a button that opens an external link. Below is the content of my SCSS file: $primary: #ae217aff; @import "../node_modules/boots ...

What could be causing the div class to move downward in the CSS code?

I have posted a question, but unfortunately, no one has answered it yet. Here is my code snippet: * { margin: 0; padding: 0; } body { width: 100%; font-family: 'Cabin', sans-serif; background-color: #333; } .firstnav { ...

Unable to dynamically update properties in CSS and JavaScript without refreshing the page

I've tried applying active properties in my CSS and JavaScript, but it doesn't seem to be working as expected. I can click on each list item, but the active state is not being displayed correctly. Can someone please assist me with this issue? I w ...

Issue arises when CSS selectors do not function properly with absolutely positioned divs

I am experiencing an issue with my CSS code that is functional on CodePen but not on my actual website. I am attempting to target the .appraisals class for manipulating the background color of an opaque screen slider containing information. Despite using a ...

Experience a seamless integration of playing videos in the background using HTML5 with responsive design

Looking for a CSS trick to create a video background that spans the full width of the browser and auto resizes with the browser size. It's essential that it plays on Safari, so please specify the supported video file types. ...