{"id":4919,"date":"2021-06-21T02:00:00","date_gmt":"2021-06-21T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/css-transition-duration\/"},"modified":"2024-02-08T00:29:23","modified_gmt":"2024-02-07T23:29:23","slug":"css-transition-duration","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/css-transition-duration\/","title":{"rendered":"CSS Transition Duration &#8211; All you need to know"},"content":{"rendered":"\n<p>This is part one of a two-part complete guide to transitions. Here&#8217;s where we&#8217;ll get you started with the <strong>CSS transition duration and CSS transition properties.<\/strong><\/p>\n\n\n\n<p>This tutorial will get you up and running quickly, and you&#8217;ll be able to add all kinds of transitions to your websites and apps. Then in <a href=\"https:\/\/alvarotrigo.com\/blog\/css-transition-timing-function\/\">part two<\/a>, you&#8217;ll fine-tune your skills, and reach Jedi level.<\/p>\n\n\n\n<p>So read on, my padowan&#8230;<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-css-transition%3F\">What is a CSS Transition?<\/h2>\n\n\n<p>A CSS transition is the simplest way to create animations on your site. You can do some really cool stuff without a line of JavaScript, and they are really easy to learn.<\/p>\n\n\n\n<p>Many moons ago, if web developers wanted to animate their websites, all they really had were gifs and marquee text. Now we have CSS transitions &#8211; and all is well with the world.<\/p>\n\n\n\n<p>And what&#8217;s really awesome is, to get up and running you really only need to know about two properties &#8211; <strong>CSS transition-duration<\/strong> and <strong>CSS transition-property<\/strong>.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-css-transition-duration%3F\">What is CSS Transition Duration?<\/h2>\n\n\n<p><strong><code>transition-duration<\/code> is the CSS property that controls how long the transition from one state to another should take.<\/strong> So if you want to change the background color of an element, <code>transition-duration<\/code> will make that change happen gradually, fading smoothly from one color to the other.<\/p>\n\n\n\n<p>The default value for <code>transition-duration<\/code> is 0s &#8211; the element will just snap instantly from the first state to the second. So in a sense, <code>transition-duration<\/code> is the master transition property &#8211; without it, nothing works.<\/p>\n\n\n\n<p>For example, imagine you want to create a &#8220;Submit&#8221; button and make it change color when someone hovers on it. You could do that like this:<\/p>\n\n\n\n<p>First, create a <code>button<\/code> element&#8230;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span>&gt;<\/span>Submit<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then sprinkle in some CSS&#8230;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">3px<\/span> solid <span class=\"hljs-number\">#F4D06F<\/span>;\n  <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">10px<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100px<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">50px<\/span>;\n  <span class=\"hljs-attribute\">font-family<\/span>: sans-serif;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#392f5a<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#F4D06F<\/span>;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#F4D06F<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#392f5a<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you get this basic hover effect:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_JjyLgeN\" data-src=\"\/\/codepen.io\/anon\/embed\/JjyLgeN?height=450&amp;theme-id=1&amp;slug-hash=JjyLgeN&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed JjyLgeN\" title=\"CodePen Embed JjyLgeN\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>We changed the <code>background<\/code> and <code>color<\/code> properties using the <code>:hover<\/code> selector, so when you hover on the button these properties are applied. But as expected, the change happens <em>instantly<\/em>. It&#8217;s a little jarring.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-css-transition-duration\">How to use CSS Transition Duration<\/h2>\n\n\n<p>Creating the transition only takes one line of code, added to the CSS for the button. Its syntax is quite simple:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">transition-duration<\/span>: 1<span class=\"hljs-selector-tag\">s<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Which creates the following:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_PoKRMxy\" data-src=\"\/\/codepen.io\/anon\/embed\/PoKRMxy?height=450&amp;theme-id=1&amp;slug-hash=PoKRMxy&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed PoKRMxy\" title=\"CodePen Embed PoKRMxy\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Much nicer!<\/p>\n\n\n\n<p>You can make the transition take as long as you like, of course. You can use fractions of a second, like 0.5s, 0.1s, or milliseconds, e.g. 300ms, 500ms. So .25s and 250ms create the same result.<\/p>\n\n\n\n<p>Try editing the pen, and setting some different values: 0.1s, 0.5s, 3s&#8230; which do you think works best?<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"css-transition-duration-transitions-everything!\">CSS Transition-Duration Transitions everything!<\/h2>\n\n\n<p>One thing to be aware of &#8211; by default, <code>transition-duration<\/code> affects everything!<\/p>\n\n\n\n<p>Let&#8217;s get nuts and add a load more properties to our button&#8217;s <code>hover<\/code> state in our example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#F4D06F<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#392f5a<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">200px<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100px<\/span>;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">30px<\/span>;\n  <span class=\"hljs-attribute\">transform<\/span>: <span class=\"hljs-built_in\">rotate<\/span>(<span class=\"hljs-number\">360deg<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This will turn out like this:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_gOxeVjj\" data-src=\"\/\/codepen.io\/anon\/embed\/gOxeVjj?height=450&amp;theme-id=1&amp;slug-hash=gOxeVjj&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed gOxeVjj\" title=\"CodePen Embed gOxeVjj\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>No, no, no! That 1-second <code>transition-duration<\/code> applied to every property we changed. This is too much!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"https:\/\/alvarotrigo.com\/blog\/assets\/imgs\/2021-06-21\/obi-wan-css-transition.jpeg\" alt=\"CSS transition duration Meme\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n\n\n\n<p>We need to be more precise about which properties we transition to. The best way to do this is to limit how many properties we change (in this example, that would mean putting fewer properties in the <code>hover<\/code> selector).<\/p>\n\n\n\n<p>But what if you want to change some states instantly, and transition others gradually? Well, that&#8217;s where CSS <code>transition-property<\/code> comes in&#8230;<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"css-transition-property\">CSS Transition-Property<\/h2>\n\n\n<p><code>transition-property<\/code> enables you to control which CSS properties will be animated when you set a <code>transition-duration<\/code>.<\/p>\n\n\n\n<p>You just list the properties you want to transition, separated by commas. Here&#8217;s an example of the syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">  <span class=\"hljs-selector-tag\">transition-duration<\/span>: 1<span class=\"hljs-selector-tag\">s<\/span>;\n  <span class=\"hljs-selector-tag\">transition-property<\/span>: <span class=\"hljs-selector-tag\">background<\/span>, <span class=\"hljs-selector-tag\">color<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This will make the background and color transition in 1 second, but nothing else will be transitioned. That creates this result:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_QWMmeJX\" data-src=\"\/\/codepen.io\/anon\/embed\/QWMmeJX?height=450&amp;theme-id=1&amp;slug-hash=QWMmeJX&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed QWMmeJX\" title=\"CodePen Embed QWMmeJX\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Notice how the width, height, and font-size change instantly. And we don&#8217;t see the rotation at all &#8211; since we rotated 360 degrees, we ended up where we started.<\/p>\n\n\n\n<p>If you&#8217;re wondering exactly which properties you&#8217;re able to animate with CSS transition-property, Mozilla has a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_animated_properties\" target=\"_blank\" rel=\"noopener nofollow\">guide for that here<\/a>.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"using-different-transitions-durations\">Using different Transitions Durations<\/h3>\n\n\n<p>If you want to transition different properties at different speeds, you can do so. Take a look at this example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">  <span class=\"hljs-selector-tag\">transition-duration<\/span>: 1<span class=\"hljs-selector-tag\">s<\/span>, 2<span class=\"hljs-selector-tag\">s<\/span>;\n  <span class=\"hljs-selector-tag\">transition-property<\/span>: <span class=\"hljs-selector-tag\">background<\/span>, <span class=\"hljs-selector-tag\">color<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here the 1s will apply to <code>background<\/code>, and the 2s will apply to <code>color<\/code>.<\/p>\n\n\n\n<p>If you have more properties than durations, the browser will just repeat your duration values. So this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">  <span class=\"hljs-selector-tag\">transition-property<\/span>: <span class=\"hljs-selector-tag\">background<\/span>, <span class=\"hljs-selector-tag\">color<\/span>, <span class=\"hljs-selector-tag\">width<\/span>, <span class=\"hljs-selector-tag\">height<\/span>;\n  <span class=\"hljs-selector-tag\">transition-duration<\/span>: 1<span class=\"hljs-selector-tag\">s<\/span>, 2<span class=\"hljs-selector-tag\">s<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Is equivalent to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">  <span class=\"hljs-selector-tag\">transition-property<\/span>: <span class=\"hljs-selector-tag\">background<\/span>, <span class=\"hljs-selector-tag\">color<\/span>, <span class=\"hljs-selector-tag\">width<\/span>, <span class=\"hljs-selector-tag\">height<\/span>;\n  <span class=\"hljs-selector-tag\">transition-duration<\/span>: 1<span class=\"hljs-selector-tag\">s<\/span>, 2<span class=\"hljs-selector-tag\">s<\/span>, 1<span class=\"hljs-selector-tag\">s<\/span>, 2<span class=\"hljs-selector-tag\">s<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"combining-transition-duration-and-transition-property\">Combining <code>transition-duration<\/code> and <code>transition-property<\/code><\/h2>\n\n\n<p>The above code, although functional, is not incredibly readable, and it&#8217;s hard to maintain.<\/p>\n\n\n\n<p>For example, what if you didn&#8217;t want to transition <code>width<\/code> anymore?<\/p>\n\n\n\n<p>You&#8217;d have to delete <code>width<\/code> from <code>transition-property<\/code>, remembering that it was the third item in the list, and then delete the third duration from the <code>transition-duration<\/code> list. That&#8217;s a lot of work!<\/p>\n\n\n\n<p>A better way is to combine these into a single <code>transition<\/code> property:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">  <span class=\"hljs-selector-tag\">transition<\/span>: <span class=\"hljs-selector-tag\">background<\/span> 1<span class=\"hljs-selector-tag\">s<\/span>, <span class=\"hljs-selector-tag\">color<\/span> 2<span class=\"hljs-selector-tag\">s<\/span>, <span class=\"hljs-selector-tag\">width<\/span> 1<span class=\"hljs-selector-tag\">s<\/span>, <span class=\"hljs-selector-tag\">height<\/span> 2<span class=\"hljs-selector-tag\">s<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This does exactly the same thing, but it&#8217;s much easier to read and maintain.<\/p>\n\n\n\n<p>If you&#8217;re sure you&#8217;ve got all your state changes set up properly, you can use <code>all<\/code> with <code>transition<\/code>, to apply the duration to all properties:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">  <span class=\"hljs-selector-tag\">transition<\/span>: <span class=\"hljs-selector-tag\">all<\/span> 2<span class=\"hljs-selector-tag\">s<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-transition-duration\">Examples of <code>transition-duration<\/code><\/h2>\n\n\n<p>You can apply transitions to almost every CSS property, such as <code>scale<\/code>, <code>transform<\/code> (<code>scale<\/code>, <code>rotate<\/code>, <code>translate<\/code>, <code>translate3d<\/code>), <code>color<\/code>, <code>position<\/code>, <code>zoom<\/code> etc.<\/p>\n\n\n\n<p>Here are a few examples where we can see how to apply its syntax:<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"scale\">Scale<\/h3>\n\n\n<p>We can increase or decrease the size of an element by using the CSS property <code>transform<\/code>. <code>scale<\/code> is one of the types of transformation that we can apply to any element.<\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_QWqJZwz\" data-src=\"\/\/codepen.io\/anon\/embed\/QWqJZwz?height=450&amp;theme-id=1&amp;slug-hash=QWqJZwz&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed QWqJZwz\" title=\"CodePen Embed QWqJZwz\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Notice how we are scaling the button from <code>1<\/code> (default value) to <code>1.5<\/code> by using <code>transform: scale(1.5)<\/code> on the <code>button:hover<\/code> state.<\/p>\n\n\n\n<p>We are also using in this case <code>transition-property: transform;<\/code> for better performance. But if you don&#8217;t bother too much about it you can use <code>transition-property: all;<\/code>.<\/p>\n\n\n\n<p>The key parts in the CSS code are these:<\/p>\n\n\n\n<p>The most important part of the code is this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">transition-duration<\/span>: <span class=\"hljs-number\">1s<\/span>;\n  <span class=\"hljs-attribute\">transition-property<\/span>: transform;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">transform<\/span>: <span class=\"hljs-built_in\">scale<\/span>(<span class=\"hljs-number\">1.5<\/span>)\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h3 class=\"wp-block-heading\" id=\"rotate\">Rotate<\/h3>\n\n\n<p>In the same way, CSS <code>transform<\/code> allows us to transform the element by rotating it on the 2-dimensional plane.<\/p>\n\n\n\n<p>The <code>rotate<\/code> property uses <code>deg<\/code> units. For example <code>transform: rotate(45deg)<\/code>. We can use positive or negative values. So, <code>90deg<\/code> will rotate the element clockwise and <code>-90deg<\/code> to the opposite direction.<\/p>\n\n\n\n<p>Here&#8217;s a working example in where we&#8217;ll be rotating the button.<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_ExwOdWy\" data-src=\"\/\/codepen.io\/anon\/embed\/ExwOdWy?height=450&amp;theme-id=1&amp;slug-hash=ExwOdWy&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed ExwOdWy\" title=\"CodePen Embed ExwOdWy\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>The most important part of the code is this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">transition-duration<\/span>: <span class=\"hljs-number\">1s<\/span>;\n  <span class=\"hljs-attribute\">transition-property<\/span>: background, color;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">transform<\/span>: <span class=\"hljs-built_in\">rotate<\/span>(<span class=\"hljs-number\">360deg<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You could even go further here and use the <code>rotateY<\/code>, <code>rotateX<\/code>, or <code>rotateZ<\/code> functions.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"translate\">Translate<\/h3>\n\n\n<p>Translate, as its own name describes, provides us a way to translate or move the element on the two-dimensional plane.<\/p>\n\n\n\n<p>For exmaple <code>transform: translate(40px, 20px)<\/code> would move our element 10px on the X \/ horizontal axis and 20px on the Y \/ vertical axis.<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_xxXQyXg\" data-src=\"\/\/codepen.io\/anon\/embed\/xxXQyXg?height=450&amp;theme-id=1&amp;slug-hash=xxXQyXg&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed xxXQyXg\" title=\"CodePen Embed xxXQyXg\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>You can choose to only move the element on the X-axis or the Y-axis. In that case, you can use <code>0<\/code> for the other parameter. For example <code>transform: translate(40px, 0)<\/code> will only move the element <code>40px<\/code> horizontally.<\/p>\n\n\n\n<p>Negative values are also allowed.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"translate3d\">Translate3d<\/h3>\n\n\n<p>This transform function works in the same way as <code>translate<\/code> but it allows us to move elements on the <code>3d<\/code> plane.<\/p>\n\n\n\n<p>We can now move elements on the X, Y, and Z axis, where Z is the coordinate that will give us a sense of depth.<\/p>\n\n\n\n<p>You&#8217;ll understand this better with an example:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_GRMwYyB\" data-src=\"\/\/codepen.io\/anon\/embed\/GRMwYyB?height=450&amp;theme-id=1&amp;slug-hash=GRMwYyB&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed GRMwYyB\" title=\"CodePen Embed GRMwYyB\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>We&#8217;ve added the <code>perspective<\/code> function too to make this much more obvious:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span> {\n\n  <span class=\"hljs-attribute\">transition-duration<\/span>: <span class=\"hljs-number\">1s<\/span>;\n  <span class=\"hljs-attribute\">transition-property<\/span>: transform;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">transform<\/span>: <span class=\"hljs-built_in\">perspective<\/span>(<span class=\"hljs-number\">500px<\/span>) <span class=\"hljs-built_in\">translate3d<\/span>(<span class=\"hljs-number\">40px<\/span>, <span class=\"hljs-number\">20px<\/span>, -<span class=\"hljs-number\">800px<\/span>)\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h3 class=\"wp-block-heading\" id=\"skew\">Skew<\/h3>\n\n\n<p>The <code>skew<\/code> function provides us a way to <em>deform<\/em> or <em>tilt<\/em> the element in one or multiple directions.<\/p>\n\n\n\n<p>Its syntax uses the <code>deg<\/code> unit in the same way we do with <code>rotate<\/code>. For example <code>transform: skewX(-25deg);<\/code> will skew the element on the X-axis:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_VwMVEVx\" data-src=\"\/\/codepen.io\/anon\/embed\/VwMVEVx?height=450&amp;theme-id=1&amp;slug-hash=VwMVEVx&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed VwMVEVx\" title=\"CodePen Embed VwMVEVx\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">transform<\/span>: <span class=\"hljs-built_in\">skew<\/span>(-<span class=\"hljs-number\">25deg<\/span>)\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"the-next-step---advanced-css-transitions\">The Next Step &#8211; Advanced CSS Transitions<\/h2>\n\n\n<p>So, CSS <code>transition-duration<\/code> and <code>transition-property<\/code> are the two key elements to get started with transitions &#8211; you can do a lot with these two, so great work on getting this far!<\/p>\n\n\n\n<p>But you wouldn&#8217;t be reading a complete guide to CSS transitions if there wasn&#8217;t more to it!<\/p>\n\n\n\n<p>In fact, that <code>transition<\/code> property actually combines two other CSS transition properties too &#8211; and you&#8217;ll master these in part 2: <a href=\"https:\/\/alvarotrigo.com\/blog\/css-transition-timing-function\/\">CSS <code>transition-timing-function<\/code> and <code>transition-delay<\/code><\/a> where we explain how to use the linear<code>, <\/code>ease<code>, <\/code>ease-in<code>, <\/code>ease-out<code>and<\/code>ease-in-out` timing functions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"https:\/\/alvarotrigo.com\/blog\/assets\/imgs\/2021-06-21\/complete-duration-css-transition-meme.jpeg\" alt=\"Meme - CSS Transition Duration Complete\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n\n\n\n<p>In the meantime, how about some inspiration &#8211; have you seen the transitions and animations that you can use in <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">fullPage.js<\/a>?<\/p>\n\n\n\n<p>For example, how about the <a href=\"https:\/\/alvarotrigo.com\/fullPage\/extensions\/water-effect.html#firstPage\">water effect<\/a>? Or the <a href=\"https:\/\/alvarotrigo.com\/fullPage\/extensions\/drop-effect.html#firstPage\">drop effect<\/a>? fullPage.js is an all-singing, all-dancing JS library that lets you make amazing full page sites, with tonnes of transitions you can use to move from page to page.<\/p>\n\n\n\n<p>It&#8217;s also really easy to set up and use &#8211; so give it a try, see what you think!<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"related-articles\">Related articles<\/h2>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/css-page-transitions\/\">Beutiful CSS Page Transitions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/hamburger-menu-css\/\">Hamburger Menu CSS animations and transitions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/css-text-animations\/\">Amazing CSS text animations<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>How can you use CSS transition duration and transition property? Find it out here where we explain how they work in a simple to understand tutorial guide for beginners and full of examples.<\/p>\n","protected":false},"author":6,"featured_media":4918,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[106],"tags":[9,67],"class_list":["post-4919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","tag-css","tag-web-design"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=4919"}],"version-history":[{"count":4,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4919\/revisions"}],"predecessor-version":[{"id":9579,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4919\/revisions\/9579"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/4918"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=4919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=4919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=4919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}