<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ transitions - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ transitions - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 24 Jun 2026 22:47:30 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/transitions/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ CSS Transition vs Animation Handbook – How to Animate Elements in CSS ]]>
                </title>
                <description>
                    <![CDATA[ CSS transitions and animations provide smooth, gradual ways to change an element's style. But they work in different ways. Here are the main distinctions between them:                         ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-transition-vs-css-animation-handbook/</link>
                <guid isPermaLink="false">66ba0dea79b7f411df58de98</guid>
                
                    <category>
                        <![CDATA[ animation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ handbook ]]>
                    </category>
                
                    <category>
                        <![CDATA[ transitions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Oluwatobi Sofela ]]>
                </dc:creator>
                <pubDate>Tue, 12 Sep 2023 00:20:36 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/09/How-to-Animate-Elements-in-CSS-Cover.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>CSS transitions and animations provide smooth, gradual ways to change an element's style. But they work in different ways.</p>
<p>Here are the main distinctions between them:</p>
<table>
    <thead>
        <tr>
            <th>CSS Transition</th>
            <th>CSS Animation</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <ul>
                    <li>
                        Creates smooth transitions from one CSS value to another.
                    </li>
                    <li>
                        You need triggers to run CSS transitions. For instance, you can use the <code>:hover</code> <a href="https://codesweetly.com/css-pseudo-selectors">pseudo-class</a> to run transitions when a user's pointer hovers over an element.
                    </li>
                    <li>
                        Transition has only two states: an initial and a final state. You cannot create intermediate steps.
                    </li>
                    <li>Runs only once.</li>
                    <li>Best used for basic style changes.</li>
                </ul>
            </td>
            <td>
                <ul>
                    <li>
                        Animates the style change from one CSS keyframe to another.
                    </li>
                    <li>CSS animations do not need triggers.</li>
                    <li>Animation allows you to create multiple states.</li>
                    <li>
                        You can run multiple animation iterations—even to infinity.
                    </li>
                    <li>Best used for dynamic style changes.</li>
                </ul>
            </td>
        </tr>
    </tbody>
</table>

<p>This handbook uses examples to explain the two animating techniques so that you can understand their similarities and differences.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><a class="post-section-overview" href="#heading-what-are-css-transitions">What Are CSS Transitions?</a></li>
<li><a class="post-section-overview" href="#heading-categories-of-css-transition-properties">Categories of CSS Transition Properties</a><ul>
<li><a class="post-section-overview" href="#heading-what-are-the-required-css-transition-properties">What Are the Required CSS Transition Properties?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-transition-property">What is the CSS <code>transition-property</code>?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-transition-duration-property">What is the CSS <code>transition-duration</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-examples-of-the-required-css-transition-properties">Examples of the Required CSS Transition Properties</a></li>
<li><a class="post-section-overview" href="#heading-what-are-the-optional-css-transition-properties">What Are the Optional CSS Transition Properties?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-transition-timing-function-property">What is the CSS <code>transition-timing-function</code> Property?</a></li>
<li><a class="post-section-overview" href="#what-is-a-css-transition-delay-property">What is the CSS <code>transition-delay</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-examples-of-the-optional-css-transition-properties">Examples of the Optional CSS Transition Properties</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-shorthand-for-defining-the-css-transition-properties">Shorthand for Defining the CSS Transition Properties</a></li>
<li><a class="post-section-overview" href="#heading-what-is-css-animation">What is CSS Animation?</a><ul>
<li><a class="post-section-overview" href="#heading-components-of-css-animations">Components of CSS Animations</a></li>
<li><a class="post-section-overview" href="#heading-what-are-css-keyframes">What are CSS @keyframes?</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-what-are-css-animation-properties">What Are CSS Animation Properties?</a><ul>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-name-property">What is the CSS <code>animation-name</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-duration-property">What is the CSS <code>animation-duration</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-timing-function-property">What is the CSS <code>animation-timing-function</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-delay-property">What is the CSS <code>animation-delay</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-iteration-count-property">What is the CSS <code>animation-iteration-count</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-direction-property">What is the CSS <code>animation-direction</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-play-state-property">What is the CSS <code>animation-play-state</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-css-animation-fill-mode-property">What is the CSS <code>animation-fill-mode</code> Property?</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#what-is-a-css-animation-property">What is the CSS <code>animation</code> Property?</a></li>
<li><a class="post-section-overview" href="#heading-important-stuff-to-know-about-css-transitions-and-animations">Important Stuff to Know about CSS Transitions and Animations</a></li>
<li><a class="post-section-overview" href="#heading-wrapping-up">Wrapping up</a></li>
</ol>
<p>Without further ado, let's discuss CSS transitions.</p>
<h2 id="heading-what-are-css-transitions">What Are CSS Transitions?</h2>
<p><strong>CSS transitions</strong> provide a smooth and gradual way to change a specific CSS property's value.</p>
<p>So, instead of allowing browsers to change a property's value immediately, CSS transitions cause the change to happen smoothly over a specified period of time.</p>
<p>For instance, suppose you wish to change an element's size on hover. In that case, you have two options:</p>
<ol>
<li>Implement the change without CSS transitions.</li>
<li>Use CSS transitions to transition smoothly from the element's initial size to its new state.</li>
</ol>
<p>Let's see some examples of the two options.</p>
<h3 id="heading-how-to-change-an-images-size-on-mouse-hover-without-using-css-transitions">How to change an image's size on mouse hover without using CSS transitions</h3>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-dsymqf"><strong>Try Editing It</strong></a></p>
<p>The code above instantaneously changes the image's size from its initial width (<code>40%</code>) to its new dimension (<code>100%</code>) because we did not use CSS transitions.</p>
<p>With CSS transitions, you will get a much more pleasing experience. Let's see an example below.</p>
<h3 id="heading-how-to-change-an-images-size-on-mouse-hover-with-css-transitions">How to change an image's size on mouse hover with CSS transitions</h3>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
  <span class="hljs-attribute">transition</span>: width <span class="hljs-number">3s</span> ease-out <span class="hljs-number">0.4s</span>;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-ufwgbu"><strong>Try Editing It</strong></a></p>
<p>The <code>transition</code> property applied a smooth and gradual transition from <code>width: 40%</code> to <code>width: 100%</code> on the image.</p>
<h2 id="heading-categories-of-css-transition-properties">Categories of CSS Transition Properties</h2>
<p>We have two categories of CSS transition properties:</p>
<ul>
<li><em>Required</em> transition properties</li>
<li><em>Optional</em> transition properties</li>
</ul>
<p>Let's discuss the two.</p>
<h3 id="heading-what-are-the-required-css-transition-properties">What Are the Required CSS Transition Properties?</h3>
<p>The two required properties you need to create CSS transitions are:</p>
<ul>
<li><code>transition-property</code></li>
<li><code>transition-duration</code></li>
</ul>
<h4 id="heading-what-is-the-css-transition-property">What is the CSS transition-property?</h4>
<p>The CSS <code>transition-property</code> specifies the CSS property you wish to transition from its initial to its new state.</p>
<h4 id="heading-what-is-the-css-transition-duration-property">What is the CSS transition-duration Property?</h4>
<p>The CSS <code>transition-duration</code> property defines the length of time in which browsers should complete the selected element's transition. In other words, <code>transition-duration</code> sets the total start-to-finish time.</p>
<p><strong>Note the following:</strong></p>
<ul>
<li>The <code>transition-duration</code> property only accepts time in milliseconds (ms) or seconds (s).</li>
<li>Zero seconds (<code>0s</code>) is the <code>transition-duration</code>'s default value. Therefore, no <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Element/transitionend_event">transition event</a> will occur if you do not define a <code>transition-duration</code> property.</li>
<li><code>transition-duration</code> accepts only a zero (<code>0</code>) or a positive numeric value. Browsers will ignore the duration declaration if you provide anything else.</li>
</ul>
<h3 id="heading-examples-of-the-required-css-transition-properties">Examples of the Required CSS Transition Properties</h3>
<p>Below are some examples of the two required CSS transition properties.</p>
<h4 id="heading-how-to-transition-an-elements-width-within-three-seconds">How to transition an element's width within three seconds</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
  <span class="hljs-attribute">transition-property</span>: width;
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">3s</span>;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-cq27rd"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>transition-property</code> tells browsers to transition the <code>img</code>'s <code>width</code> from its initial value (<code>40%</code>) to its new state (<code>100%</code>).</li>
<li>We used the <code>transition-duration</code> property to define three seconds (<code>3s</code>) duration from the start to the end of the transition.</li>
</ol>
<p>Therefore, instead of an instantaneous change from the image's initial width (<code>40%</code>) to its new size (<code>100%</code>), browsers will transition smoothly between the old and new state in three seconds (<code>3s</code>).</p>
<h4 id="heading-how-to-transition-a-fonts-size-within-five-seconds">How to transition a font's size within five seconds</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
  <span class="hljs-attribute">transition-property</span>: font-size;
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">5s</span>;
}

<span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">7rem</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-huvkzp"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>transition-property</code> informs browsers to transition the <code>p</code> element's <code>font-size</code>.</li>
<li>We used the <code>transition-duration</code> property to define a five-second (<code>5s</code>) duration from the start to the end of the transition.</li>
</ol>
<p>Therefore, instead of an immediate change from the paragraph's initial font size (<code>1rem</code>) to its new size (<code>7rem</code>), browsers will transition smoothly between the old and new state in five seconds (<code>5s</code>).</p>
<p>Let's now discuss the optional CSS transition properties.</p>
<h3 id="heading-what-are-the-optional-css-transition-properties">What Are the Optional CSS Transition Properties?</h3>
<p>The two optional CSS transition properties are:</p>
<ul>
<li><code>transition-timing-function</code></li>
<li><code>transition-delay</code></li>
</ul>
<h4 id="heading-what-is-the-css-transition-timing-function-property">What is the CSS transition-timing-function Property?</h4>
<p>The CSS <code>transition-timing-function</code> property defines the implementation timing (speed) of the selected element's transition.</p>
<p>In other words, the <code>transition-timing-function</code> specifies the speed for implementing the transition at various intervals of its duration.</p>
<p>The values the <code>transition-timing-function</code> property accepts are:</p>
<ul>
<li><code>ease</code>: Starts the transition slowly. Then, speeds it up and ends it slowly. <code>ease</code> is the <code>transition-timing-function</code> property's default value. It is equivalent to <code>cubic-bezier(0.25,0.1,0.25,1)</code>.</li>
<li><code>ease-in</code>: Starts the transition slowly with a gradual increase in speed. It is equal to <code>cubic-bezier(0.42,0,1,1)</code>.</li>
<li><code>ease-out</code>: Starts fast and ends the transition slowly. It is equivalent to <code>cubic-bezier(0,0,0.58,1)</code>.</li>
<li><code>ease-in-out</code>: Starts and ends the transition slowly. It is equal to <code>cubic-bezier(0.42,0,0.58,1)</code>.</li>
<li><code>linear</code>: Starts and ends the transition using a consistent speed throughout the transition's duration. It is equivalent to <code>cubic-bezier(0,0,1,1)</code>.</li>
<li><code>cubic-bezier(p1, p2, p3, p4)</code>: Allows you to define the values of the <a target="_blank" href="https://www.cssportal.com/css-cubic-bezier-generator/">cubic-bezier curve</a>.</li>
</ul>
<h3 id="heading-what-is-the-css-transition-delay-property">What is the CSS transition-delay Property?</h3>
<p>The CSS <code>transition-delay</code> property defines how long the browser should wait before it starts the transition.</p>
<p><strong>Note the following:</strong></p>
<ul>
<li>The <code>transition-delay</code> property must be in milliseconds (ms) or seconds (s).</li>
<li><code>0s</code> is the <code>transition-delay</code>'s default value. It causes the transition to start immediately from the moment browsers apply it to an HTML element.</li>
<li>A negative value causes the transition to begin immediately from the specified time. For instance, suppose an element's <code>transition-delay</code> value is <code>-3s</code>. In that case, the transition will start immediately at 3 seconds.</li>
<li>A positive value causes the transition to begin after the specified delay time has elapsed. For instance, suppose an element's <code>transition-delay</code> value is <code>3s</code>. In that case, the transition will start after a 3-second delay.</li>
</ul>
<h3 id="heading-examples-of-the-optional-css-transition-properties">Examples of the Optional CSS Transition Properties</h3>
<p>Below are some examples of the two optional CSS transition properties.</p>
<h4 id="heading-how-to-transition-an-elements-width-with-an-ease-out-speed">How to transition an element's width with an ease-out speed</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
  <span class="hljs-attribute">transition-property</span>: width;
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">3s</span>;
  <span class="hljs-attribute">transition-timing-function</span>: ease-out;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-tqzgmf"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>transition-property</code> informs browsers to transition the <code>img</code> element's width.</li>
<li>We used the <code>transition-duration</code> property to define three seconds (<code>3s</code>) duration from the start to the end of the transition.</li>
<li>We used the <code>transition-timing-function</code> property to begin the transition quickly and end it slowly.</li>
</ol>
<h4 id="heading-how-to-transition-an-elements-width-with-a-linear-speed">How to transition an element's width with a linear speed</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
  <span class="hljs-attribute">transition-property</span>: width;
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">3s</span>;
  <span class="hljs-attribute">transition-timing-function</span>: linear;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-1gqwai"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>transition-property</code> informs browsers to transition the <code>img</code> element's width.</li>
<li>We used the <code>transition-duration</code> property to define three seconds (<code>3s</code>) duration from the start to the end of the transition.</li>
<li>The <code>transition-timing-function</code> property tells browsers to transition from the element's initial width to its new size using a consistent transition speed throughout.</li>
</ol>
<h4 id="heading-how-to-transition-an-elements-width-with-a-two-second-delay">How to transition an element's width with a two-second delay</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
  <span class="hljs-attribute">transition-property</span>: width;
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">3s</span>;
  <span class="hljs-attribute">transition-timing-function</span>: linear;
  <span class="hljs-attribute">transition-delay</span>: <span class="hljs-number">2s</span>;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-ejjufi"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>transition-property</code> informs browsers to transition the <code>img</code> element's <code>width</code> property.</li>
<li>We used the <code>transition-duration</code> property to define three seconds (<code>3s</code>) duration from the start to the end of the transition.</li>
<li>The <code>transition-timing-function</code> property transitions the <code>img</code> element's width using a consistent transition speed.</li>
<li>We used the <code>transition-delay</code> property to apply a two-second (<code>2s</code>) delay to the starting time of the transition.</li>
</ol>
<p>Now that we know the CSS transition properties, we can discuss defining them with a shorthand syntax.</p>
<h2 id="heading-shorthand-for-defining-the-css-transition-properties">Shorthand for Defining the CSS Transition Properties</h2>
<p>We use the <code>transition</code> property as shorthand for the <code>transition-property</code>, <code>transition-duration</code>, <code>transition-timing-function</code>, and <code>transition-delay</code> properties.</p>
<p>In other words, instead of writing:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">transition-property</span>: width;
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">3s</span>;
  <span class="hljs-attribute">transition-timing-function</span>: linear;
  <span class="hljs-attribute">transition-delay</span>: <span class="hljs-number">2s</span>;
}
</code></pre>
<p>You can alternatively use the <code>transition</code> property to shorten your code like so:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">transition</span>: width <span class="hljs-number">3s</span> linear <span class="hljs-number">2s</span>;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-vtvbpo"><strong>Try Editing It</strong></a></p>
<p>Here is the <code>transition</code> property's syntax:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">transition</span>: &lt;<span class="hljs-selector-tag">property-name</span>&gt; &lt;<span class="hljs-selector-tag">duration</span>&gt; &lt;<span class="hljs-selector-tag">timing-function</span>&gt; &lt;<span class="hljs-selector-tag">delay</span>&gt;;
</code></pre>
<p>Note that you can use the <code>transition</code> property to transition the state of multiple CSS properties.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0.4</span>;
  <span class="hljs-attribute">transition</span>: width <span class="hljs-number">3s</span> linear, opacity <span class="hljs-number">4s</span> ease-out, transform <span class="hljs-number">5s</span>;
}

