Stylish progress bar using CSS animations

I am trying to make the progress bar expand from 0% width to 50% width in a span of 2 seconds. Here is the code I have written:

<style>  
  #progressbar {
      background-color: #000000;
      border-radius: 8px;
      padding: 3px;
      width: 400px;
    }

    #progressbar div {
       background-color: #0063C6;
       height: 10px;
       border-radius: 5px;
       animation:loadbar 2s;
        -webkit-animation:loadbar 2s;
    }

    @keyframes loadbar {

    0% {
        width: 0%;
    }

    100% {
        width: 50%;
    }

    @-webkit-keyframes loadbar {

    0% {
        width: 0%;
    }

    100% {
        width: 50%;
    }

}
</style>

<div id="progressbar">
    <div></div>
</div>

Upon opening the page, the progress bar's width fills up completely rather than reaching 50%. Can you help me identify what mistake I may have made?

Answer №1

Your progress bar animation was not properly closed before. The animation should now be functioning correctly. I have also included a "forwards" keyword to ensure the animation only plays once.

#progressbar {
  background-color: black;
  border-radius: 8px;
  padding: 3px;
  width: 400px;
}

#progressbar div {
  background-color: #0063C6;
  height: 10px;
  border-radius: 5px;
  animation:loadbar 2s normal forwards ease-in-out;
  -webkit-animation:loadbar 2s normal forwards ease-in-out;
}

@keyframes loadbar {

  0% {
    width: 0%;
  }

  100% {
    width: 100%;
  }
}
@-webkit-keyframes loadbar {

  0% {
    width: 0%;
  }

  100% {
    width: 100%;
  }

}

Answer №2

Check out this Fiddle example

#progressbar div {
   background-color: #0063C6;
   width: 50%;
   height: 10px;
   border-radius: 5px;
   animation:loadbar 2s;
   -webkit-animation:loadbar 2s;
}

@keyframes loadbar {

  0% {
    width: 0%;
  }

  100% {
    width: 50%;
  }
}
@-webkit-keyframes loadbar {

  0% {
    width: 0%;
  }

  100% {
    width: 50%;
  }

}

Answer №3

Check out the jsFiddle demo

Start by setting the initial width to 0%

#progressbar div {
   background-color: #0063C6;
   height: 10px;
   width:0%; /* Don't forget to add this line */
   border-radius: 5px;
   animation:loadbar 2s;
   -webkit-animation:loadbar 2s;
   -webkit-animation-fill-mode: forwards;
   animation-fill-mode: forwards;
}  

In addition, I included the following code...

-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;

If you'd like the animation to end with a forwards motion, it's necessary to have this... feel free to check out the demo without it here in jsFiddle

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

Switching CSS styles - seeking a smoother integration

I have successfully implemented a JS CSS switcher, which works brilliantly. However, I would like it to function more seamlessly. When opening a new page on the site, the default CSS style sometimes flickers briefly before the selected CSS style is reappli ...

What is the best way to adjust my carousel so that it occupies just 60% of the screen, allowing the panels beneath it to utilize the remaining space

How can I adjust the size of the carousel to occupy only 60% of the screen without the need for scrolling, allowing the panels directly below to fill the remaining 40%? The images within the carousel should resize accordingly based on the desktop screen si ...

Rotating arrows enhance the functionality of the accordion menu

I have successfully implemented a basic accordion with rotating arrows on click. Everything is working smoothly except for one issue: When I collapse one item and then try to collapse another, the previous arrow does not return to its default state. Is ...

How to create a scrollable table using HTML and CSS

I created a Dashboard, but I'm having trouble making my mailPage scrollable. If you have some time to spare, could you please take a look at my website and help me with this issue? Here is my code: window.addEventListener("load", function () { ...

CSS animations can make the navigation bar vanish into thin air

I recently started implementing CSS3 animations from the Animate.css library and I must say, they really enhance the look and feel of my website. The animations pair perfectly with WOW.js to create stunning effects. However, I have encountered a minor iss ...

