CSS Transforms, Transitions, and Animations
Transforms
The transform property applies a 2D or 3D transformation to an element. Transforms are triggered when an element changes states, such as on mouse-hover or mouse-click. The examples in this post will demonstrate transforms on mouse-hover.
Transform Syntax
- matrix: Defines a 2D transformation, using a matrix of six values
- matrix3d Defines a 3D transformation, using a 4x4 matrix of 16 values
- translate(x,y) Defines a 2D translation
- translate3d(x,y,z) Defines a 3D translation
- translateX(x) Defines a translation, using only the value for the X-axis
- translateY(y) Defines a translation, using only the value for the Y-axis
- translateZ(z) Defines a 3D translation, using only the value for the Z-axis
- scale(x,y) Defines a 2D scale transformation
- scale3d(x,y,z) Defines a 3D scale transformation
- scaleX(x) Defines a scale transformation by giving a value for the X-axis
- scaleY(y) Defines a scale transformation by giving a value for the Y-axis
- scaleZ(z) Defines a 3D scale transformation by giving a value for the Z-axis
- rotate(angle) Defines a 2D rotation, the angle is specified in the parameter
- rotate3d(x,y,z,angle) Defines a 3D rotation
- rotateX(angle) Defines a 3D rotation along the X-axis
- rotateY(angle) Defines a 3D rotation along the Y-axis
- rotateZ(angle) Defines a 3D rotation along the Z-axis
- skew(x-angle,y-angle) Defines a 2D skew transformation along the X- and the Y-axis
- skewX(angle) Defines a 2D skew transformation along the X-axis
- skewY(angle) Defines a 2D skew transformation along the Y-axis
- perspective(n) Defines a perspective view for a 3D transformed element
- initial Sets this property to its default value. Read about initial
- inherit Inherits this property from its parent element. Read about inherit
By understanding the transform property, you’ll unlock ways to further build unique and engaging interfaces from scratch. So, in this post, I’ll show you everything you need to know to start using the CSS transform property, including the many ways you can manipulate elements in two and three dimensions, and even how to animate such effects.
Transitions
CSS transitions allows you to change property values smoothly, over a given duration.
To create a transition effect, you must specify two things:
- the CSS property you want to add an effect to.
- the duration of the effect.
The transition-timing-function property specifies the speed curve of the transition effect.
The transition-timing-function property can have the following values:
- ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default)
- linear - specifies a transition effect with the same speed from start to end
- ease-in - specifies a transition effect with a slow start
- ease-out - specifies a transition effect with a slow end
- ease-in-out - specifies a transition effect with a slow start and end
Transitions are the grease in the wheel of CSS transforms. Without a transition, an element being transformed would change abruptly from one state to another. By applying a transition you can control the change, making it smooth and gradual.
Animations
An animation lets an element gradually change from one style to another.
The @keyframes
When you specify CSS styles inside the @keyframes, the animation will change from the current style to the new style at certain times. To get an animation to work, you must bind the animation to an element.
The animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles.
The animation-direction property can have the following values:
- normal - The animation is played as normal (forwards). This is default.
- reverse - The animation is played in reverse direction (backwards).
- alternate - The animation is played forwards first, then backwards.
- alternate-reverse - The animation is played backwards first, then forwards.
The animation-fill-mode property specifies a style for the target element when the animation is not playing (before it starts, after it ends, or both).
The animation-fill-mode property can have the following values:
-
none - Default value. Animation will not apply any styles to the element before or after it is executing
- forwards - The element will retain the style values that is set by the last keyframe (depends on animation-direction and animation-iteration-count)
- backwards - The element will get the style values that is set by the first keyframe (depends on animation-direction), and retain this during the animation-delay period
- both - The animation will follow the rules for both forwards and backwards, extending the animation properties in both directions.
Get back to EMAM’S HOMEPAGE
I have created this page as a part of my project using Github, Please visit my profile, I will be more than happy to hear from you all. © Emam Shararah 2021