<span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">1</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">rotate</span>(<span class="hljs-number">45deg</span>);
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-transitions/js-ajygzm"><strong>Try Editing It</strong></a></p>
<p>The snippet above used commas (<code>,</code>) to separate each of the transitional properties we are applying to the <code>img</code> element.</p>
<p>So, now that we know what CSS transitions are and how they work, we can discuss CSS animations.</p>
<h2 id="heading-what-is-css-animation">What is CSS Animation?</h2>
<p><strong>CSS animations</strong> provide a smooth and gradual way to animate an element's style changes from one keyframe to another.</p>
<h3 id="heading-components-of-css-animations">Components of CSS Animations</h3>
<p>CSS animations consist of two components:</p>
<ol>
<li>Keyframes</li>
<li>Animation properties</li>
</ol>
<h3 id="heading-what-are-css-keyframes">What are CSS @keyframes?</h3>
<p><strong>@keyframes</strong> define the styles you want browsers to apply smoothly to an element at some specified key moments (frames).</p>
<h3 id="heading-syntax-of-css-keyframes">Syntax of CSS @keyframes</h3>
<p>A CSS @keyframes <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule">at-rule</a> consists of the following:</p>
<ol>
<li>An <code>@keyframes</code> keyword</li>
<li>The <code>@keyframes</code>' name</li>
<li>A block of zero or more keyframes</li>
<li>The animation's key moment selector</li>
<li>The key moment's style declarations</li>
</ol>
<p><strong>Here's an illustration:</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/css-animation-keyframes-illustration-codesweetly.png" alt="Anatomy of CSS @keyframes at-rule" width="600" height="400" loading="lazy">
<em>A CSS @keyframes at-rule consists of a keyword, a name, and a block of keyframes</em></p>
<h3 id="heading-examples-of-css-keyframes">Examples of CSS @keyframes</h3>
<p>Below are examples of the CSS @keyframes.</p>
<h4 id="heading-how-to-define-change-colors-keyframes">How to define <code>change-color</code>'s keyframes</h4>
<pre><code class="lang-css"><span class="hljs-keyword">@keyframes</span> change-color {
  <span class="hljs-comment">/* The first keyframe */</span>
  0% {<span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-comment">/* The last keyframe */</span>
  100% {<span class="hljs-attribute">background-color</span>: yellow;}
}
</code></pre>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>We created the <code>@keyframes</code> at-rule named <code>change-color</code>.</li>
<li>We defined a keyframe for browsers to apply when an associated element's animation is at its zero percent (<code>0%</code>) duration.</li>
<li>We defined a keyframe for browsers to apply when an associated element's animation is at its one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p><strong>Note:</strong></p>
<ul>
<li>You can name your <code>@keyframes</code> anything you wish.</li>
<li><code>0%</code> is equivalent to the keyword <code>from</code>. And <code>100%</code> is the same as the keyword <code>to</code>. In other words, the snippet above is equal to the following:</li>
</ul>
<pre><code class="lang-css"><span class="hljs-keyword">@keyframes</span> change-color {
  <span class="hljs-comment">/* The first keyframe */</span>
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">background-color</span>: purple;} 
  <span class="hljs-comment">/* The last keyframe */</span>
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">background-color</span>: yellow;} 
}
</code></pre>
<ul>
<li>An animation's start and end states (<code>from</code> and <code>to</code>) are optional.</li>
<li>Suppose you omit an <code>@keyframes</code>' start (<code>0%</code>) or end (<code>100%</code>) state. In that case, browsers will default to the element's existing styles for either state.</li>
<li>The important rule (<code>!important</code>) does not work in keyframes. Browsers will ignore any keyframe declaration with an <code>!important</code> rule.</li>
</ul>
<p>Let's see another example.</p>
<h4 id="heading-how-to-define-shape-images-keyframes">How to define <code>shape-image</code>'s keyframes</h4>
<pre><code class="lang-css"><span class="hljs-keyword">@keyframes</span> shape-image {
  0% { <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid blue;}
  40% { <span class="hljs-attribute">width</span>: <span class="hljs-number">70%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid red; <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;}
  75% { <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">30px</span> solid green;}
  100% { <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">7px</span> solid purple;}
}
</code></pre>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>We created the <code>@keyframes</code> <a target="_blank" href="https://codesweetly.com/css-ruleset">ruleset</a> named <code>shape-image</code>.</li>
<li>We defined four keyframes for browsers to apply when an associated element's animation is at the specified key moments.</li>
</ol>
<p><strong>Tip:</strong> Use the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/CSSKeyframesRule">CSSKeyframesRule</a> interface in JavaScript to access the CSS <code>@keyframes</code> at-rules.</p>
<p>So, now that we know the CSS @keyframes ruleset, we can discuss the other component of CSS animations—<em>animation properties</em>.</p>
<h2 id="heading-what-are-css-animation-properties">What Are CSS Animation Properties?</h2>
<p><strong>CSS animation properties</strong> inform browsers about the animation you wish to apply to a specific element.</p>
<p>In other words, CSS animation properties describe the animation's attributes, such as its name, duration, direction, and iteration.</p>
<p>The nine (9) types of CSS animation properties are:</p>
<ul>
<li><code>animation-name</code></li>
<li><code>animation-duration</code></li>
<li><code>animation-timing-function</code></li>
<li><code>animation-delay</code></li>
<li><code>animation-iteration-count</code></li>
<li><code>animation-direction</code></li>
<li><code>animation-play-state</code></li>
<li><code>animation-fill-mode</code></li>
<li><code>animation</code></li>
</ul>
<p>Let's discuss each one now.</p>
<h3 id="heading-what-is-the-css-animation-name-property">What is the CSS <code>animation-name</code> property?</h3>
<p>The CSS <code>animation-name</code> property defines the name of the <code>@keyframes</code> at-rules containing the styles you wish to apply to a specific element.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">animation-name</span>: change-color;
}

<span class="hljs-keyword">@keyframes</span> change-color {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">background-color</span>: yellow;}
}
</code></pre>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>We created <code>change-color</code>'s <code>@keyframes</code> ruleset.</li>
<li>We defined two keyframes for browsers to use when the <code>div</code> element's animation is at its zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p><strong>Tip:</strong> You can use the keyword <code>none</code> to deactivate an animation.</p>
<h3 id="heading-what-is-the-css-animation-duration-property">What is the CSS <code>animation-duration</code> property?</h3>
<p>The CSS <code>animation-duration</code> property defines the time to complete one animation cycle.</p>
<p><strong>Note the following:</strong></p>
<ul>
<li>The <code>animation-duration</code> property must be in milliseconds (ms) or seconds (s) units.</li>
<li><code>animation-duration</code>'s value must be zero or positive. Otherwise, browsers will ignore the duration declaration.</li>
<li>Zero seconds (<code>0s</code>) is <code>animation-duration</code>'s default value.</li>
<li>Suppose <code>0s</code> is <code>animation-duration</code>'s value. In that case, browsers will still execute the animation by firing the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Element/animationstart_event"><code>animationStart</code></a> and <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Element/animationend_event"><code>animationEnd</code></a> events. But the <a target="_blank" href="https://codesweetly.com/css-animations-explained#what-is-an-animation-fill-mode-property-in-css"><code>animation-fill-mode</code></a>'s value will determine whether the animation is visible. For instance, if you set the <code>animation-fill-mode</code> to <code>none</code>, the animation will be invisible.</li>
</ul>
<p>Let's see some examples of the <code>animation-duration</code> property.</p>
<h4 id="heading-how-to-animate-an-elements-color-change-within-three-seconds">How to animate an element's color change within three seconds</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">animation-name</span>: change-color;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">3s</span>;
}

<span class="hljs-keyword">@keyframes</span> change-color {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">background-color</span>: yellow;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-h6mb4k"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to three seconds (<code>3s</code>).</li>
<li>We created <code>change-color</code>'s @keyframes <a target="_blank" href="https://codesweetly.com/css-ruleset">ruleset</a>.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Therefore, browsers will create a smooth three-second animation from <code>change-color</code>'s first keyframe to its last.</p>
<h4 id="heading-how-to-animate-an-images-border-and-width-changes-within-seven-seconds">How to animate an image's border and width changes within seven seconds</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">animation-name</span>: shape-image;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">7s</span>;
}

<span class="hljs-keyword">@keyframes</span> shape-image {
  0% { <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid blue;}
  40% { <span class="hljs-attribute">width</span>: <span class="hljs-number">70%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid red; <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;}
  75% { <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">30px</span> solid green;}
  100% { <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">7px</span> solid purple;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-prumgo"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>img</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to seven seconds (<code>7s</code>).</li>
<li>We created <code>shape-image</code>'s @keyframes ruleset.</li>
<li>We defined four keyframes for browsers to apply when the image's animation is at the specified key moments.</li>
</ol>
<p>Therefore, browsers will create a smooth seven-second animation from <code>shape-image</code>'s first keyframe to its last.</p>
<h3 id="heading-what-is-the-css-animation-timing-function-property">What is the CSS <code>animation-timing-function</code> property?</h3>
<p>The CSS <code>animation-timing-function</code> property defines an animation's implementation timing (speed) throughout its duration.</p>
<p>In other words, the <code>animation-timing-function</code> property specifies the speed for implementing the animation at various intervals of its duration.</p>
<p>The values the <code>animation-timing-function</code> property accepts are:</p>
<ul>
<li><code>ease</code>: Starts the animation slowly. Then speeds it up and ends it slowly. <code>ease</code> is the <code>animation-timing-function</code> property's default value. It is equivalent to <code>cubic-bezier(0.25, 0.1, 0.25, 1)</code>.</li>
<li><code>ease-in</code>: Starts the animation slowly with a gradual increase in speed. It is equivalent to <code>cubic-bezier(0.42, 0, 1, 1)</code>.</li>
<li><code>ease-out</code>: Starts fast and ends the animation slowly. It is equivalent to <code>cubic-bezier(0, 0, 0.58, 1)</code>.</li>
<li><code>ease-in-out</code>: Starts and ends the animation slowly. It is equivalent to <code>cubic-bezier(0.42, 0, 0.58, 1)</code>.</li>
<li><code>linear</code>: Starts and ends the animation using a consistent speed throughout the animation's duration. It is equivalent to <code>cubic-bezier(0, 0, 1, 1)</code>.</li>
<li><code>cubic-bezier(p1, p2, p3, p4)</code>: Allows you to define the values of the <a target="_blank" href="https://www.cssportal.com/css-cubic-bezier-generator/">cubic-Bezier curve</a>.</li>
</ul>
<p>Let's see some examples of the <code>animation-timing-function</code> property.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-using-a-linear-speed">How to animate an element's width change using a linear speed</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">7s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: linear;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-tzwrdc"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to seven seconds (<code>7s</code>).</li>
<li>The <code>linear</code> timing function applied a consistent speed to the <code>div</code>'s animation.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code>'s animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Therefore, browsers will create a smooth seven-second animation from <code>change-width</code>'s first keyframe to its last.</p>
<p>Let's see another example.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-using-an-ease-in-out-and-a-linear-speed">How to animate an element's width change using an ease-in-out and a linear speed</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">7s</span>;
}

<span class="hljs-selector-class">.first-div</span> {
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
}

<span class="hljs-selector-class">.second-div</span> {
  <span class="hljs-attribute">background-color</span>: orange;
  <span class="hljs-attribute">animation-timing-function</span>: linear;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-janmqa"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to seven seconds (<code>7s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>first-div</code>'s animation.</li>
<li>The <code>linear</code> timing function applied a consistent speed to the <code>second-div</code>'s animation.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> elements' animations are at their zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) durations.</li>
</ol>
<p>Therefore, browsers will create a smooth seven-second animation from <code>change-width</code>'s first keyframe to its last.</p>
<h3 id="heading-what-is-the-css-animation-delay-property">What is the CSS <code>animation-delay</code> property?</h3>
<p>The CSS <code>animation-delay</code> property defines how long browsers should wait before starting an animation.</p>
<p>In other words, use <code>animation-delay</code> to specify whether the animation should start immediately from the beginning, immediately from a specific time, or later (after some delay).</p>
<p><strong>Note the following:</strong></p>
<ul>
<li>The <code>animation-delay</code> property must be in milliseconds (ms) or seconds (s) units.</li>
<li><code>0s</code> is <code>animation-delay</code>'s default value. It causes the animation to start once browsers apply it to an HTML element.</li>
<li>A negative value causes the animation to start immediately from the specified time. For instance, suppose an element's <code>animation-delay</code> value is <code>-3s</code>. In that case, the animation will begin immediately at 3 seconds.</li>
<li>A positive value causes the animation to start after the specified delay time has elapsed. For instance, suppose an element's <code>animation-delay</code> value is <code>3s</code>. In that case, the animation will begin after a 3-second delay.</li>
</ul>
<p>Let's see some examples of the <code>animation-delay</code> property.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-with-a-four-second-delay">How to animate an element's width change with a four-second delay</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">7s</span>;
}

<span class="hljs-selector-class">.first-div</span> {
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
}

<span class="hljs-selector-class">.second-div</span> {
  <span class="hljs-attribute">background-color</span>: orange;
  <span class="hljs-attribute">animation-timing-function</span>: linear;
  <span class="hljs-attribute">animation-delay</span>: <span class="hljs-number">4s</span>;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-iidpmk"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> elements.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to seven seconds (<code>7s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>first-div</code>'s animation.</li>
<li>The <code>linear</code> timing function applied a consistent speed to the <code>second-div</code>'s animation.</li>
<li>The <code>animation-delay</code> property applied a four-second (<code>4s</code>) delay to the starting time of the <code>second-div</code>'s animation.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> elements' animations are at their zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) durations.</li>
</ol>
<p>Therefore, browsers will delay the <code>second-div</code>'s animation for four seconds while starting the <code>first-div</code>'s animation immediately.</p>
<p>Below is another example of the <code>animation-delay</code> property.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-from-the-fourth-second-mark-of-the-animation-sequence">How to animate an element's width change from the fourth-second mark of the animation sequence</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">7s</span>;
}

<span class="hljs-selector-class">.first-div</span> {
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
}

<span class="hljs-selector-class">.second-div</span> {
  <span class="hljs-attribute">background-color</span>: orange;
  <span class="hljs-attribute">animation-timing-function</span>: linear;
  <span class="hljs-attribute">animation-delay</span>: -<span class="hljs-number">4s</span>;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-6xga4t"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> elements.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to seven seconds (<code>7s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>first-div</code>'s animation.</li>
<li>The <code>linear</code> timing function applied a consistent speed to the <code>second-div</code>'s animation.</li>
<li>The <code>animation-delay</code> property makes the <code>second-div</code>'s animation start from the fourth-second mark of the animation sequence.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> elements' animations are at their zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) durations.</li>
</ol>
<p>Therefore, browsers will start the <code>second-div</code>'s animation immediately at the fourth-second mark.</p>
<h3 id="heading-what-is-the-css-animation-iteration-count-property">What is the CSS <code>animation-iteration-count</code> property?</h3>
<p>The CSS <code>animation-iteration-count</code> property defines the number of times browsers should repeat an animation.</p>
<p><strong>Note the following:</strong></p>
<ul>
<li><code>1</code> is <code>animation-iteration-count</code>'s default value.</li>
<li>The <code>animation-iteration-count</code> property accepts non-integer values—for instance, <code>0.5</code> tells browsers to play half of a single animation cycle.</li>
<li><code>animation-iteration-count</code> does <em>not</em> accept negative values.</li>
<li>An <code>infinite</code> value means browsers should repeat the animation forever.</li>
</ul>
<p>Below are some examples.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-with-a-two-cycle-iteration">How to animate an element's width change with a two-cycle iteration</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-iteration-count</span>: <span class="hljs-number">2</span>;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-vbcswe"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-iteration-count</code> property tells browsers to run the animation twice.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Therefore, browsers will run the <code>div</code>'s animation in two cycles.</p>
<p>Below is another example of the <code>animation-iteration-count</code> property.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-with-an-infinite-iteration">How to animate an element's width change with an infinite iteration</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-iteration-count</span>: infinite;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-p1zwk5"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-iteration-count</code> property tells browsers to run the animation infinitely.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at its zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Therefore, browsers will run the <code>div</code>'s animation infinitely.</p>
<h3 id="heading-what-is-the-css-animation-direction-property">What is the CSS <code>animation-direction</code> property?</h3>
<p>The CSS <code>animation-direction</code> property specifies whether the animation's first iteration should run forward or backward. It also defines whether browsers should alternate the direction of subsequent iterations.</p>
<p>The values <code>animation-direction</code> accepts are:</p>
<ul>
<li><code>normal</code>: Play the animation in the normal direction (that is, forward). <code>normal</code> is <code>animation-direction</code>'s default value.</li>
<li><code>reverse</code>: Play the animation in the reverse direction (backward).</li>
<li><code>alternate</code>: Play the first animation cycle in the normal direction. Then, alternates the subsequent iterations between the backward and forward directions.</li>
<li><code>alternate-reverse</code>: Play the first animation cycle in the reverse direction. Then, alternates the subsequent iterations between the forward and backward directions.</li>
</ul>
<p>Below are some examples.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-while-starting-each-animation-cycle-backward">How to animate an element's width change while starting each animation cycle backward</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-iteration-count</span>: infinite;
  <span class="hljs-attribute">animation-direction</span>: reverse;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-d2n3zt"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-iteration-count</code> property tells browsers to run the animation infinitely.</li>
<li>The <code>animation-direction</code> property starts each animation cycle in reverse (backward).</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Below is another example of the <code>animation-direction</code> property.</p>
<h4 id="heading-how-to-animate-an-elements-width-change-while-alternating-the-animations-direction">How to animate an element's width change while alternating the animation's direction</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-iteration-count</span>: infinite;
  <span class="hljs-attribute">animation-direction</span>: alternate;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-ld9kms"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-iteration-count</code> property tells browsers to run the animation infinitely.</li>
<li>The <code>animation-direction</code> property alternates the direction of each cycle's animation.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<h3 id="heading-what-is-the-css-animation-play-state-property">What is the CSS <code>animation-play-state</code> property?</h3>
<p>The CSS <code>animation-play-state</code> property specifies whether the browser is running or has paused a specific animation.</p>
<p>The values the <code>animation-play-state</code> property accepts are:</p>
<ul>
<li><code>running</code>: Specifies that the browser is running the animation. <code>running</code> is <code>animation-play-state</code>'s default value.</li>
<li><code>paused</code>: Specifies that the browser has paused the animation.</li>
</ul>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-iteration-count</span>: infinite;
  <span class="hljs-attribute">animation-direction</span>: alternate;
}

