
Hello World! So after the last tutorial on styling our menu, we are going to look into animations involving CSS3. We are going to be applying the webkit animation properties to text elements inside of a specific div tag, but you can truly do this with any dom element on the page.
This is what we are going to be making today:
Still interested? Great! That is what I was hoping to view. With CSS3 we can create animations to replace animated gifs, Flash animations, and JS features. If you aren’t interested well scroll to the bottom and check out the source for yourself.
To create animations in CSS3, you will have to learn about the @keyframes rule. The @keyframes rule is where the animation is created. This is very similar to Flash. You will specify a CSS style inside the @keyframes rule and the animation will gradually change from the current style to the new style.
Internet Explorer and Opera do not yet support the @keyframes rule or the animation property. Firefox requires the prefix -moz-, while Chrome and Safari require the prefix -webkit-.
This is the CSS we will be using.
< style>
@-webkit-keyframes pulse {
from {
opacity: 0.0;
font-size: 100%;
}
to {
opacity: 1.0;
font-size: 150%;
}
}
div {
-webkit-animation-name: pulse;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-direction: alternate;
-webkit-animation-duration: 3s;
}
< /style>
It seems simple enough because it truly is. CSS3 makes leaps and bounds to make your life easier. It reduces load time and the need for external libraries to make the UX better. The HTML / JS for this page is as follows
< div id="content"> div>
< p id="log"> p>
< script>
document.getElementById('content').innerHTML = 'animated content';
< /script>
Now everything together into one file would be:
< !DOCTYPE html>
< html>
< head>
< style>
@-webkit-keyframes pulse {
from {
opacity: 0.0;
font-size: 100%;
}
to {
opacity: 1.0;
font-size: 150%;
}
}
div {
-webkit-animation-name: pulse;
-webkit-animation-duration: 3s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-direction: alternate;
}
< /style>
< /head>
< body>
< div id="content"> div>
< p id="log"> p>
< script>
document.getElementById('content').innerHTML = 'animated content';
< /script>
< /body>
< /html>
If you were to do this for Firefox, you would change the CSS to:
< style>
@-moz-keyframes pulse {
from {
opacity: 0.0;
font-size: 100%;
}
to {
opacity: 1.0;
font-size: 150%;
}
}
div {
-moz-animation-name: pulse;
-moz-animation-duration: 3s;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: ease-in-out;
-moz-animation-direction: alternate;
}
< / style>
Following these simple methods can create a nice transition period in dom elements.






8 Responses
Great article even with a jumpy page!
Haha thanks Jon. The page does pulse from that animation. I wish there was a different way to display it.
the article is good….
This is a very informative post. I was searching such a good information, and your explanation is really very effective…..Thanks Keep the good job up……….:))
I am glad you could look past the jumpy page :)
If you set a fixed height for div#content, the jumping stops.
I couldn’t finish the article without opening up developer tools and fixing it myself. After that fix, very useful information.
Thanks Nathan, just fixed it. I don’t know how anyone read this article before.
[...] CSS3 is changing how we build websites. Javascript and images are being replaced with CSS3 animations and properties. What was once required to be an image is now built in CSS3. One of the best examples of this is the rounded corners on menus or buttons. The ability for CSS3 and cross browser compatibility is coming along slowly. No longer will we have to rely on so much JavaScript and images to create nice looking website elements such as buttons and menu navigations. You can build a rounded navigation menu, with no images and no Javascript, and effectively make use of the new CSS3 properties border-radius and animation. [...]