Exploring CSS3 Transitions and Animations with Examples – Part 1
With the birth of Cascading Style Sheets level 3, many new properties came into existence for the first time, out of which Animations and Transitions are the two that have been able to capture the attention of web designers and developers worldwide.
Before moving ahead with CSS3 Transitions and Animations, let’s define both of them to make you understand what they are actually.
CSS3 Transitions
Have you ever applied an effect on any of your web element to change its style from one to another? If yes, then that is what we call as Transition. And in CSS3 we have got a specific property to apply those effects and grab the web users’ attention. In simple words, CSS3 Transitions is a newly W3C recommended CSS3 property, which provides you a complete control over the speed of the effects applied on web elements.
For example let’s take a case of the menu bar. Generally, we see web designers apply a hover effect on the menu options to notify the users about the selected option. If you observe such an effect closely, you’ll notice that the effect gets applied instantly when a user hover his mouse over the given options. But with CSS3 Transitions in place, you can control the time that hover effect takes to apply.
CSS3 Animations
Being a web developer, you must have tried a lot of Flash animations animated images, and JavaScripts to bring an animated effect on your web elements. And that’s what CSS3 Animations is made for. Basically, Animations is a new CSS3 property which lets you apply animation effect on most of the HTML elements without using Flash, Animated Images or JavaScript.
The Progress
When W3C officially recommended both CSS3 Transitions and Animations, many browsers weren’t able to support that. But now as the time has progressed, almost all major web browsers, which include Google Chrome, Mozilla Firefox, Safari, Opera, IE 10, etc., have started to support these two awesome properties of CSS3. However, if your users view your site made using these two properties on previous versions of unsupported browsers they won’t be able to see the applied transitions and animations effect in place. That’s because unsupported browsers just ignore these codes completely. Therefore, you must make sure that your webpage doesn’t rely on these properties.
Exploring CSS3 Transitions
Like we told you above, transitions can only come into play when an element has a change or effect in state along with different styles specified for each state. You can differentiate the styles of your web elements using :hover, :active, :target and :focus pseudo classes.
Moreover, in total there are four transition properties that you can use, these includes transition-property, transition-duration, transition-timing-function, and transition-delay. Though you can use all the four transition properties to apply the effect on your HTML elements, but it’s not necessary always. Out of these four properties, developers use the first three more often.
Here’s a brief explanation of all four above mentioned properties.
- Transition Property: Here you need to specify the CSS property to which you want to apply the transition on.
- Transition Duration: With this property, you can set the time length that a transition takes. By default it is set to 0.
- Transition-Timing-Function: It defines how the speed while transition takes place is calculated. By default it calculates in ‘ease’ mode, under which the transition effect starts slowly in the beginning, then it gets fast, and at last ends at a slow speed.
- Transition-Delay: It specifies when the transition effect will start to take place. By default its value is set to 0.
Here’s a live example using the all four transition properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
.circle { width:100px; height:100px; border-radius:100px; background: #800080; transition-property:background; transition-duration:5s; transition-timing-function:linear; transition-delay:1s; } .circle:hover { background:#e03881; } |
With this code, the transition will take an effect on a circular shape having a width, height, and border-radius of 100 pixels each, with a background color code #800080, which will get transformed into different color code #e03881. The transition will get completed within 5 seconds, once you hover over the shape for at least a minimum of 6 seconds as the transition effect will start with a delay of 1 second.
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html> <head> <style> .circle { width:400px; height:400px; border-radius:400px; background:#800080; transition-property: background; transition-duration:5s; transition-timing-function:linear; transition-delay:2s; } .circle:hover { background: #e03881; } </style> </head> <body> <div class="circle"></div> <p>Hover over the div element above, to see the transition effect.</p> <p><b>Note:</b> The transition effect will wait 1 seconds before starting.</p> </body> </html> |
Vendor Prefixes
In the CSS3 code shown above, we didn’t use any vendor prefixes to make the transition effect browser compatible. We did this to present you the code in a simple and easy to understandable manner. But if you want the transition effect to work smoothly across all major browsers, namely; Google Chrome, Opera, Safari, and Mozilla Firefox, you must use the vendor prefixes given below.
Here’s the complete CSS3 Transitions code with all the required vendor prefixes for the example shown above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
.circle { width:100px; height:100px; border-radius:100px; background: #800080; transition-property:background; transition-duration:5s; transition-timing-function:linear; transition-delay:1s; /*****************************Vendor Prefixes Start*************************/ /* Safari & Chrome */ -webkit-transition-property:background; -webkit-transition-duration:5s; -webkit-transition-timing-function:linear; -webkit-transition-delay:1s; /* Mozilla Firefox */ -moz-transition-property:background; -moz-transition-duration:5s; -moz-transition-timing-function:linear; -moz-transition-delay:1s; /* Opera */ -o-transition-property:background; -o-transition-duration:5s; -o-transition-timing-function:linear; -o-transition-delay:1s; } /***************************Vendor Prefixes Ends***************************/ .circle:hover { background:#e03881; } |
So that is it for the first part of this CSS3 Transitions and Animations tutorial, we will now move to the second part, in which we will tell you more about the Transition Properties one by one, and all with the examples. Till then keep reading our other informative blog posts comprising of tutorials, reference guides, etc.CSS3 Transitions and Animations: With this tutorial, you will be able to understand the effect these two css3 properties can bring on your website.