Live DemoDownload Source Code

Getting Started

While browsing through Google I stumbled onto this post, outlining the CSS3 animation style. It’s actually pretty simple once you understand properties and how the animation/keyframes work together. My page uses a simple HTML5 doctype with a single external CSS stylesheet.
First we should take a look at the HTML markup and how this constructs an active dynamic bar. The page includes two different sections – one with inner labels and one without labels. Since all the skill bars use almost identical code, I’ve copied over just the first bar on the page.


The div #skillset is used to encapsulate the entire collection of skills. This behaves like a wrapper so we could center everything, increase/decrease the width, or adjust other settings in CSS. The inner div with a class .skill wraps around each individual skill being measured. In this case we’re looking at HTML5/CSS3 coding which is ranked at 100% knowledge.
The inner H3 header is in place so the viewer can tell what the skill is measuring. This could be placed inside the skill bar instead, but I chose to put the years of experience inside the bar. You’ll notice the animated bar itself is contained within an outer shell.
The span element .bar uses a static grey background with some box shadow effects to stand out on the page. It was designed in a similar fashion as the CSS Deck example, but I’ve tried to simplify the HTML a bit. The inner span .skillbar is where the animation happens.

Animate Individual Skills

You’ll notice each .skillbar also has a secondary class related to the skill being measured. These are attached to CSS3 animations which run over keyframes. Each skill is measured separately and they each stop at a different point (HTML/CSS just happens to reach 100%).
Since I am using classes for these skill bars we can actually duplicate them many times on the page. This is how I created another set of examples on the same page – by reusing the same animated classes. Since these bars also contain small bits of text, they can’t start at 0px width. Normally this is fine but in this case the text would break onto the next line, and only once the bar animates would the text reposition itself.
So having individual classes means I can setup the original width to match internal text length. This way it never breaks or appears buggy, and the text always stays centered in the skill bar no matter how wide it animates.

Positioning the Elements

My basic CSS properties aren’t too complicated but there are some ideas worth discussing. I’ll start with the overall structure of the skill bars container.


The outer .bar container uses a fixed width/height value to ensure everything looks the same in all browsers. The inner .skillbar does keep a height value with text, but in the second example, those span elements just wouldn’t appear because they’re empty. We need to use 100% height on all the internal skill bars to ensure they appear even without any content.
The centered text effect is also pretty cool because the internal content will animate along with the bar itself. You might not like this effect and it’s pretty simple to change the alignment. But if you consider how easy this all is, you might start brainstorming new concepts to try out.

Animation Fill Modes

When I first started animating the elements, each bar would finish the animation, then automatically resort back to 100% width. This obviously wasn’t my intention so I went in search of a solution.
I was reading that we can setup a default width on the original class like .htmlcss so it ends up at 100% width once the animation is done… or in the case of jQuery the class would need a width of 55%. I feel this almost defeats the purpose having to copy over the width value twice. Then I found out about fill modes which was something I had never heard about before.


Fill modes tell the animation how to utilize styles before & after completion. Using the keyword forwards will force each element to retain CSS styles found in the last keyframe. So in this case we are running a different animation for each class, and each animation has a 0% starting position followed by a 100% completed position. I never knew after the animation completes that it would revert back to the original styles.
If you want to read a bit more on fill modes check out this article by Mozilla which delves into greater detail. The concept is very simple, but I can understand how a new developer trying out CSS3 keyframe animations wouldn’t know anything about it.

Finalizing Keyframes

The last block of code I want to share are the keyframes themselves. We give each keyframe setup a unique name which is represented by the earlier animation properties. I’m using CSS3 prefixes for -moz and -webkit to handle compliance with as many browser versions as possible. You’ll notice the animations are split into blocks grouped by the animation name:


You’ll also notice each animation only uses a starting position and a completed position, all of which should animate within 1.5 seconds. Some of the starting positions use 35px or 45px to fully encapsulate the label text. You could change this value to 0px if you don’t want any text found within the skill bar itself.
Other than these styles, everything else should be pretty well-known. The last big section in my stylesheet redefines the inner gradients for each skill bar – without labels you can try incorporating colors to differentiate between skill sets. This is why my secondary set skill bars also include an .alt class.
Live DemoDownload Source Code

Closing

There are many other fantastic tutorials out there covering this topic and other keyframe animations. Many great code samples can be found on websites like CodePen and JSFiddle if you have time to search. Both my examples with inner text labels, or just the standard percentages, can work in any similar website design.
Be creative with these animations to see how they could fit into other future web projects. You can also download my tutorial source code and try building out more complex animations.