<span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">animation-play-state</span>: paused;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-kbommn"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-iteration-count</code> property tells browsers to run the animation infinitely.</li>
<li>The <code>animation-direction</code> property alternates the direction of each cycle's animation.</li>
<li>We used the <code>animation-play-state</code> on the <code>div</code>'s hover <a target="_blank" href="https://codesweetly.com/css-pseudo-selectors">pseudo-class</a> to pause the animation whenever users move their mouse over the <code>div</code> element.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<h3 id="heading-what-is-the-css-animation-fill-mode-property">What is the CSS <code>animation-fill-mode</code> property?</h3>
<p>The CSS <code>animation-fill-mode</code> property defines the styles browsers should apply to an element before (or after) its animation runs.</p>
<p>The values the <code>animation-fill-mode</code> property accepts are:</p>
<ul>
<li><code>none</code>: Browsers will apply <em>no</em> style to the element before or after the animation runs. <code>none</code> is <code>animation-fill-mode</code>'s default value.</li>
<li><code>forwards</code>: The element will retain the last keyframe's style declarations when the animation ends. (Note: The <code>animation-direction</code> and <code>animation-iteration-count</code> properties determine the last keyframe.)</li>
<li><code>backwards</code>: The element will retain the first keyframe's style declarations during the <code>animation-delay</code> period. (Note: The <code>animation-direction</code> property determines the first keyframe.)</li>
<li><code>both</code>: Browsers will apply both the forwards and backwards rules. Therefore, the animation properties will extend in both directions.</li>
</ul>
<p>Below are some examples.</p>
<h4 id="heading-how-to-style-an-element-after-its-animation-ends">How to style an element after its animation ends</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">background-color</span>: green;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-fill-mode</span>: forwards;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-lkc7mw"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-fill-mode</code> property tells browsers to retain the last keyframe's style declarations when the animation ends.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Below is another example of the <code>animation-fill-mode</code> property.</p>
<h4 id="heading-how-to-style-an-element-during-its-animation-delay-period">How to style an element during its animation delay period</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">background-color</span>: green;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-delay</span>: <span class="hljs-number">3s</span>;
  <span class="hljs-attribute">animation-fill-mode</span>: backwards;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-nfmq3r"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-fill-mode</code> property tells browsers to retain the first keyframe's style declarations during the <code>animation-delay</code> period.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to apply when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<p>Let's see a third example of the <code>animation-fill-mode</code> property.</p>
