{"id":22004,"date":"2017-03-21T12:00:21","date_gmt":"2017-03-21T17:00:21","guid":{"rendered":"https:\/\/getflywheel.com\/?p=22004"},"modified":"2024-09-29T11:55:45","modified_gmt":"2024-09-29T16:55:45","slug":"css3-transitions-transforms-create-animations","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/","title":{"rendered":"How to Use CSS3 Transitions and Transforms to Create Animations"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Editor\u2019s note: This guest post was written by&nbsp;<a href=\"https:\/\/abbeyjfitzgerald.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Abbey Fitzgerald<\/a>&nbsp;a UX software engineer and web designer who loves the art of crafting code.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Interesting animations can be created with CSS3 by using transforms and transitions. Transforms are used to make an element change from one state to another. Examples would be rotating, moving, skewing, and scaling elements. Without a transition, an element being transformed would change abruptly from one state to another. To prevent this, a transition can be added so you can control the change, making it look smoother.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use CSS3\u00a0transforms<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are a few types of commonly used transforms. We\u2019ll focus on 2D examples for this tutorial, but it\u2019s good to be aware that cool things can be done with 3D transforms, too. (Once you\u2019ve mastered 2D, 3D will be much easier!)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the following examples, the original rectangles are in a darker green and the transformed are a bit more transparent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Translate<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>translate()<\/code> method moves an element from its current position to a new one.<br>With this code, the adjusted rectangle is moved 40 pixels to the right, and 100 pixels down from the current position.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-translate.png\" alt=\"css3-transitions-transforms-translate\" class=\"wp-image-22009\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\n -ms-transform: translate(40px, 100px); \/* IE 9 *\/\n    -webkit-transform: translate(40px, 100px); \/* Safari *\/\n    transform: translate(40px, 100px);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Rotate<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>rotate()<\/code> method rotates an element clockwise or counter-clockwise by the specified degree value. This code rotates the rectangle clockwise by 40 degrees.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-rotate.png\" alt=\"css3-transitions-transforms-rotate\" class=\"wp-image-22010\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>-ms-transform: rotate(40deg); \/* IE 9 *\/\n    -webkit-transform: rotate(40deg); \/* Safari *\/\n    transform: rotate(40deg);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Scale<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>scale()<\/code> method increases or decreases the size of an element (according to the parameters given for the width and height). In this example, the adjusted rectangle is two times larger than its original width and three times larger than its original height.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-scale.png\" alt=\"css3-transitions-transforms-scale\" class=\"wp-image-22011\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>-ms-transform: scale(2, 3); \/* IE 9 *\/\n    -webkit-transform: scale(2, 3); \/* Safari *\/\n    transform: scale(2, 3);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SkewX<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With <code>skewX()<\/code>, only the x-axis will be affected. This rectangle is skewed 30 degrees along the x-axis:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-skew-x.png\" alt=\"css3-transitions-transforms-skew-x\" class=\"wp-image-22012\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\n  -ms-transform: skewX(30deg); \/* IE 9 *\/\n    -webkit-transform: skewX(30deg); \/* Safari *\/\n    transform: skewX(30deg);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SkewY<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the same idea, but on the y-axis. The <code>skewY()<\/code> method skews an element along the y-axis by the defined angle. This rectangle is skewed 30 degrees along the y-axis.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-skew-y.png\" alt=\"css3-transitions-transforms-skew-y\" class=\"wp-image-22013\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\n-ms-transform: skewY(30deg); \/* IE 9 *\/\n    -webkit-transform: skewY(30deg); \/* Safari *\/\n    transform: skewY(30deg);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Skew<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to skew both the x and y-axis, this can be done in one place. The <code>skew()<\/code> method skews an element along the x and y-axis using the specified angles. The following example skews the rectangle element 30 degrees along the x-axis and 20 degrees along the x-axis.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-skew.png\" alt=\"css3-transitions-transforms-skew\" class=\"wp-image-22014\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\n-ms-transform: skew(30deg, 20deg); \/* IE 9 *\/\n    -webkit-transform: skew(30deg, 20deg); \/* Safari *\/\n    transform: skew(30deg, 20deg);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Matrix<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is where things get interesting, but also more efficient in the right situation. If you are doing a lot of transforms and don\u2019t want to write them out all individually, these 2D transforms can be combined with the <code>matrix()<\/code> method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s a basic outline to follow:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY())<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Just a heads up, it takes some research to come up with the values in the matrix. Here is a <a href=\"https:\/\/dev.opera.com\/articles\/understanding-the-css-transforms-matrix\/\" target=\"_blank\" rel=\"noopener noreferrer\">helpful guide<\/a> to get started.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-matrix.png\" alt=\"css3-transitions-transforms-matrix\" class=\"wp-image-22015\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To make this example, here are the matrix values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n  -ms-transform: matrix(2, -0.3, 0, 1, 0, 0); \/* IE 9 *\/\n    -webkit-transform: matrix(2, -0.3, 0, 1, 0, 0); \/* Safari *\/\n    transform: matrix(2, -0.3, 0, 1, 0, 0);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to use CSS3 transitions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that transforms have been covered, it\u2019s important to note that they are frequently used with transitions. This will make more sense in the following design examples.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s good to keep in mind that values can be customized to make the transition between two states of an element just how you would like. Think of it as a way to control animation speed when changing CSS properties. One example that you\u2019ve probably come across is if you hover over a button. It\u2019s common to see a \u201cslow darkening\u201d rather than just a quick instant darker color when you hover over it. This \u201cslow darkening\u201d was created with a transition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are specifying a lot of values, shorthand will come in handy. The <code>transition<\/code> CSS property is a shorthand property for <code>transition-property<\/code>, <code>transition-duration<\/code>, <code>transition-timing-function<\/code>, and <code>transition-delay<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Transition details<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>transition-property<\/code> specifies the CSS property where the transition will be applied, since you can apply a transition to an individual property. Examples of applying a transition to an individual property would be on a background or a transform. If you would like to target all the properties on the site, the transform property can be set to <code>all<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>transition-duration<\/code> is helpful to have the changes in a property to take place over a specified time rather than immediately. You\u2019ll see seconds and milliseconds as the possible values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>transition-duration: 7s;\ntransition-duration: 120ms;\ntransition-duration: 2s, 3s;\ntransition-duration: 10s, 30s, 230ms;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>transition-timing-function<\/code> CSS property lets you establish an acceleration curve, so that the speed of the transition can vary over its duration. There are many options to experiment with.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>transition-timing-function: linear|ease|ease-in|ease-out|ease-in-out|step-start|step-end|steps(int,start|end)|cubic-bezier(n,n,n,n)|initial|inherit;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>transition-delay<\/code> is pretty self-explanatory. A specified value sets the number of seconds or milliseconds to wait before the transition effect will start. <code>Initial<\/code> will set the property to its default value. If <code>inherit<\/code> is specified, this means that it inherits the property from its parent element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Here are all the properties:\ntransition-property: background; \/\/all\n  transition-duration: 1s;\n  transition-timing-function: linear; \/\/other options are ease\n  transition-delay: 0.5s;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the basic shorthand setup for a transition:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>div {\n  transition: &#91;property] &#91;duration] &#91;timing-function] &#91;delay];\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full shorthand sequence:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>div {\n  transition: background 1s linear 0.5s;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Animation planning<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before you create really detailed animations, it\u2019s good to take a step back before creating something totally crazy (especially if it will be in the public on the web). There\u2019s nothing wrong with adding some fun flair, but it\u2019s tempting to over animate. The movement you create should convey meaning and enhance the user experience, not distract from it. With that said, it\u2019s time to get creating!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I do want to call out that there are animated gifs in this tutorial to show the animations. The gifs are on repeat, which wouldn\u2019t typically be done for the designs. The purpose of having them on repeat is for demonstration purposes only.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Moving an image with the CSS transform property<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before we get into complicated transforms and transitions, let\u2019s talk about coordinates on an axis grid. (Heads up: This may bring back memories of graph paper from math coursework.) Coordinates are used to move the image.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">X and Y coordinates<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Things will look slightly differently than what you may expect. The -y value is above the x axis. HTML and CSS use what\u2019s called an \u201cinverted Cartesian coordinate system,\u201d because web pages start from top-left and read downwards.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-coordinate-basics.png\" alt=\"css3-transitions-transforms-coordinate-basics\" class=\"wp-image-22016\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Animation one: Basic transform with horizontal movement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We briefly touched base on translate() and how it can move an element. When put into practice, it can really float your boat, literally. As we learned above, The translate() method moves an element from its current position (according to the parameters given for the x-axis and the y-axis).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first project will be moving a submarine graphic. Submarines need to come up for air once in awhile, so we\u2019ll carefully style it with <code>translate()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To move it from its initial position using <code>transform: translate(x,y)<\/code>, two new values will have to be specified. To make the submarine move right and up, the x value should be positive and the y value should be negative. If the y value is set to 0, it would move only to the right and not up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Submarine going up for air with transform()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example we are going to move the object 200px to the right and 25px up. The syntax is <code>transform: translate(200px,-25px);<\/code> and the object will move on the hover of <code>.underwater<\/code>. By creating the styles for the movement in <code>.underwater:hover .move-right<\/code>, the action will happen on hover.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the starting HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp;amp;amp;lt;div class=&amp;quot;underwater&amp;quot;&amp;amp;amp;gt;\n  &amp;amp;amp;lt;div class=&amp;quot;object submarine move-right&amp;quot;&amp;amp;amp;gt;\n  \t&amp;amp;amp;lt;!-- image is set as a background image on submarine --&amp;amp;amp;gt;\n  &amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n&amp;amp;amp;lt;\/div&amp;amp;amp;gt;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-basic-right-move.gif\" alt=\"css3-transitions-transforms-basic-right-move\" class=\"wp-image-22017\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">See this on <a href=\"http:\/\/codepen.io\/abbeyjfitzgerald\/pen\/WpxVJW\" target=\"_blank\" rel=\"noopener noreferrer\">Codepen<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It really does not take a lot of CSS to make this basic animation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.underwater {\n  position: relative;\n  min-height: 600px;\n  background-color: #4fc3da;\n}\n\n.underwater:hover .move-right{\n    transform: translate( 200px ,-25px );\n    -webkit-transform: translate( 200px ,-25px ); \/** Chrome &amp;amp;amp;amp; Safari **\/\n    -ms-transform: translate( 200px ,-25px ); \/** Firefox **\/\n}\n\n.submarine {\n  height: 200px;\n  background: url(&amp;quot;little-submarine.svg&amp;quot;) no-repeat;\n}\n\n.object {\n  transition: all 2s ease-in-out;\n    -webkit-transition: all 2s ease-in-out; \/** Chrome &amp;amp;amp;amp; Safari **\/\n    -ms-transition: all 2s ease-in-out; \/** Opera **\/\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, our coordinates would fall in the top right square. On hover, the submarine moves up to the new coordinates because of the transform.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-coordinate-detail.png\" alt=\"css3-transitions-transforms-coordinate-detail\" class=\"wp-image-22018\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Things look smooth because of the transition. The <code>transition-duration<\/code> was set to <code>2sec<\/code>, which isn\u2019t too fast for this animation. The <code>transition-timing-function<\/code> was set to <code>ease-in-out<\/code> which gives it a slower start and end. If the duration time was increased to something larger, this would be very apparent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic animation two: Horizontal movement with keyframes and animation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Transform is being used a little differently in this example. Keyframes and the animation property will be utilized to create the next animation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Keyframe Basics<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Inside <code>@keyframes<\/code> is where you define the styles and stages for the animation. Here\u2019s the example we will be using, which will help give a \u201cfade in while moving downward\u201d effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@keyframes fadeInDown {\n  0% {\n    opacity: .8;\n    transform: translate(0, 0);\n  }\n\n  100% {\n    opacity: 1;\n    transform: translate(0, 30px);\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The transform values are incorporated into the keyframes. The original placement is set at 0%, and at 100%, the position will move down by 30px.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Animation basics<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To use CSS3 animation, you specify keyframes for the animation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Like designed above, <code>@keyframes<\/code> hold what styles the element will have at certain times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever you do this, make sure to give the animation a descriptive name. In this case, we\u2019re using <code>fadeOut<\/code>. Any class that includes the <code>fadeOut<\/code> will be applied. In the stages of the animation, the \u201cfrom\u201d is set to 0% and \u201cto\u201d is set to 100%. This example is pretty simple with just the two stages, but there can certainly be more steps added in between.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Specific actions with animation subproperties<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The animation property is used to call <code>@keyframes<\/code> inside a CSS selector. Animations can and will often have more than one subproperty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keyframes define what the animation will look like; subproperties define specific rules for the animation. Timing, duration, and other key details of how the animation sequence should progress are all including in subproperties.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are a few examples of animation subproperties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Animation-name: Name of the <code>@keyframesat-rule<\/code>, which describes the animation\u2019s keyframes. The name <code>fadeOut<\/code> in the previous example is an example of <code>animation-name<\/code>.<\/li>\n\n\n\n<li>Animation-duration: Length of time that an animation should take to complete one full cycle.<\/li>\n\n\n\n<li>Animation-timing-function: Timing of the animation, specifically how the animation transitions through keyframes. This function has the ability to establish acceleration curves. Examples are <code>linear<\/code>, <code>ease<\/code>, <code>ease-in<\/code>, <code>ease-out<\/code>, <code>ease-in-out<\/code>, or <code>cubic-bezier<\/code>.<\/li>\n\n\n\n<li>Animation-delay: Delay between the time the element is loaded and the beginning of the animation.<\/li>\n\n\n\n<li>Animation-iteration-count: Number of times the animation should repeat. Want the animation to go on forever? You can specify <code>infinite<\/code> to repeat the animation indefinitely.<\/li>\n\n\n\n<li>Animation-direction: This determines whether or not the animation should alternate direction on each run through the sequence or reset to the start point and repeat itself.<\/li>\n\n\n\n<li>Animation-fill-mode: Values that are applied by the animation both before and after it has executed.<\/li>\n\n\n\n<li>Animation-play-state: With this option, you are able to pause and resume the animation sequence. Examples are <code>none<\/code>, <code>forwards<\/code>, <code>backwards<\/code>, or <code>both<\/code>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>.fadeInDown {\n    -webkit-animation-name: fadeInDown;\n    animation-name: fadeInDown;\n    -webkit-animation-duration: 4s;\n    animation-duration: 4s;\n    -webkit-animation-timing-function: ease-in-out;\n    animation-timing-function: ease-in-out;\n    animation-iteration-count: 1;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what it would look like written in shorthand:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>animation: 4s ease-in-out 1 paused fadeInDown;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the HTML structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp;amp;amp;lt;div class=&amp;quot;underwater&amp;quot;&amp;amp;amp;gt;\n\n    &amp;amp;amp;lt;div class=&amp;quot;content-wrap fadeInDown&amp;quot;&amp;amp;amp;gt;\n\n    &amp;amp;amp;lt;div class=&amp;quot;submarine&amp;quot;&amp;amp;amp;gt;&amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n\n      &amp;amp;amp;lt;h2&amp;amp;amp;gt;Cute Submarine&amp;amp;amp;lt;\/h2&amp;amp;amp;gt;\n\n      &amp;amp;amp;lt;p&amp;amp;amp;gt;Text here.&amp;amp;amp;lt;\/p&amp;amp;amp;gt;\n\n    &amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n\n&amp;amp;amp;lt;\/div&amp;amp;amp;gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class of <code>fadeInDown<\/code> is making the submarine and content move up and down.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-submarine-push-text.gif\" alt=\"css3-transitions-transforms-submarine-push-text\" class=\"wp-image-22019\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Making the animation smoother by adjusting keyframes<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With a few more keyframes, we can make the animation much smoother.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@keyframes fadeInDown {\n  0% {\n    opacity: .8;\n    transform: translateY(5px);\n  }\n\n  25% {\n    opacity: .9;\n    transform: translateY(15px);\n  }\n\n  50% {\n    opacity: 1;\n    transform: translateY(30px);\n  }\n\n  75% {\n    opacity: 1;\n    transform: translateY(15px);\n  }\n\n  100% {\n    opacity: .9;\n    transform: translateY(0);\n  }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/submarine-push-text-timing-smooth-example.gif\" alt=\"submarine-push-text-timing-smooth-example\" class=\"wp-image-22020\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">See this on <a href=\"http:\/\/codepen.io\/abbeyjfitzgerald\/pen\/XMNBzq\" target=\"_blank\" rel=\"noopener noreferrer\">Codepen<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Adjusting timing of the animation<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Adding more keyframes helped smooth out the animation, but we can add a little more interactivity with more keyframes and a text delay on the div that contains all the text. It is a fun effect to to have the submarine bounce off the text, so having the delay line up with the movement of the submarine allows for that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The HTML will have the effective classes applied:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp;amp;amp;lt;div class=&amp;quot;underwater&amp;quot;&amp;amp;amp;gt;\n\n    &amp;amp;amp;lt;div class=&amp;quot;submarine move-down fadeInDown&amp;quot;&amp;amp;amp;gt; &amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n\n    &amp;amp;amp;lt;div class=&amp;quot;moving-content move-down text-delay fadeInDownText&amp;quot;&amp;amp;amp;gt;\n\n      &amp;amp;amp;lt;p&amp;amp;amp;gt;Text goes here.&amp;amp;amp;lt;\/p&amp;amp;amp;gt;\n\n    &amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n\n&amp;amp;amp;lt;\/div&amp;amp;amp;gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And here is the updated CSS that allows for the interactive animation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@keyframes fadeInDown {\n  0% {\n    opacity: .8;\n    transform: translateY(0);\n  }\n\n  25% {\n    opacity: 1;\n    transform: translateY(15px);\n  }\n\n  50% {\n    opacity: 1;\n    transform: translateY(30px);\n  }\n\n  75% {\n    opacity: 1;\n    transform: translateY(15px);\n  }\n\n  100% {\n    opacity: 1;\n    transform: translateY(0);\n  }\n}\n\n@keyframes fadeInDownText {\n  0% {\n    opacity: .8;\n    transform: translateY(0);\n  }\n\n  100% {\n    opacity: 1;\n    transform: translateY(35px);\n  }\n}\n\n.fadeInDown {\n    -webkit-animation-name: fadeInDown;\n    animation-name: fadeInDown;\n}\n\n.fadeInDownText {\n    -webkit-animation-name: fadeInDownText;\n    animation-name: fadeInDownText;\n}\n\n .move-down{\n    -webkit-animation-duration: 4s;\n    animation-duration: 4s;\n    -webkit-animation-timing-function: ease-in-out;\n    animation-timing-function: ease-in-out;\n    -webkit-animation-iteration-count: 1;\n    animation-iteration-count: 1\n}\n\n.text-delay {\n    -webkit-animation-delay: 2s; \/* Safari 4.0 - 8.0 *\/\n    animation-delay: 2s;\n    -webkit-animation-iteration-count: 1;\n    animation-iteration-count: 1;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/submarine-push-text-timing.gif\" alt=\"submarine-push-text-timing\" class=\"wp-image-22021\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">See this on <a href=\"http:\/\/codepen.io\/abbeyjfitzgerald\/pen\/vxXQMK\" target=\"_blank\" rel=\"noopener noreferrer\">Codepen<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cubic-bezier progress bar in CSS3 animation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s time to celebrate our animation progress by making a progress bar!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All the concepts we\u2019ve covered will come together to create something like this. A progress bar is a very common UI element, so creating something functional like this will help you see how other web elements can be animated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the starting HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp;amp;amp;lt;div class=&amp;quot;container&amp;quot;&amp;amp;amp;gt;\n  &amp;amp;amp;lt;div class=&amp;quot;row&amp;quot;&amp;amp;amp;gt;\n    &amp;amp;amp;lt;div class=&amp;quot;masthead&amp;quot;&amp;amp;amp;gt;\n      &amp;amp;amp;lt;p&amp;amp;amp;gt;CSS3 Loading Bar&amp;amp;amp;lt;\/p&amp;amp;amp;gt;\n    &amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n  &amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n &amp;amp;amp;lt;div class=&amp;quot;fast-loader&amp;quot;&amp;amp;amp;gt;&amp;amp;amp;lt;\/div&amp;amp;amp;gt;\n&amp;amp;amp;lt;\/div&amp;amp;amp;gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And the CSS to make it come to life:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@keyframes speedSetting {\n  0% { width: 0px; }\n  100% { width: 100%; }\n}\n\n@keyframes progressMotion {\n  0% { opacity: 1; }\n  50% {opacity: 1; }\n  100% { opacity: 0; }\n}\n\n.fast-loader {\n  width: 0px;\n  height: 10px;\n  background: linear-gradient(to left, blue,rgba(255,255,255,.0));\n  animation: speedSetting 2s cubic-bezier(1,.01,0,1) infinite, progressMotion 2s ease-out infinite;\n-webkit-animation: speedSetting 2s cubic-bezier(1,.01,0,1) infinite, progressMotion 2s ease-out infinite;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-loading-bar.gif\" alt=\"css3-transitions-transforms-loading-bar\" class=\"wp-image-22022\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the first set of keyframes, the width is going from 0 to 100% with a duration of two seconds. The keyframes in <code>progressMotion<\/code> are controlling the opacity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CSS bezier curve<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>cubic-bezier()<\/code> function can be used with the <code>transition-timing-function<\/code> property to control how a transition will change speed over its duration. You\u2019ll see this in the animation. See how it starts a little slower and then picks up the pace?<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2017\/03\/css3-transitions-transforms-bezier.jpg\" alt=\"css3-transitions-transforms-bezier\" class=\"wp-image-22023\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It is helpful to picture a bezier wrapper as a square. The lower left and the upper right are places where key points are, which are the P0 and P3. These are always set to (0,0) and (1,1), which don\u2019t change. However, P1 and P2 can be moved with the <code>cubic-bezier()<\/code> function if you specify new points with an x or y value.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>x1 is the x coordinate of control point p1<\/li>\n\n\n\n<li>y1 is the y coordinate of control point p1<\/li>\n\n\n\n<li>x2 is the x coordinate of control point p2<\/li>\n\n\n\n<li>y2 is the y coordinate of control point p2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some example values:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">x1 = .7<br>y1 = .16<br>x2 = .2<br>y2 = .9<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So an example might look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cubic-bezier(.7,.16,.2,.9)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The progress bar is a great example of how a bezier can customize the timing of an animation. With transforms, transitions, and many other options, custom animations can easily be created. Hopefully seeing these basic CSS3 animation examples has helped you see what other possibilities there are. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Looking for a <a href=\"https:\/\/wpengine.com\/blog\/a-smarter-way-to-wordpress\/\" target=\"_blank\" rel=\"noreferrer noopener\">smarter way to WordPress<\/a>? <a href=\"https:\/\/wpengine.com\/plans\/\" target=\"_blank\" rel=\"noreferrer noopener\">Find out more<\/a> about industry-leading <a href=\"https:\/\/wpengine.com\/wordpress-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">hosting for WordPress sites<\/a>, including award-winning 24\/7\/365 support at <a href=\"https:\/\/wpengine.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">WP Engine<\/a>.\u00a0\u00a0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Editor\u2019s note: This guest post was written by&nbsp;Abbey Fitzgerald&nbsp;a UX software engineer and web designer who loves the art of crafting code. Interesting animations can be created with CSS3 by using transforms and transitions. Transforms are used to make an element change from one state to another. Examples would be rotating, moving, skewing, and scaling<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":140698,"template":"","resource-topic":[1396],"resource-role":[1397,899],"resource-type":[916],"class_list":["post-22004","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Use CSS3 Transitions and Transforms to Create Animations<\/title>\n<meta name=\"description\" content=\"Learn how to create animations with CSS3 by using transforms and transitions, which are used to make an element change from one state to another.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use CSS3 Transitions and Transforms to Create Animations\" \/>\n<meta property=\"og:description\" content=\"Learn how to create animations with CSS3 by using transforms and transitions, which are used to make an element change from one state to another.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-29T16:55:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2017\/03\/css3-transitions-transforms-1100x500-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/\",\"name\":\"How to Use CSS3 Transitions and Transforms to Create Animations\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2017-03-21T17:00:21+00:00\",\"dateModified\":\"2024-09-29T16:55:45+00:00\",\"description\":\"Learn how to create animations with CSS3 by using transforms and transitions, which are used to make an element change from one state to another.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Use CSS3 Transitions and Transforms to Create Animations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use CSS3 Transitions and Transforms to Create Animations","description":"Learn how to create animations with CSS3 by using transforms and transitions, which are used to make an element change from one state to another.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"How to Use CSS3 Transitions and Transforms to Create Animations","og_description":"Learn how to create animations with CSS3 by using transforms and transitions, which are used to make an element change from one state to another.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-09-29T16:55:45+00:00","og_image":[{"width":1100,"height":500,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2017\/03\/css3-transitions-transforms-1100x500-1.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/","name":"How to Use CSS3 Transitions and Transforms to Create Animations","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2017-03-21T17:00:21+00:00","dateModified":"2024-09-29T16:55:45+00:00","description":"Learn how to create animations with CSS3 by using transforms and transitions, which are used to make an element change from one state to another.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/css3-transitions-transforms-create-animations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"How to Use CSS3 Transitions and Transforms to Create Animations"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2017\/03\/css3-transitions-transforms_343x245.png","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Designer, Site Owner","topic":"<strong>Topics:<\/strong> Design","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/22004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/140698"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=22004"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=22004"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=22004"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=22004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}