Exploring CSS3 Transitions and Animations with Examples – Part 2
In the first part of this tutorial on CSS3 Transitions and Animations, we told you about what Transitions and Animations are in CSS3. And we also covered the CSS3 Transitions with an example including all of its properties. But in this second part of the tutorial, we will explore all Transition properties one by one with which your all doubts regarding CSS3 Transitions would get cleared.
So let’s begin with the first property of CSS3 Transitions, and as we move on further we will explain each and every CSS3 Transitions property with demo. Apart from this, we will also provide you with a list of all transitional properties that you can define inside transition-property.
Transition-property
The first transitional property i.e., transition-property is the one that defines the properties that will be transformed in combination with all other transitional properties. However, by default all the transitional properties of an element get altered when the transition effect takes place. But, the value of the properties that will be affected by the transitions can only be defined using the transition-property.
In our previous tutorial, we only defined the background as the value in the transition-property. And that’s because only the background gets changed while hovering a mouse over the shape. However, if you add any other property in an element but doesn’t define it within the transition-property value, you won’t see that particular transitions effect come into play.
Moreover, there might be occasions when you will need to execute multiple properties using CSS3 transition-property. In that case, the values can be separated with a comma. Additionally, you can also keep ‘all’ as the value if you want the transitions effect to take place on all the properties of an element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
.triangle { width: 0px; height: 0px; border-style: solid; border-width: 150px 75px 0 75px; border-color: #5fb832 transparent transparent transparent; transition-property: background, border radius transition-duration: 5s; transition-timing-function: linear; transition-delay: .2s; } .triangle:hover { border-radius: 150px; border-color:#565657 transparent transparent transparent; background: #f08c57; } |
Here you can see for the transition-property value, we have kept the background, and border-radius. With this, when you’ll hover over the given triangle shape, it will get transformed into a circle with two different background; one the triangle background as a triangle has been formed using border-color, and the actual background, which only comes while hovering the mouse.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html> <head> <style> .triangle { width: 0px; height: 0px; border-style: solid; border-width: 150px 75px 0 75px; border-color: #5fb832 transparent transparent transparent; transition-property:background, border radius transition-duration:5s; transition-timing-function:linear; transition-delay:.2s; -webkit-transition-property:background, border radius; -webkit-transition-duration:5s; -webkit-transition-timing-function:linear; -webkit-transition-delay:.2s; -moz-transition-property:background, border radius; -moz-transition-duration:5s; -moz-transition-timing-function:linear; -moz-transition-delay:.2s; -o-transition-property:background, border radius; -o-transition-duration:5s; -o-transition-timing-function:linear; -o-transition-delay:.2s; } .triangle:hover { border-radius: 150px; border-color:#565657 transparent transparent transparent; background: #f08c57; } </style> </head> <body> <div class="triangle"></div> </body> </html> |
Transitional Properties
As we promised in the beginning, here’s a list of transitional properties that you can use to apply the transitional effects on an element. In addition to this, you must remember that not all properties can be transitioned. Instead, properties having an identifiable halfway point, font-sizes, colors, etc. can be transitioned.
background-color | font-size | min-height |
background-position | font-weight | min-width |
border-color, border-width | height, left | opacity, outline-color |
border-spacing | letter-spacing | outline-offset |
bottom | line-height | outline-width |
clip | margin | padding |
color | max-height | right |
crop | max-width | text-indent |
vertical-align | width | text-shadow |
visibility | z-index | top |
word-spacing | —– | —- |
Transition-duration
Transition-duration is the time a transition takes to complete. You can set its value using the general time measuring units, including milliseconds (ms) and seconds (s). The value can also be kept in fractional measurements, for example; .2s.
Moreover, if you want to implement the transition effect completely on your element where multiple properties are to be involved, you can define multiple time durations, one for each included property. Just like you separate the property value of the transition-property, you can define and separate the multiple durations using a comma. However, the order of the defined values when recognizing the involved individual properties and durations matters a lot. For example; the first recognized property within the transition-property will apply the transitional effects as per the first time period defined within the transition-duration property.
If multiple properties are being transitioned with only one duration value declared, that one value will be the duration of all the transitioned properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
.triangle { width: 0px; height: 0px; border-style: solid; border-radius: 00px; background: green; border-width: 150px 75px 0 75px; border-color: #5fb832 transparent transparent transparent; transition-property: background, border radius; border-color: purple; transition-duration:.1s, 5s; transition-timing-function:linear; transition-delay: .1s, 2s;= } |
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 33 |
<!DOCTYPE html> <html> <head> <style> .triangle { width: 0px; height: 0px; border-style: solid; border-radius: 00px; background: green; border-width: 150px 75px 0 75px; border-color: #5fb832 transparent transparent transparent; transition-property: background, border radius; border-color: purple; transition-duration:.1s, 5s; transition-timing-function:linear; transition-delay: .1s, 2s; } .triangle:hover { background: #f08c57; border-color:#565657 transparent transparent transparent; border-radius: 150px; } </style> </head> <body> <div class="triangle"></div> </body> </html> |
Transition-timing-function
This transitional property is used to define the speed curve in which a transition will progress. Once you set the duration using the transition-duration property for a transitional effect, it can have multiple speeds within a single duration. Some of the commonly used values for the transition-timing-function property are; linear, ease, ease-in, ease-out, and ease-in-out.
The ‘linear’ value demonstrates a transitional effect moving ahead at a steady speed from a particular state to a new one. The ‘ease-in’ value reflects a transitional effect that progresses slowly in the beginning and soon speeds up and maintain the same till the time transition effect gets completed. The ‘ease-out’ value works exactly the opposite of the ease-in value. When you keep the ease-out value, the transition starts at a great speed but slows down for the entire transition duration. At last but not the least, the ‘ease-in-out’ value identifies a transition that begins slowly, speeds up in the middle, and gets slow at the end.
This property works in a cubic-bezier function, for which the values can also be set specifically. You can keep its value in numeric form. For example; x1, y1, x2, y2. The other values include step-start, step-stop, and a uniquely identified steps (number_of_steps, direction) value.
For transitioning multiple properties at a time, you can define multiple transitional timing functions. And like other transitional property values, these timing function values, can also be defined and separated using a comma.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
.triangle { width: 0px; height: 0px; border-style: solid; border-radius: 00px; background: green; border-width: 0px 75px 150px 75px; border-color: transparent transparent #5fb832 transparent; transition-property: background, border radius; border-color: cream; transition-duration:.1s, 5s, 7s ; transition-timing-function:linear, ease-in, ease-out; transition-delay: .1s, 2s, 4s; } |
The 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 33 |
<!DOCTYPE html> <html> <head> <style> .triangle { width: 0px; height: 0px; border-style: solid; border-radius: 00px; background: green; border-width: 0px 75px 150px 75px; border-color: transparent transparent #5fb832 transparent; transition-property: background, border radius; border-color: cream; transition-duration:.1s, 5s, 7s ; transition-timing-function:linear, ease-in, ease-out; transition-delay: .1s, 2s, 4s; } .triangle:hover { background: #f08c57; border-color:#565657 transparent transparent transparent; border-radius: 150px; } </style> </head> <body> <div class="triangle"></div> </body> </html> |
Transition-delay
If you want the transition effect to begin with a little delay, the transition-delay property will come in handy. The value for the delay can be set in milliseconds or seconds, which will inform the browser how long the set transition effect should be held up before executing it completely. Here also you can set the delay for multiple transitions, for which the declared values can separated via comma.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
.triangle { width: 0px; height: 0px; border-style: solid; border-radius: 00px; background: #437480; border-width: 50px 0px 50px 150px; border-color: transparent transparent transparent #b57bba; transition-property: background, border radius; transition-duration:5s; transition-timing-function:linear, ease-out, ease-in; transition-delay: 5s; } |
The 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 33 |
<!DOCTYPE html> <html> <head> <style> .triangle { width: 0px; height: 0px; border-style: solid; border-radius: 00px; background: #437480; border-width: 50px 0px 50px 150px; border-color: transparent transparent transparent #b57bba; transition-property: background, border radius; transition-duration:5s; transition-timing-function:linear, ease-out, ease-in; transition-delay: 5s; } .triangle:hover { background: #f08c57; border-color:#565657 transparent transparent transparent ; border-radius: 50px; } </style> </head> <body> <div class="triangle"></div> </body> </html> |