<h4 id="heading-how-to-style-an-element-during-its-animation-delay-and-after-the-animation">How to style an element during its animation delay and after the animation</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">background-color</span>: green;
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-delay</span>: <span class="hljs-number">3s</span>;
  <span class="hljs-attribute">animation-fill-mode</span>: both;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-gbypmt"><strong>Try Editing It</strong></a></p>
<p>Here's what we did in the snippet above:</p>
<ol>
<li>The <code>animation-name</code> property specifies the <code>@keyframes</code> we wish to apply to the <code>div</code> element.</li>
<li>The <code>animation-duration</code> property sets the animation's runtime for one cycle to five seconds (<code>5s</code>).</li>
<li>We used the <code>ease-in-out</code> timing function to apply a slow start and slow end speed to the <code>div</code>'s animation.</li>
<li>The <code>animation-fill-mode</code> property tells browsers to apply both the forwards and backwards rules.</li>
<li>We created <code>change-width</code>'s @keyframes ruleset.</li>
<li>We defined two keyframes for browsers to use when the <code>div</code> element's animation is at zero percent (<code>0%</code>) and one hundred percent (<code>100%</code>) duration.</li>
</ol>
<h2 id="heading-what-is-the-css-animation-property">What is the CSS <code>animation</code> Property?</h2>
<p>We use the <code>animation</code> property as a shorthand for:</p>
<ul>
<li><code>animation-name</code></li>
<li><code>animation-duration</code></li>
<li><code>animation-timing-function</code></li>
<li><code>animation-delay</code></li>
<li><code>animation-iteration-count</code></li>
<li><code>animation-direction</code></li>
<li><code>animation-play-state</code></li>
<li><code>animation-fill-mode</code></li>
</ul>
<p>In other words, instead of writing:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">animation-name</span>: change-width;
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
  <span class="hljs-attribute">animation-timing-function</span>: ease-in-out;
  <span class="hljs-attribute">animation-delay</span>: <span class="hljs-number">2s</span>;
  <span class="hljs-attribute">animation-iteration-count</span>: <span class="hljs-number">3</span>;
  <span class="hljs-attribute">animation-direction</span>: alternate;
  <span class="hljs-attribute">animation-play-state</span>: running;
  <span class="hljs-attribute">animation-fill-mode</span>: both;
}
</code></pre>
<p>You can alternatively use the <code>animation</code> property to shorten your code like so:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">animation</span>: <span class="hljs-number">5s</span> ease-in-out <span class="hljs-number">2s</span> <span class="hljs-number">3</span> alternate both running change-width;
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-37ccew"><strong>Try Editing It</strong></a></p>
<p>Here is the <code>animation</code> property's syntax:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">animation</span>: <span class="hljs-selector-tag">animation-duration</span> <span class="hljs-selector-tag">animation-timing-function</span> <span class="hljs-selector-tag">animation-delay</span> <span class="hljs-selector-tag">animation-iteration-count</span> <span class="hljs-selector-tag">animation-direction</span> <span class="hljs-selector-tag">animation-fill-mode</span> <span class="hljs-selector-tag">animation-play-state</span> <span class="hljs-selector-tag">animation-name</span>;
</code></pre>
<p><strong>Note:</strong></p>
<ul>
<li>The way you arrange the time values is essential. Browsers read the first time-value as <code>animation-duration</code>. And they assign the second one to <code>animation-delay</code>.</li>
<li>It is best to list <code>animation-name</code> last. Otherwise, browsers may assign the <code>animation-name</code>'s value to other properties.</li>
<li>You can apply multiple <code>@keyframes</code> <a target="_blank" href="https://codesweetly.com/css-ruleset">rulesets</a> to an element using the <code>animation</code> property. Here's an example:</li>
</ul>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">background-color</span>: green;
  <span class="hljs-attribute">animation</span>: 
    <span class="hljs-number">5s</span> ease-in-out <span class="hljs-number">3s</span> <span class="hljs-number">3</span> alternate both change-width, 
    <span class="hljs-number">5s</span> <span class="hljs-number">3s</span> infinite alternate both change-shape, 
    <span class="hljs-number">5s</span> <span class="hljs-number">3s</span> infinite rotate-hue;
}

