Stopping the Stroke Animation in CSS at the Final Frame of the SVG

Check out this CodePen

The SVG in the codepen above has a stroke that animates into view, but unfortunately disappears at the end of the animation.

Is there a way to make sure it stays visible once it's fully loaded?

.path {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: dash 5s linear alternate;
}

@keyframes dash {
  from {
    stroke-dashoffset: 1000;
  }
  to {
    stroke-dashoffset: 0;
  }
}

Answer №1

Make sure to include the following properties in your .path

  animation-fill-mode: forwards; // Ensures last frame stays visible
    animation-iteration-count: 1;// Animation runs only once 

Your CSS should look like this:

.path {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: dash 5s linear alternate;
animation-fill-mode: forwards; // Ensures last frame stays visible
    animation-iteration-count: 1;
}

You can view this on Codepen Here, and it is working perfectly.

Answer №2

The solution provided may not align perfectly with the original question, but if you are looking to use SVG's <animate> tag for your animation and have run into a similar issue, you can utilize the fill attribute. Here is an example:

<animate ... fill="freeze"/>

By using this attribute, the animation will freeze at its final frame upon completion. For instance:

<svg viewBox="-5 -5 100 20" xmlns="http://www.w3.org/2000/svg">
  <rect width="90" height="10">
    <animate attributeName="width" values="0;90" dur="3s" repeatCount="1" fill="freeze"/>
  </rect>
</svg>

For more information, check out the reference here.

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

Changing the style of TinyMCE dropdowns using CSS

I successfully incorporated the tinyMCE editor into my Vue.js Application. init = { height: 500, menubar: false, plugins: [ 'advlist autolink lists link image charmap print preview anchor', 'searchreplace vi ...

What is the most effective way to create these lines using CSS?

Is there a way to create the background image shown in the link below? https://i.sstatic.net/YB4N7.png ...

Steps for positioning grid lines behind the flex container

Currently, I have a flex container with several flex items arranged from top to bottom. My goal is to create light grey color lines behind this flex container to achieve a design similar to this. I attempted to add another flex container on top of this on ...

What is the process for assigning a CSS class to a div element that is generated by react's render() function?

In React, I am encountering an issue where the css class I want to set on the div returned from render is being removed. This problem even persists when I try nesting another div inside the first one and setting the class on the enclosed div. Has anyone ...

DIV that floats and P element

When utilizing the float attribute, two div elements align next to each other, but two paragraph elements do not; causing the 'p' element to display oddly. Interestingly, when floating two 'div' elements and two 'p' elements, ...

Hide the overflow for images

Looking for a way to display 6 images divided into 2 rows and 3 columns on large screens, but switch to displaying only 4 images with one image per row on small screens? You can achieve this using CSS. Any ideas on how to implement it? Thank you. ...

Which is better: Starting with rows or columns in Bootstrap 4 layout design?

Many Bootstrap 4 grid layout demos feature columns within rows, but is it considered improper to have rows within columns? For example: <div class="col col-md-6"> <div class="row"> </div> </div> ...

New programmer seeking to apply a dim effect to the entire photo while also adding a highlight when the image is hovered over

I am looking to add a special effect to my photos where they appear dimmed but highlighted when hovered over. Any guidance on how to achieve this would be greatly appreciated! The image is contained within a flexbox div that also contains a clickable lin ...

Utilizing jQuery or Javascript to obtain the title of the current webpage, find a specific string within it, and then apply a class to the corresponding element

Let's imagine I start with this: <title>Banana</title> and also have some navigation set up like this: <ul id="navigation"> <li> <a href=#">Banana</a> </li> </ul> Upon loading the sit ...

React: Struggling to Get Margins Right for Navigation Bar Spacing

I recently tried my hand at following a tutorial for building a React app: React JS Tutorial for Beginners #1 – Build a website using React, Sass, Gulp and Node.js After completing the tutorial, everything seemed to be working smoothly except for one th ...

Is the CSS for the selected option not functioning properly in Internet Explorer version 10?

$('#inputTest').on('input', function() { $('#helloSellect option').css({ display: "none" }); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="inputTe ...

Tips for aligning elements of varying sizes seamlessly with CSS grid?

I am currently working on creating a CSS grid layout. The issue I'm facing is that the first element has a fixed height, causing the entire first row to have the same height. I want the elements in the second row to move up and fill the empty space in ...

The navigation on my mobile collapses whenever I use the Hello bar app

I am facing a problem with my Shopify theme. I recently installed an app called "Hello Bar" to show offers on all pages, but it seems to be incompatible with my theme. The developers of the app suggested adding the following code to my CSS file: @media ...

Can I split icons instead of combining them when stacking in Font Awesome?

Is it possible to stack icons by dividing their size instead of multiplying them? I am looking to have my first icon be the same size as the font I am using, and the icon on top of it to be half the font size. Can this be achieved with Font Awesome or do I ...

Troubleshooting problem with table reflow in Bootstrap v4.0.0-alpha.3 on mobile devices

I am having trouble fixing the table-reflow issue in mobile view while trying out the code below. Any suggestions on how to resolve this would be greatly appreciated. Check out the image here To see the code, visit CODEPEN <div class="container"> ...

Is it possible to maintain a div's height in proportion to its width using CSS?

Is it possible to set a CSS variable so that my div's height is always half of its width, and the width scales with the screen size (in percentage)? <div class="ss"></div> CSS: .ss { width: 30%; height: 50% of the width; } This ...

Guidelines for Implementing Stylesheets from a Referenced Node Module

I am currently developing a VS Code module that incorporates highlight.js to produce HTML for syntax highlighting of source code. Within the highlight.js npm package, there is a directory named styles containing various CSS files that I intend to utilize. ...

Introducing a variety of sub-menu fonts within an elegantly designed jQuery vertical accordion menu

I am struggling to achieve different font sizes, colors, and weights for top and sub-menu level links in my simple jQuery vertical accordion. Despite changing the CSS, I can't seem to get it right. What am I missing? Is there an easy way to accomplish ...

No JavaScript needed for this CSS framework

I have been tasked with creating a website without the use of JavaScript, following very specific instructions. Furthermore, it must be compatible with iPhone and other mobile devices, while still adhering to the no JavaScript rule. I am open to utilizing ...

Discovering the font-weight attribute in Selenium IDE

Currently, I am working with Selenium IDE and aim to detect when a text element has the 'font-weight:bold' CSS property applied to it using 'verifyAttribute'. The specific CSS locator being used is: css=body.home.es ul#languages li:nt ...