Changing the width of an SVG path from 0 to 100% using CSS

I have an SVG design of wires that I want to animate the width from "0 to 100%", but I'm having trouble achieving this effect. It's easy to do with a div element, but not with an SVG image.

Below is my code snippet:

svg path {
 animation: fadeRight 1s ease-in-out forwards;
}
@keyframes fadeRight {
  from {
    width: 0
  }
  to {
    width: 100%
  }
}
<svg version="1.1" 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 1000 694.3" style="enable-background:new 0 0 1000 694.3;" xml:space="preserve"><g><g><g id="Layer_1_1_" class="st6"><g><path class="st5" d="M1273.1,68.1c-0.1-0.3-0.3-0.4-0.5-0.3c-20.7,6.9-78.4,22-149.1,29.4c-51.9,5.4-103.3,6.2-152.7,2.2C909,94.4,850.2,82,795.9,62.6c-0.1,0-0.1,0-0.2,0c-0.1,0-0.3,0-0.3,0.1c-32.4,38-68.9,67.5-108.4,87.4c-31.5,15.9-65.1,25.9-99.8,29.6c-47.2,5.1-87.1-3.1-101.6-8.1c0,0-0.1,0-0.1,0c0.1-0.2,0-0.4-0.2-...

If you want to view the live website with the full wire animations, visit this URL:

Note: I understand that path elements cannot have a width attribute, and despite trying various solutions, I have been unable to achieve the desired effect. That's why I am reaching out for help here. Please suggest any alternative animation methods or ideas on how I can animate it.

Animation Request: I specifically require an animation that goes from 0% to 100%, as scaling and translating won't achieve the desired smooth effect I'm looking for. Think of it like a loading progress bar transitioning smoothly from start to finish. The challenge is that the wire is curved, unlike the usual straight loaders. Any assistance would be greatly appreciated as I've hit a roadblock in my progress.

Answer №1

  • One option is to clip it and change the clip, as demonstrated below.

  • Another alternative is to use stroke-dasharray to create an effect where the line appears to be drawn. Examples of this technique can be found in resources like this one.

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 1000 694.3" xml:space="preserve">
  <g id="Layer_1_1_" class="st6">
    <g>
      <path class="st5" d="M1273.1,68.1c-0.1-0.3-0.3-0.4-0.5-0.3c-20.7,6.9-78.4,22-149.1,29.4c-51.9,5.4-103.3,6.2-152.7,2.2C909,94.4,850.2,82,795.9,62.6c-0.1,0-0.1,0-0.2,0c-0.1,0-0.3,0-0.3,0.1c-32.4,38-...
      <clipPath id="c">
        <rect height="100%" width="0%" fill="black">
          <animate attributeName="width" dur="1s" from="0" to="100%" fill="freeze" />
        </rect>
      </clipPath>
    </g>
  </g>
</svg>

Answer №2

For SVG paths, there is no width attribute and they cannot be styled using the CSS width property. Instead, you can utilize a scale transform.

svg path {
  animation: fadeRight 1s ease-in-out forwards;
}
@keyframes fadeRight {
  from {
    transform: scaleX(0)
  }
  to {
    transform: scaleX(1)
  }
}
<svg version="1.1" 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 1000 694.3" style="enable-background:new 0 0 1000 694.3;" xml:space="preserve"><g><g><g id="Layer_1_1_" class="st6"><g><path class="st5" d="M1273.1,68.1c-0.1-0.3-0.3-0.4-0.5-0.3c-20.7,6.9-78.4,22-149.1,29.4c-51.9,5.4-103.3,6.2-152.7,2.2C909,94.4,850.2,82,795.9,62.6c-0.1,0-0.1,0-0.2,0c-0.1,0-0.3,0-0.3,0.1c-32.4,38-...

Answer №3

Adjusting the width can be done in the following way:

line {stroke-width: 20}

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

Tips for integrating an HTML template into a React project

I'm finding it challenging to integrate an HTML template into React. The template I am using is for an admin dashboard with multiple pages. I have successfully copied and pasted the HTML code into JSX files and fixed any syntax issues. Here's wh ...

Crafting a personalized arrow for sorting headers in Angular Material

Currently working on an Angular 5 project and I'm looking to implement a custom sort icon in the header. The goal is to achieve a similar effect to this example, without using the default arrow. I attempted to modify the CSS styles, but it wasn' ...

Automatically activate the next tab in Bootstrap