<span class="hljs-keyword">@keyframes</span> change-width {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">70px</span>; <span class="hljs-attribute">background-color</span>: purple;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">background-color</span>: orange;}
}

<span class="hljs-keyword">@keyframes</span> change-shape {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">border-radius</span>: <span class="hljs-number">0%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid blue;}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>; <span class="hljs-attribute">border</span>: <span class="hljs-number">7px</span> solid green;}
}

<span class="hljs-keyword">@keyframes</span> rotate-hue {
  <span class="hljs-selector-tag">from</span> {<span class="hljs-attribute">filter</span>: <span class="hljs-built_in">hue-rotate</span>(<span class="hljs-number">0deg</span>);}
  <span class="hljs-selector-tag">to</span> {<span class="hljs-attribute">filter</span>: <span class="hljs-built_in">hue-rotate</span>(<span class="hljs-number">360deg</span>);}
}
</code></pre>
<p><a target="_blank" href="https://codesweetly.com/try-it-sdk/css/css-animations/js-4lyg4d"><strong>Try Editing It</strong></a></p>
<p>The snippet above applied three <code>@keyframes</code> rulesets to the <code>div</code> element using commas (<code>,</code>) to separate each <code>@keyframes</code>' configurations.</p>
<p><strong>Note:</strong> We used the <a target="_blank" href="https://www.quackit.com/css/functions/css_hue-rotate_function.cfm"><code>hue-rotate()</code></a> function to rotate the <code>div</code>'s colors.</p>
<h2 id="heading-important-stuff-to-know-about-css-transitions-and-animations">Important Stuff to Know about CSS Transitions and Animations</h2>
<ol>
<li>You can't animate all CSS properties. Have a look at MDN's <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties">Animatable CSS properties</a> article to see the ones you can animate.</li>
<li>CSS transitions and animations are <a target="_blank" href="https://codesweetly.com/web-tech-terms-e#expensive-operation-computing">expensive operations</a> for most CSS properties—except <code>opacity</code> and <code>transform</code>. In other words, applying transitions (or animations) to any CSS box model property is inherently a <a target="_blank" href="https://codesweetly.com/web-tech-terms-c#cpu-intensive">CPU-intensive</a> task. Therefore, animate only <code>opacity</code>, and <code>transform</code> properties if you are concerned about your page's performance.</li>
<li>Be mindful of the <a target="_blank" href="https://dzhavat.github.io/2021/02/18/debugging-layout-repaint-issues-triggered-by-css-transition.html">layout repainting issues</a> that CSS transitions may cause through your elements' stacking order.</li>
</ol>
<h2 id="heading-wrapping-up">Wrapping up</h2>
<p>In this article, we discuss the differences between CSS transitions and animations. We also used examples to discuss how to use them.</p>
<p>Thanks for reading.</p>
<h3 id="heading-and-heres-a-useful-react-typescript-resource">And here's a useful React TypeScript resource:</h3>
<p>I wrote a book about <a target="_blank" href="https://amzn.to/3Pa4bI4">Creating NPM Packages</a>!</p>
<p>It is a beginner-friendly book that takes you from zero to creating, testing, and publishing NPM packages like a pro.</p>
<p><a target="_blank" href="https://amzn.to/3Pa4bI4"><img src="https://www.freecodecamp.org/news/content/images/2023/09/creating-npm-package-banner-codesweetly.png" alt="Creating NPM Package Book Now Available at Amazon" width="600" height="400" loading="lazy"></a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Sign Up Form with Floating Labels and Transitions Using Plain HTML and CSS ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial we are going to build a modern sign up form with floating labels and smooth transitions using plain HTML and CSS. A view As you can see in the above image, when an input within the form gains focus, its label floats to the top and a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-sign-up-form-with-html-and-css/</link>
                <guid isPermaLink="false">66d4617c57503cc72873dee0</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ transitions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Mon, 13 Sep 2021 21:15:35 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/floating-label.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial we are going to build a modern sign up form with floating labels and smooth transitions using plain HTML and CSS.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--9--2.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>A view</em></p>
