<?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[ image masking - 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[ image masking - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 10:50:23 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/image-masking/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How I built a mood changing animation using CSS masks ]]>
                </title>
                <description>
                    <![CDATA[ By Ankit Karnany Remember the cartoons we used to watch during our childhood? At that time they were the epitome of animations. Nowadays, animations aren’t just limited to cartoons — we come across them almost everyday when we check our phone or look... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-i-built-a-mood-changing-animation-using-css-masks-565b16ed051f/</link>
                <guid isPermaLink="false">66c34d5593db2451bd441486</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ image masking ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 02 Aug 2018 17:02:26 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*tTAVggfIn612FqCu" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ankit Karnany</p>
<p>Remember the cartoons we used to watch during our childhood? At that time they were the epitome of animations. Nowadays, animations aren’t just limited to cartoons — we come across them almost everyday when we check our phone or look at any device which has a screen.</p>
<p>Today, animation is used not only to attract attention but to enhance the user experience and guide user flow. In any good design, animations are added in such a way that they blend with the common flow, thus creating a seamless user experience.</p>
<p>So, in this article we will build a simple animation of a face with different expressions, and we’ll learn a little bit of CSS in that process.</p>
<h3 id="heading-getting-started">Getting started</h3>
<p>We will use a CSS technique which is somewhat rare among web developers, but that designers use quite often. It is called <strong>masking</strong>.</p>
<p>So what comes to your mind when you hear “mask?”</p>
<p>You’re probably picturing a cover on top of something. That’s all we need to understand.</p>
<p>Wait — but this article is related to coding and using CSS animation…</p>
<p>Don’t worry! We’ll get right to it.</p>
<h3 id="heading-creating-the-basic-mask">Creating the basic mask</h3>
<p>Let say we have a <code>&lt;d</code>iv&gt; w<code>ith a background:</code> green; and it looks something like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/2XO9bGq7nwPzRnJwEVIGM18CnBW1sMXO0Hav" alt="Image" width="423" height="353" loading="lazy"></p>
<p>Now, say I have a <code>face.svg</code>:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/rtGKXNWrhwS0GfhGmUpb228Tu137Z4FvgMkc" alt="Image" width="250" height="231" loading="lazy"></p>
<p>If we apply a CSS property <code>mask-image: url(face.svg);</code> on the <code>&lt;d</code>iv&gt;, you’ll be amazed to see what we get:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/vKQ3qFzMOVQLuHbur4KZ19hm5Rn7PMfGtmoq" alt="Image" width="342" height="319" loading="lazy"></p>
<p>You might be thinking that something is odd here. The <code>face.svg</code> was placed over the <code>&lt;d</code>iv&gt;, but it took up the color o<code>f the back</code>ground. This is the opposite of what you might have expected. This happens because o<code>f the mas</code>k-type property which makes the opaque part of the svg transparent. This allows the background color to be visible.</p>
<p>Let’s not get deeper into that now. Just keep in mind that we can use <code>background-color</code> to change the color of the mask. If you are familiar with different ways of using <code>background-color</code>, we can also use gradients and write a simple gradient which fills red in the center and radially spreads into black outwards. The code for that is the following:</p>
<pre><code>background-image: -webkit-radial-gradient( hsla(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">50</span>%, <span class="hljs-number">.7</span>), hsla(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">20</span>%, <span class="hljs-number">.8</span>), hsla(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">10</span>%, <span class="hljs-number">1</span>));
</code></pre><p>Which results in this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/aX8BzmkSrurnCCGxBX6B1FTTfk0RA35SNVxe" alt="Image" width="392" height="338" loading="lazy"></p>
<h3 id="heading-adding-the-animation">Adding the animation</h3>
<p>Now let’s add some animation to this empty face. For this I have <code>expression.svg</code> which looks like the below image. For simplicity, I have created all the svgs with the same width and height, so that we avoid aligning the face and expression manually.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/SOtVpTHeSekjDBcZsh2fJWOnLoYPRe6CQvNg" alt="Image" width="250" height="231" loading="lazy"></p>
<p>Now <code>mask-image</code> has this cool option which allows multiple images to be used as masks. So we can do this: <code>mask-image: url(face.svg), url(expression.svg);</code>. This is what we have now:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/A6H-X9gCRAHHBWFo62WsClZqPbsqGgTrl0U4" alt="Image" width="341" height="299" loading="lazy"></p>
<p>One of the most important properties of CSS masks is <code>mask-position</code>, which positions the mask from the top left corner relative to its parent. And I can position multiple masks using the property <code>mask-position</code> just like <code>mask-image</code>.</p>
<p>So to make the face sad, we can use something like this: <code>mask-position: 0 0, 0 12px;</code>. And the result is this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/G3LPJQJhJulrprRLpA6-3abAq1L1-sv319pp" alt="Image" width="345" height="274" loading="lazy"></p>
<p>The first position <code>0 0</code> is for the <code>face.svg</code>, and the second <code>0 12px</code> is for <code>expression.svg</code>. This pushes it <strong>12px</strong> from above and results in the above expression.</p>
<h4 id="heading-applying-functionality">Applying functionality</h4>
<p>Now let’s animate these expressions on hover. So the complete code we get after applying the hover pseudo class is the following:</p>
<pre><code>i {  background-image: -webkit-radial-gradient(hsla(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">50</span>%, <span class="hljs-number">.7</span>), hsla(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">20</span>%, <span class="hljs-number">.8</span>) <span class="hljs-number">60</span>%, hsla(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">10</span>%, <span class="hljs-number">1</span>));    mask-image: url(<span class="hljs-string">'face.svg'</span>), url(<span class="hljs-string">'expression.svg'</span>);  mask-position: <span class="hljs-number">0</span> <span class="hljs-number">0</span>, <span class="hljs-number">0</span> <span class="hljs-number">12</span>px; <span class="hljs-comment">/* To make the sad expression */</span>
</code></pre><pre><code>  transition: mask-position <span class="hljs-number">.5</span>s ease-out;}
</code></pre><pre><code>i:hover {  background-image: -webkit-radial-gradient(hsla(<span class="hljs-number">120</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">50</span>%, <span class="hljs-number">.7</span>), hsla(<span class="hljs-number">120</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">20</span>%, <span class="hljs-number">.8</span>) <span class="hljs-number">60</span>%, hsla(<span class="hljs-number">120</span>, <span class="hljs-number">100</span>%, <span class="hljs-number">10</span>%, <span class="hljs-number">1</span>));
</code></pre><pre><code>  mask-position: <span class="hljs-number">0</span> <span class="hljs-number">0</span>, <span class="hljs-number">0</span> <span class="hljs-number">0</span>; <span class="hljs-comment">/* To make the happy expression */</span>
</code></pre><pre><code>  transition: mask-position <span class="hljs-number">.1</span>s linear;}
</code></pre><p>After playing with the CSS a bit more, we can do this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ipIKP8u6LWV6ikxw-HJv-1xrJWBYPYXQfqOv" alt="Image" width="702" height="466" loading="lazy"></p>
<p>This is one of the methods we can use to build those gripping animations we come across almost daily.</p>
<h4 id="heading-one-important-note"><strong>One Important Note</strong></h4>
<p>The masking properties might not work across all the browsers. So to make them work in all the browsers, just prepend browser specific labels like <code>-webkit-</code> , <code>-moz-</code> &amp; <code>-0-</code> .</p>
<p>You can look at the complete code here on <a target="_blank" href="https://github.com/nktkarnany/mask-css">github</a> and <a target="_blank" href="https://codepen.io/nktkarnany/pen/bjmZOQ">codepen</a>.</p>
<p>Thanks for reading! I hope you learned something.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