Javascript malfunctioning - exhausted all troubleshooting options

My JavaScript code consists of a single line, but I keep encountering the "getElementById null" error. document.getElementById("menu-background").style.left = "0"; HTML: <html> <head> <title></title> <link rel="style ...

The function view() is not displaying the CSS and JS files properly

Having trouble with loading the css and js on my view: This is how I set up the controller: return view('home.news') ->with("news", $news); And here's how the route is defined: Route::get('/news/{id}&apos ...

What are the steps to insert a plotly graph into a webpage so that it responds to different

I have been working on integrating a plotly pie chart into my website. In order to make the iframe responsive, I decided to utilize this specific tool to generate the necessary html/css. While the chart appears satisfactory on a laptop screen, it is barely ...

The newly added radio button does not function as a separate group as expected

I currently have a set of radio buttons: <input type="radio" class='form-control' name="progress_type[]" value="Journal Papers"><span class='radio-label'>Journal Paper</span> <input type="radio" class='form-co ...

How can you identify dynamically created elements within an AngularJS directive?

I have a directive where I need to target specific DOM elements, some of which are dynamically generated in an ng-repeat loop. If I try to select them directly, I only get the static elements. However, if I delay the selection by, let's say, 500ms, I ...

CSS-only Modal (Utilize CSS to display or conceal a row upon hover of the preceding row)

Is it possible to create a hover effect in CSS that collapses or shows a table row when hovering over the previous row? I'm looking to display additional information on hover over a specific row in a table. ...

What is the best way to determine when an IndexedDB instance has finished closing?

In the world of IndexedDB, the close method operates asynchronously. This means that while close finishes quickly and returns immediately, the actual connection closure happens in a separate thread. So, how can one effectively wait until the close operatio ...

What could be causing the compatibility issue between fr units and grid-template-rows in CSS grid?

I'm having trouble getting the row height to change using fr units in my grid-template-rows. It seems to work fine with other units like pixels, but not with fr. I find it confusing that the fr units work perfectly fine with grid-template-columns, so ...

positioning two out of three items in a navigation menu to the right and the remaining one to the left

I am looking to design a navigation bar that spans the full width of the screen, with the logo on the left side serving as a link back to the home page. Additionally, I want to include elements for register/login and cart. The background color should cove ...

Is it Possible to Stack Multiple Rows of Images with HTML5/CSS3?

I am struggling to arrange images in rows, but I'm facing unexpected outcomes. The images only break to a new line when the page is filled. I attempted to use overflow:hidden, however, the expanding images end up hidden under white space. I can't ...

Having trouble getting your React project to work because NPM start won't cooperate? Here are some troubleshooting tips

Hello, I recently downloaded a React project from https://github.com/fabiau/jc-calendar. However, when I try to run npm start, I encounter error messages. I have attempted to use "NPM Fund" and "NPM Update", but unfortunately, neither of them resolved the ...

The true height is never fully accurate when a vertical scrollbar is visible

Having an issue that needs solving. I've created a simple layout, but I'm struggling with getting the sidebar and wrapper to adjust their height properly. This is how the layout looks: <div class="sidebar collapse"> <div class="sideb ...

The JavaScript date picker is malfunctioning in the HTML editor, but it functions properly in Fiddle

I have a working format available in a JS fiddle. Here is the code I have used on my demo site: I created a new folder named "js" and placed datepicker.js inside it, then linked it in my HTML like this: <script type="text/javascript" src="js/datepicke ...

Can a plugin be executed as a test?

I am currently using HTMLhint, however, I would like to have it run as a test rather than just through the command line plugin. Is there a way to achieve this and if so, how can I do it? I have searched online but haven't been able to find a solution ...

JavaScript must be able to detect when the checkbox value is reversed, which is dependent on the user-entered data

Hey there, I come across a situation where users are selecting a checkbox to insert or update a row of data in a MySQL database through SparkJava/ Java. The functionality is working fine except for a minor glitch. The issue arises when the checkbox behav ...