<p>As you can see in the above image, when an input within the form gains focus, its label floats to the top and a semi-thick border appears around the input. If text gets typed into the input and the input loses focus, the label remains on top. Otherwise, the label drops back down into the input.</p>
<p>Many modern forms have some sort of transition applied to them. Not only do these transitions make the form more dynamic, but they also help guide the user on the state of the input (that is whether it has focus or not) and what kind of data each input is expected to handle.</p>
<p>In this tutorial, you will learn about some cool CSS features like transitions, selectors like <code>:placeholder_focus</code> ,and many other CSS properties you should know.</p>
<p>Let's dive in!</p>
<h2 id="heading-the-html-markup">The HTML Markup</h2>
<p>We are going to define the markup for our sign up form. But before that, we have to set up our HTML boilerplate and correctly link to our stylesheet from the <code>head</code> tag. You can easily do this with the <a target="_blank" href="https://emmet.io/">Emmet plugin</a> by typing <code>!</code> then tab in your IDE/Code editor.</p>
<p>You can also copy this boilerplate and paste it into your <code>index.html</code> file:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"ie=edge"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Within the body tag, we define the markup for our sign up form:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"signupFrm"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Sign up<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Email<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Username<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Password<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Confirm Password<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"submitBtn"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Sign up"</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We create a container <code>div</code> to hold the form element. Each of the form's inputs, along with its text label, are wrapped inside a container div. The labels serve the purpose of informing the user what information each input should take in.</p>
<p>And our page will look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/noCSS.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>HTML Form of four inputs and four labels</em></p>
<p>You may notice that the placeholder value we have assigned to all inputs is "a". This will be helpful later in the tutorial when we start to apply some dynamic logic.</p>
<h2 id="heading-how-to-style-the-form">How to Style the Form</h2>
<p>Our form is pretty basic, so let's add some styling to make it look nicer.</p>
<p>First, we need to perform some resets and set the background color:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Lato&amp;display=swap'</span>);

<span class="hljs-comment">/* Get rid of all default margins/paddings. Set typeface */</span>
<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">box-sizing</span>: border-box;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"lato"</span>, sans-serif;
}
</code></pre>
<p>Here's what our page will look like:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/reset-lato.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Without any styling yet</em></p>
<p>After stying the <code>body</code>, we'll set the display mode of the content to <code>flex</code>. This makes sure that all the direct children inside a container element <code>div</code> are displayed side-by-side by default.</p>
<p>In our case, there's only one child inside the container <code>signupFrm</code>. The only reason we use <code>display: flex</code> here is to use the <code>align-items</code> and <code>justify-content</code> properties to help center everything vertically and horizontally:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Puts the form in the center both horizontally and vertically. Sets its height to 100% of the viewport's height */</span>

<span class="hljs-selector-class">.signupFrm</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>The <code>vh</code> property, which stands for viewport height, ensures that the form takes up 100% of the height of the browser window, regardless of the screen size or orientation. That will make it more responsive.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/middle.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Our form is now aligned to the center</em></p>
<p>Now we'll style the form a bit:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.form</span> {
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span> <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">10px</span> <span class="hljs-number">25px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">92</span>, <span class="hljs-number">99</span>, <span class="hljs-number">105</span>, .<span class="hljs-number">2</span>);
}

<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>In the first style targeted on the form, we set the background to white, we give it a width of 400px, we add some curve around the form, and finally we set a shadow around the box. We also set the font size of the title and some space below the element.</p>
<p>And the result should look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/with-card.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The form is now inside a card, with a box shadow</em></p>
<p>Next, we'll style the <code>div</code> which contains the form inputs and form labels.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.inputContainer</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">45px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">90%</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">17px</span>;
}
</code></pre>
<p>We set the position property of our input's container <code>div</code> to relative. This will enable us position the children <code>input</code> and <code>label</code> however we want. We also set the width to take up 90 percent of the entire container width.</p>
<p>This is how our form will be rendered in the web browser.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/margin-added.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Looks better</em></p>
<p>Now we need to style our inputs.</p>
<p>We first set the <code>position</code> to <code>absolute</code>. This will allow us to move each of them to the top-left part of the relatively positioned container parent.</p>
<p>We also need to hide our arbitrary placeholder text (the "a" characters mentioned earlier), so they don't overlap with the text within each label. The placeholder text will be needed when we implement the transition:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Style the inputs */</span>