I have a modal that performs a count. Once the count is completed, the modal closes. My goal is to automatically switch to the next tab in Bootstrap when the modal closes. Here is the HTML: <ul class="nav nav-tabs namelocationtable"> <div clas ...

Disabling collapse in Bootstrap 5 is impossible using stopPropagation() when clicking on a particular element

I'm attempting to prevent an element from collapsing/expanding when clicking a checkbox inside it. In my example on codepen, my code worked fine with Bootstrap v4, but after switching to v5, the stopPropagation function doesn't seem to work. Yo ...

Font and div placement varies on a mac within a WordPress environment

www.oihanevalbuenaredondo.be After styling my post titles in CSS using font-family: adobe arabic, I noticed that while they display correctly on my Windows machine, they appear in a basic font on Mac OS, Safari specifically. Why is it that Safari cannot r ...

The placeholder is invisible

After thoroughly researching this topic, I am still stumped as to why my Placeholder is not appearing. I would greatly appreciate any insights or expertise you can provide to help me understand what I might be doing incorrectly. <input type="text" name ...

The checkbox click function is not functioning properly when placed within a clickable column

In my coding project, I decided to create a table with checkboxes in each column. <table class="bordered"> <thead> <tr style="cursor:pointer" id="tableheading" > <th>Name ...

Validating SASS based on class name

Hi there, I am fairly new to SASS and do not consider myself a programming expert. I have ten aside elements that each need different background colors depending on their class name. Even after going through the SASS documentation, I am still unable to f ...

Offering various language options on a website determined by the URL

I've been contemplating how to add multi-language support to my personal website, which I developed using ExpressJS and NodeJS with EJS as the template engine. Currently, the website is only available in English, but I want to add a German version as ...

Storing data collected from a Google form into an Excel spreadsheet

I have created a form using table layout. My goal is to automatically shift the focus to the next input field once the current one reaches its maximum length. I tried implementing this functionality with jQuery, but it only seems to work with inputs and no ...

Refresh HTML table when database is updated

My panel has a user table which is populated using the following snippet of Php code : <html> <body> <div id="order_table"> <table> <thead> <tr> <th>Name</th> < ...

The function fromEvent is throwing an error stating that the property does not exist on the type 'Event'

I'm attempting to adjust the position of an element based on the cursor's location. Here is the code I am currently using: this.ngZone.runOutsideAngular(() => { fromEvent(window, 'mousemove').pipe( filter(() => this.hove ...

When you click, you will be directed to the specific details of the object

I have a recipe component that displays a list of recipes from my database and a recipe-detail component that should show the details of a selected recipe. What I aim to achieve is that when someone clicks on a recipe name, they are routed to the recipe-de ...

Popover disappears when scrolling the page, but its position remains constant in Angular 4+

After clicking on an icon, a popover appears. I've noticed that the popover has a delay set, but when I scroll, the popover also moves along with it. Is there a way to keep the popover in place and not have it affected by scrolling? <md-badge cla ...

"Utilizing lightbox JS to produce a centralized overlay across the entire webpage

Currently in the process of redesigning a gallery for a client at www.stagecraft.co.uk/gallery.html I am encountering some challenges with my overlay. I want it to be centered and on top of any divs that are currently in view, as new images from the clie ...

Template for Joomla with a white stripe running along the right side

When I resize my browser to be half the width of my screen at www.thames.ikas.sk, a white stripe appears on the right side. Why is this happening? Why aren't my html and body elements taking up the full width of the browser? This issue doesn't oc ...

Utilizing Jquery cycle to overlay a div on top of a scrolling image

Hey there! I'm currently using the jquery.cycle.all.js plugin for my website. I've encountered an issue where I want to position a menu div on top of an image slider. The menu is in the correct location, but unfortunately, it's hidden benea ...

Transitioning in CSS involves animating the changing of a CSS property over

My goal with CSS is to create a transition effect with a delay that behaves differently when entering and exiting. Specifically, I want a 1 second delay when an element enters, but no delay (0 seconds) when it exits. transition: width 0.3s ease-in 1s; tra ...

Fuzzy division following a CSS transformation/animation

I have been working on a content slider, using the guidelines from this demonstration However, I have encountered an issue where my content, which consists of a few divs, appears slightly blurry after the CSS transition completes. This problem only seems ...

Encountering [Object] error in Angular's ngbTypeahead functionality

Here is the code for my input field in component.html: <input type="text" class="form-control" [(ngModel)]="searchQuery" [ngbTypeahead]="recommends" name="searchQuery" typeaheadOptionField="user ...