Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this tutorial we're going to add a little bit of a personality to a basic social media nav listing by animating it into place with CSS. This is a fun way to draw attention to certain elements on the page as it loads, but it's definitely one of those things that's best in small doses. We'll also look at animating more than one property in our keyframes. We'll be creating a slight bounce-in animation for our social media nav. So when the page loads, it will look a little like this. Normally, something like this would be in site footer, but for simplicity's sake, I've left it as the only element on my page.
Let's go into Coda and look at the HTML behind this example. The HTML behind this nav is pretty typical nav material. It's a list of links to Flickr, Dribbble, Twitter, and Facebook. If we switched to our CSS, our starter CSS is a bit more complex and usual due to the specific background positioning for each logo image in our nav. Not to worry, though, all the steps adding our animation are still the same despite all these extra starting styles. I've decided that I'd like to have my social-nav bounce into place. So, I'll need to create keyframes that first have it placed out of sight above the page and then I'll have to move it past its normal position at first so it kind of overcompensates and then move it into its originally intended position.
I'm going to need to do this with three keyframes. So, let's start writing our @-keyframes block. I'm going to call this animation bounce-in since that's the effect that we're going for. I'll just add a bit more space so we can see what we're doing. I'll be defining my keyframes in percentages since I know I'll need at least three. So, I'll start it with my 0% keyframe, and in this keyframe I know I want my animation to start with my navigation above the page essentially out of sight, higher up than we can see in the browser.
So we'll set a translation--a translation along the Y axis of -200 pixels. So that should move us up and out of view. And then to create the balance when my animation is almost complete, I want it to move past its original position just a little bit lower than it should be. So I'm going to guess that might be around 90% since that's mostly complete, and I will add a translation here of 8 pixels, so this should be just a little bit lower than it really should be.
And then at 100%, my last keyframe of the animation, I'll add another translate of 0 which will have our navigation end up in exactly the same place as it would have originally with a translation of nothing, of zero. We want to have the entire navigation move with this animation so we'll need to assign the animation to the unordered list that's containing our entire nav. That's up here in our unordered list with the class of social, so I'll add the animation properties here. We'll add our animation.
We'll add the animation name of bounce-in since that's the one that we just defined keyframes for. We'll set our duration to 1 second so it's fairly short, nothing super long in this case. I'm going to set my animation timing function to linear, and I'm going to have this animation iteration count set at 1, so it will only play one time. The direction will be normal and my animation fill mode will be backwards. And the reason I'm going to add backwards in there is so my navigation will stay out of sight at the beginning in case there's any delay in the animation being applied. I'm also setting my timing function to linear to let the spacing of my keyframes implied the ease-in of our little bounce.
That's easier for me than trying to find just the right ease-in to create the bounce. It's just one tiny little bounce, so it's pretty easy to do with the keyframe that we've defined. So now we can save our CSS and go preview what our navigation looks like in the browser. So if we refresh, our nav starts out of sight, falls in, gets a little bit too far, and then bounces back into place. So that's pretty much the effect we are going for. But there's one other thing I'd like to add. Having an element fade in while it animates into place is a pretty nice touch. So let's go and add that to our animation as well.
So we'll go back to our CSS and scroll back down to our keyframes, and I'm going to add an additional property to a couple of our keyframes. I want to have the nav start out as completely transparent or with its opacity set to 0. So let's add that in to our 0% keyframe. Let's make a little space and then add opacity of 0. So, when our 0% keyframe while our nav placed above the page it will also have opacity of 0. Obviously, we're going to need to set this to something you can actually see.
So, we'll also add this opacity property to our 90% keyframe. Make a little space here, and then set the opacity at the 90% keyframe to 1 which is completely opaque or actually 100% visible. So, now our navigation will be fading in while it drops into that initial bottom position. At our 90% keyframe, our opacity is set to 1, which means our navigation is completely visible, and we don't need to list opacity again in our 100% keyframe because we don't want it to change any further during the animation. We want it to stay at 1.
If we don't list the property again in future keyframes to overwrite the last value, it will continue at the last value we set for the remainder of the animation. CSS treats animations of multiple properties in the same set of keyframes as nearly independent. They're part of the same keyframes but they don't all have to change at the exact same time, that's pretty handy. Let's save our CSS and go back and preview our navigation one more time. So, we'll refresh the page. Now, that we've got an effect we like, let's head back to our CSS and add the rest of our vendor prefixes so this will work in browsers that aren't just WebKit.
Great, so now we have all our vendor prefixes in, and we're all set to preview this in any browser we want. We've got a nice little effect going on our social-nav now while the page loads, and we've started developing slightly more complex animations with multiple animated properties within one set of keyframes.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.