<span class="hljs-selector-class">.input</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#DADCE0</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">7px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">outline</span>: none;
  <span class="hljs-attribute">background</span>: none;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">1</span>;
}

<span class="hljs-comment">/* Hide the placeholder texts (a) */</span>

<span class="hljs-selector-pseudo">::placeholder</span> {
  <span class="hljs-attribute">color</span>: transparent;
}
</code></pre>
<p>With the styles applied, our form should now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/absolute.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The placeholder "a" is no longer visible</em></p>
<p>Next, we style the text labels:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Styling text labels */</span>

<span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">4px</span>;
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#DADCE0</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">transition</span>: <span class="hljs-number">0.5s</span>;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">0</span>;
}
</code></pre>
<p>The label shows text that tells which information is expected inside the input. We start by setting its position to absolute. And by setting the <code>top</code> and <code>left</code> properties, we can move the text upwards relative to its container.</p>
<p>Next we set a transition of 0.5 seconds. This is how long it will take the text to move up when hovered on.</p>
<p>Finally, we also set a z-index of 0. The low z-index ensures that the label is positioned behind other "higher-placed" elements if they ever overlap.</p>
<p>Here is what gets rendered on the page:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/labels-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The result</em></p>
<p>Now we are going to focus on the buttons.</p>
<p>We'll add some smooth animations with the CSS <code>transform</code> property, which moves the button up a little and changes the color once it's hovered over:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.submitBtn</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">margin-left</span>: auto;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span> <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">6px</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">30px</span>;
}

<span class="hljs-selector-class">.submitBtn</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#9867C5</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">2px</span>);
}
</code></pre>
<p>Here is the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--6-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The button scales up and changes color when hovered over</em></p>
<p>Next, we need to perform some state changes.</p>
<p>When an input gains focus, we want to position its label beyond the top of the container (-7px), 3 pixels from the left, reduce the font-size to 14, and change the color to purple:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.input</span><span class="hljs-selector-pseudo">:focus</span> + <span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">top</span>: -<span class="hljs-number">7px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">3px</span>;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">10</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">600</span>;
  <span class="hljs-attribute">color</span>: purple;
}
</code></pre>
<p>Here's the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--5-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Labels go up when input gains focus</em></p>
<p>We also need to add a purple border around the input when it gains focus.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.input</span><span class="hljs-selector-pseudo">:focus</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid purple;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--7--3.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Purple border added</em></p>
<p>Finally, we have to do something very important.</p>
<p>Currently, when you type some text into the form and move focus (your mouse) away from it, the label text and the text within the input collide:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--8--3.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Collision between label and input value</em></p>
<p>With the following CSS, we'll specify that, when we type a value into the input and change focus, we want the label to remain floating. Also, well specify that we want the label text to lose its purple color:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.input</span><span class="hljs-selector-pseudo">:not(</span><span class="hljs-selector-pseudo">:placeholder-shown)+</span> <span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">top</span>: -<span class="hljs-number">7px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">3px</span>;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">10</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">600</span>;
}
</code></pre>
<p>And with that, here is the final look of our sign up page.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--9--1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final look</em></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>I hope you learned some new things about CSS from this tutorial. CSS transitions bring life to your website, and in this guide we have made our form much more lively with them.</p>
<p>You can get all the code from this tutorial from this <a target="_blank" href="https://github.com/KingsleyUbah/Sign-Up-CSS">GitHub repository</a>.</p>
<p>I recently created a newsletter where I provide practical tips and resources on learning web development. Subscribe to my <a target="_blank" href="https://www.getrevue.co/profile/ubahthebuilder">newsletter</a> and get tips right in your inbox.</p>
<p>Thanks for following along.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ CSS Transitions explained ]]>
                </title>
                <description>
                    <![CDATA[ The simplest (and most straightforward) way to animate your components is through CSS Transitions. In this article, you’ll learn how CSS Transitions work, and how to make animations with it. A transition occurs when a CSS property changes from one va... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-transitions-explained-d67ab9a02049/</link>
                <guid isPermaLink="false">66c4c7244173ed342943d0b0</guid>
                
                    <category>
                        <![CDATA[ animations ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ transitions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Zell Liew ]]>
                </dc:creator>
                <pubDate>Fri, 22 Dec 2017 02:36:13 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*dtrF5zNPuHvPlUSY.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The simplest (and most straightforward) way to animate your components is through CSS Transitions. In this article, you’ll learn how CSS Transitions work, and how to make animations with it.</p>
<p>A transition occurs when a CSS property changes from one value to another value over a period of time.</p>
<p>You can create CSS Transitions with the <code>transition</code> property:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition</span>: property duration transition-timing-function delay; 
}
</code></pre>
<p>The <code>transition</code> property is a shorthand of four CSS properties, <code>transition-property</code>, <code>transition-duration</code>, <code>transition-timing-function</code>, <code>transition-delay</code>.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-property</span>: property; 
  <span class="hljs-attribute">transition-duration</span>: duration; 
  <span class="hljs-attribute">transition-timing-function</span>: timing-function; 
  <span class="hljs-attribute">transition-delay</span>: delay 

  <span class="hljs-comment">/* The transition property is the shorthand for the above four properties */</span> 
  transition: property duration timing-function delay; 
}
</code></pre>
<p><code>transition-property</code> refers to the CSS property you wish to transition. It is required in the <code>transition</code> shorthand.</p>
<p><code>transition-duration</code> refers to the duration of the transition. How long do you want the transition to last? This value is written in seconds with the <code>s</code> suffix (like <code>3s</code>). It is also required in the <code>transition</code> shorthand.</p>
<p><code>transition-timing-function</code> refers to how to transition occurs. You’ll learn more about this later.</p>
<p><code>transition-delay</code> refers to how long you want to wait before starting the duration. This value is written in seconds with the <code>s</code> suffix (like <code>3s</code>). <code>transition-delay</code> is optional in the <code>transition</code> shorthand.</p>
<h3 id="heading-triggering-transitions">Triggering transitions</h3>
<p>You can trigger CSS transitions directly with pseudo classes like <code>:hover</code> (activates when mouse goes over an element), <code>:focus</code> (activates when a user tabs onto an element, or when a user clicks into an input element), or <code>:active</code> (activates when user clicks on the element).</p>
<pre><code class="lang-css"><span class="hljs-comment">/* creating transitions directly in CSS */</span> 
<span class="hljs-selector-class">.button</span> { 
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#33ae74</span>; 
  <span class="hljs-attribute">transition</span>: background-color <span class="hljs-number">0.5s</span> ease-out; 
} 

<span class="hljs-selector-class">.button</span><span class="hljs-selector-pseudo">:hover</span> { 
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#1ce</span>; 
}
</code></pre>
<div class="embed-wrapper"><iframe height="500" width="500" style="width:100%" src="//codepen.io/zellwk/embed/Qqzzxd/?height=265&amp;theme-id=0&amp;default-tab=css,result" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/zellwk/pen/Qqzzxd/">CSS Transition</a> by Zell Liew
  (<a href="https://codepen.io/zellwk">@zellwk</a>) on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>You can also trigger CSS transitions through JavaScript by adding or removing a class.</p>
<pre><code class="lang-css"><span class="hljs-comment">/* CSS */</span>
<span class="hljs-selector-class">.button</span> { 
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#33ae74</span>; 
  <span class="hljs-attribute">transition</span>: background-color <span class="hljs-number">0.5s</span> ease-out; 
} 

<span class="hljs-selector-class">.button</span><span class="hljs-selector-class">.is-active</span> { <span class="hljs-attribute">color</span>: <span class="hljs-number">#1ce</span>; }

// <span class="hljs-selector-tag">JavaScript</span>
<span class="hljs-selector-tag">const</span> <span class="hljs-selector-tag">button</span> = <span class="hljs-selector-tag">document</span><span class="hljs-selector-class">.querySelector</span>('<span class="hljs-selector-class">.button</span>') <span class="hljs-selector-tag">button</span><span class="hljs-selector-class">.addEventListener</span>('<span class="hljs-selector-tag">click</span>', _ =&gt; <span class="hljs-selector-tag">button</span><span class="hljs-selector-class">.classList</span><span class="hljs-selector-class">.toggle</span>('<span class="hljs-selector-tag">is-active</span>'))
</code></pre>
<div class="embed-wrapper"><iframe height="500" width="500" style="width:100%" src="//codepen.io/zellwk/embed/GMPPBg/?height=265&amp;theme-id=0&amp;default-tab=css,result" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/zellwk/pen/GMPPBg/">CSS Transition with JavaScript</a> by Zell Liew
  (<a href="https://codepen.io/zellwk">@zellwk</a>) on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-understanding-transition-timing-function">Understanding transition-timing-function</h3>
<p>The <code>transition-timing-function</code> governs how a transition occurs. All transitions have a value of <code>linear</code> by default, which means the property changes evenly until the end of the transition.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition</span>: transform <span class="hljs-number">1s</span> linear; 

  <span class="hljs-comment">/* OR */</span> 
  <span class="hljs-attribute">transition-property</span>: transform; 
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">1s</span>; 
  <span class="hljs-attribute">transition-timing-function</span>: linear; 
}
</code></pre>
<p>The thing is, few things move linearly in life. That’s not how real objects move. Sometimes, we accelerate; sometimes, we decelerate. The <code>transition-timing-function</code> allows you to capture all of that.</p>
<p>Imagine yourself throwing a tennis ball into an open field. The ball leaves your hand with the maximum speed. As it moves, it loses energy, it decelerates and eventually comes to a halt. This is called <code>ease-out</code>. There’s a timing function for it.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-timing-function</span>: ease-out; 
}
</code></pre>
<p>Now imagine you’re in a car. It’s not moving right now. When you move the car, it accelerates and goes toward its top speed. This is called <code>ease-in</code>. There’s also a timing function for it.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-timing-function</span>: ease-in; 
}
</code></pre>
<p>Since you have <code>ease-in</code> and <code>ease-out</code>, there’s also a timing function that combines the two together, <code>ease-in-out</code>. (I advise against using <code>ease-in-out</code> in your transitions unless your transitions last longer than a second. Nothing eases in and out within a second. It simply looks weird.)</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-timing-function</span>: ease-in-out; 
}
</code></pre>
<p>See this pen for a demo of the timing functions you’ve learned so far:</p>
<div class="embed-wrapper"><iframe height="500" width="500" style="width:100%" src="//codepen.io/zellwk/embed/Oxrqpo/?height=265&amp;theme-id=0&amp;default-tab=css,result" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/zellwk/pen/Oxrqpo/">CSS Transition Timing Functions (no cubic)</a> by Zell Liew
  (<a href="https://codepen.io/zellwk">@zellwk</a>) on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>Finally, if you don’t like any of the above choices, you can create your own timing function with <code>cubic-bezier</code>.</p>
<h3 id="heading-creating-your-own-timing-function-with-cubic-bezier">Creating your own timing function with cubic-bezier</h3>
<p>A Cubic-bezier is a set of four values that determine your transition-timing-function. It looks like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-timing-function</span>: <span class="hljs-built_in">cubic-bezier</span>(x1, y1, x2, y2); 
}
</code></pre>
<p>Don’t worry about the <code>x1</code>, <code>y1,</code>, <code>x2</code> and <code>y2</code>. You’ll never create cubic-bezier curves by writing numbers yourself (unless you already know what they mean and you’re tweaking your timing function for perfection).</p>
<p>You can rely on both Chrome and Firefox’s trusty developer tools to help you create your curves. To use it, you add a <code>transition-timing-function</code> into an element, then open up devtools and click on the timing function.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*dtrF5zNPuHvPlUSY.png" alt="Image" width="800" height="410" loading="lazy"></p>
<p>See the Pen <a target="_blank" href="https://codepen.io/zellwk/pen/gGZqNo/">CSS Transition Timing Functions</a> by Zell Liew (<a target="_blank" href="https://codepen.io/zellwk">@zellwk</a>) on <a target="_blank" href="https://codepen.io/">CodePen</a>.</p>
<p>Going in-depth about creating your own bezier curves for your animations is out of scope for today’s article. If you’re interested, you can find more information about cubic-bezier curves in “<a target="_blank" href="https://www.smashingmagazine.com/2014/04/understanding-css-timing-functions/">Understanding CSS Timing Functions</a>“ by <a target="_blank" href="https://twitter.com/Stephen_Greig">Stephen Greig</a>.</p>
<h3 id="heading-transitioning-two-or-more-properties">Transitioning two or more properties</h3>
<p>You can transition two (or more) CSS properties by separating them with a comma in your <code>transition</code> or <code>transition-property</code> property.</p>
<p>You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition</span>: background-color <span class="hljs-number">1s</span> ease-out, color <span class="hljs-number">1s</span> ease-out; 

  <span class="hljs-comment">/* OR */</span>  
  <span class="hljs-attribute">transition-property</span>: background, color; 
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">1s</span>; 
  <span class="hljs-attribute">transition-timing-function</span>: ease-out; 
}
</code></pre>
<p>You may be tempted to transition every CSS property with <code>all</code>. Don’t ever do this. This is bad for performance. Always specify the property you’re trying to transition.</p>
<pre><code class="lang-css"><span class="hljs-comment">/* DON'T EVER DO THIS */</span> 
<span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-property</span>: all;
} 

<span class="hljs-comment">/* ALWAYS DO THIS */</span> 
<span class="hljs-selector-class">.selector</span> { 
  <span class="hljs-attribute">transition-property</span>: background-color, color, transform; 
}
</code></pre>
<h3 id="heading-transitioning-in-vs-transitioning-out">Transitioning in vs transitioning out</h3>
<p>Sometimes, you want the properties to transition in and out at differently. You want the duration, timing-function or delay to be different.</p>
<p>To do so, you write another set of <code>transition-</code> properties.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.button</span> { 
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#33ae74</span>; 
  <span class="hljs-attribute">transition</span>: background-color <span class="hljs-number">0.5s</span> ease-out; 
} 

<span class="hljs-selector-class">.button</span><span class="hljs-selector-pseudo">:hover</span> { 
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#1ce</span>; 
  <span class="hljs-attribute">transition-duration</span>: <span class="hljs-number">2s</span>; 
}
</code></pre>
<p>When you write transition properties in the triggering (pseudo) class, the transition properties in the triggering class overwrites the original transition properties you’ve stated in the base class.</p>
<p>So, in the example above, when you hover on the button, the background color takes 2 seconds to change from <code>#33ae74</code> to <code>#1ce</code>.</p>
<p>When you hover out from the button, the background color only takes 0.5s to change back to <code>#1ce</code> because the <code>transition-duration</code> of 2s no longer exists.</p>
<div class="embed-wrapper"><iframe height="500" width="500" style="width:100%" src="//codepen.io/zellwk/embed/GOLLyR/?height=265&amp;theme-id=0&amp;default-tab=css,result" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/zellwk/pen/GOLLyR/">CSS Transition (different transition speeds when transitioning in and out)</a> by Zell Liew
  (<a href="https://codepen.io/zellwk">@zellwk</a>) on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-wrapping-up">Wrapping up</h3>
<p>CSS Transitions are the easiest way to perform animations. You can create transitions either with the <code>transition</code> shorthand or with <code>transition-</code> properties.</p>
<p>To create a transition, you overwrite a property in a class (or psuedo class), and you specify the property to transit in <code>transition</code> or <code>transition-property</code>.</p>
<p>Remember to change your <code>transition-timing-function</code> so your animation looks more real!</p>
<p>If you loved this article, you’ll love learn <a target="_blank" href="https://zellwk.com/learnjs/">Learn JavaScript</a> — a course that helps you learn to <strong>build real components from scratch</strong> with JavaScript.</p>
<p>(Oh, by the way, if you liked this article, I’d appreciate it if you could <a target="_blank" href="http://twitter.com/share?text=CSS%20Transitions%20explained%20by%20%40zellwk%20?%20&amp;url=https://zellwk.com/blog/css-transitions/&amp;hashtags=">share it</a>. )</p>
<p><em>Originally published at <a target="_blank" href="https://zellwk.com/blog/css-transitions/">zellwk.com</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
