<?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[ SVG - 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[ SVG - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 16:31:53 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/svg/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ SVG Toggle Button Tutorial – How to Handle Dark Mode with CSS and JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ How can you detect dark mode in CSS and JavaScript? How can you manually override it with a toggle button? And how can you create a sun and moon icon with SVG? In this tutorial, you will learn how to detect dark mode in CSS and JavaScript, and you wi... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-handle-dark-mode-with-css-and-javascript/</link>
                <guid isPermaLink="false">66c4c80b6e4b60b5b844529c</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hunor Márton Borbély ]]>
                </dc:creator>
                <pubDate>Fri, 08 Mar 2024 13:46:26 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/03/thumbnail.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>How can you detect dark mode in CSS and JavaScript? How can you manually override it with a toggle button? And how can you create a sun and moon icon with SVG?</p>
<p>In this tutorial, you will learn how to detect dark mode in CSS and JavaScript, and you will create a toggle button with SVG to override the default behavior. You will use plain HTML, CSS, and JavaScript, so you don't need any preliminary requirements before starting.</p>
<p>You can also <a target="_blank" href="https://youtu.be/GUSUA72t7p0">watch this article as a video</a> on YouTube.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-handle-dark-mode-with-css">How to Handle Dark Mode with CSS</a></li>
<li><a class="post-section-overview" href="#heading-how-to-code-a-sun-icon-with-svg">How to Code a Sun Icon with SVG</a></li>
<li><a class="post-section-overview" href="#heading-how-to-detect-dark-mode-in-javascript">How to Detect Dark Mode in JavaScript</a></li>
<li><a class="post-section-overview" href="#heading-how-to-code-a-moon-icon-with-svg">How to Code a Moon Icon with SVG</a></li>
<li><a class="post-section-overview" href="#heading-how-to-toggle-dark-mode-with-javascript">How to Toggle Dark Mode with JavaScript</a></li>
<li><a class="post-section-overview" href="#next-step">Next Steps</a></li>
</ul>
<h2 id="heading-how-to-handle-dark-mode-with-css">How to Handle Dark Mode with CSS</h2>
<p>Let's say you have a simple website with some text. By default, you set the text color to be black and the background color to be white. Implementing dark mode for this site with CSS is very simple:</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">title</span>&gt;</span>Dark Mode<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">"index.css"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"index.js"</span> <span class="hljs-attr">defer</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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">p</span>&gt;</span>
      How to detect dark mode in CSS and in JavaScript? How can we override it
      manually with a toggle button? In this quick tutorial, we look into
      detecting dark mode in CSS and JavaScript, and then we create a toggle
      button with SVG to override the default behavior.
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</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><img src="https://www.freecodecamp.org/news/content/images/2024/03/Screenshot-2024-03-07-at-11.33.28.png" alt="Image" width="600" height="400" loading="lazy">
<em>A simple website with some text in dark mode</em></p>
<p>All you need to do is add a media query and set a condition. With this condition, you set the following CSS statements to be only valid if the preferred color scheme is dark. </p>
<p>Inside this media query, you can define the colors for dark mode. In this case, you flip the colors and set the text color to white and the background color to black:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: Montserrat;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">500px</span>;
}

<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
  <span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">background-color</span>: black;
    <span class="hljs-attribute">color</span>: white;
  }
}
</code></pre>
<p>This will take the setting from your OS or browser setting. By default, it comes from the Operating System, but the browser can decide to override it. In Google Chrome, you can find this setting under 'Appearance'. By default, it follows the device setting.</p>
<p>What's great about the CSS solution is that if you change this setting while visiting the website, the styling will update automatically.</p>
<p>This way, you can set a custom style for the body element and any other elements as well. </p>
<p>It doesn't work in one case, though. You cannot style what's inside an HTML Canvas element with CSS. If you built up a <a target="_blank" href="https://www.freecodecamp.org/news/gorillas-game-in-javascript/">game entirely from JavaScript</a> using the Canvas API or Three.js, you must also set the colors for dark mode in JavaScript.</p>
<p>In the next steps, we will cover this and look into how to create an SVG toggle button to switch between light and dark modes.</p>
<h2 id="heading-how-to-code-a-sun-icon-with-svg">How to Code a Sun Icon with SVG</h2>
<p>Before you learn how to handle dark mode in JavaScript, let's take a quick detour and see how to code a dark mode toggle button with SVG. Detecting dark mode is one thing, but you should allow the user to manually toggle between light and dark modes.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.022.png" alt="Image" width="600" height="400" loading="lazy">
<em>Sun Icon</em></p>
<p>Check out my previous tutorial if you need a quick introduction to <a target="_blank" href="https://www.freecodecamp.org/news/svg-tutorial-learn-to-code-images/">coding SVG icons</a>. It contains many great examples from beginner to advanced levels. And if you are new to SVGs, don't worry. These are very simple examples.</p>
<p>So, let's start with the <code>svg</code> element. This will serve as a container for all the image elements. Set its size to 30 times 30:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.004.png" alt="Image" width="600" height="400" loading="lazy">
<em>The <code>svg</code> element</em></p>
<p>Then, add a circle. For a <code>circle</code> element, you have to set the center coordinates of the circle and its radius. The center coordinates are both 15, and the radius is 6. Then, set a color with the <code>fill</code> property:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.006.png" alt="Image" width="600" height="400" loading="lazy">
<em>We add a <code>circle</code> as the core of the sun</em></p>
<p>To set the color, you can use the <code>currentColor</code> property that takes over the current <code>color</code> setting from CSS. This will come in handy later when you toggle dark and light modes. The icon will switch colors automatically.</p>
<p>Then, add the sun rays. You need to use the <code>line</code> element for this, where you have to set the starting and end coordinates. You can also set the stroke color with the <code>stroke</code> property, the <code>stroke-width</code> to add thickness, and the <code>stroke-linecap</code> property to make the ends of the lines rounded:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">line</span>
    <span class="hljs-attr">id</span>=<span class="hljs-string">"ray"</span>
    <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
    <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span>
    <span class="hljs-attr">stroke-linecap</span>=<span class="hljs-string">"round"</span>
    <span class="hljs-attr">x1</span>=<span class="hljs-string">"15"</span>
    <span class="hljs-attr">y1</span>=<span class="hljs-string">"1"</span>
    <span class="hljs-attr">x2</span>=<span class="hljs-string">"15"</span>
    <span class="hljs-attr">y2</span>=<span class="hljs-string">"4"</span>
  &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">line</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.010.png" alt="Image" width="600" height="400" loading="lazy">
<em>We add a <code>line</code> element as a sunray</em></p>
<p>Now, once you have one ray, you can reuse the same ray to draw the others. </p>
<p>You can give this ray an <code>id</code> and reuse it with the <code>use</code> element. For the reused elements, you can set a rotation. Set the rotation angle and the center of rotation. You want to rotate the rays around the center of the sun, so set it to <code>15,15</code>. Then, increment the rotation by 45 degrees for each ray:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">line</span>
    <span class="hljs-attr">id</span>=<span class="hljs-string">"ray"</span>
    <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
    <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span>
    <span class="hljs-attr">stroke-linecap</span>=<span class="hljs-string">"round"</span>
    <span class="hljs-attr">x1</span>=<span class="hljs-string">"15"</span>
    <span class="hljs-attr">y1</span>=<span class="hljs-string">"1"</span>
    <span class="hljs-attr">x2</span>=<span class="hljs-string">"15"</span>
    <span class="hljs-attr">y2</span>=<span class="hljs-string">"4"</span>
  &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">line</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(45 15 15)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(90 15 15)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(135 15 15)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(180 15 15)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(225 15 15)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(270 15 15)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(315 15 15)"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.016.png" alt="Image" width="600" height="400" loading="lazy">
<em>The finished sun icon</em></p>
<h2 id="heading-how-to-detect-dark-mode-in-javascript">How to Detect Dark Mode in JavaScript</h2>
<p>Before we get to the moon icon, let's see how to detect dark mode in JavaScript. This can be useful when building a game, like we did a couple weeks ago in the <a target="_blank" href="https://www.freecodecamp.org/news/gorillas-game-in-javascript/">Gorillas JavaScript game tutorial</a>. </p>
<p>In that game, we were drawing on an HTML Canvas element with JavaScript. We set all the colors with JavaScript. If we want to support dark mode, we can set the colors based on a <code>darkMode</code> variable. But how do we detect if we are in dark mode? How do we set the value of this variable?</p>
<p>The following code is an example snippet from the game tutorial above. Here we set the fill color before we draw a rectangle on the canvas. To learn more about drawing on a <code>canvas</code> element, check out <a target="_blank" href="https://www.freecodecamp.org/news/gorillas-game-in-javascript/">this tutorial</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.018.png" alt="Image" width="600" height="400" loading="lazy">
<em>When drawing on a <code>canvas</code> element we set the colors from JavaScript. But how we detect dark mode?</em></p>
<p>Detecting dark mode in JavaScript is also very simple. Interestingly enough, this solution also depends on the CSS query selectors you used before.</p>
<p>You can create a <code>matchMedia</code> object with the same condition we used in CSS. This method can check if the document matches a media query. Pass on <code>prefers-color-scheme: dark</code> as an argument:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> darkModeMediaQuery = <span class="hljs-built_in">window</span>.matchMedia(<span class="hljs-string">"(prefers-color-scheme: dark)"</span>);

<span class="hljs-keyword">let</span> darkMode = darkModeMediaQuery.matches;

. . .

function drawBuildings() {
  state.buildings.forEach(<span class="hljs-function">(<span class="hljs-params">building</span>) =&gt;</span> {
    ctx.fillStyle = darkMode ? <span class="hljs-string">"#254D7E"</span> : <span class="hljs-string">"#947285"</span>;
    ctx.fillRect(building.x, <span class="hljs-number">0</span>, building.width, building.height);
  });
}
</code></pre>
<p>Then, can check the <code>matches</code> property of this object. If it is true, then you are in dark mode. You can save this into a variable, and later, you can use this variable to decide what colors you should use when painting on the canvas element.</p>
<p>This variable, however, doesn't get refreshed automatically when you switch between light mode and dark mode. You need to add an event listener that detects if the settings change. </p>
<p>Here, we define a function that checks the <code>matches</code> property of the incoming object to decide if you just switched to bright or dark mode:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> darkModeMediaQuery = <span class="hljs-built_in">window</span>.matchMedia(<span class="hljs-string">"(prefers-color-scheme: dark)"</span>);

<span class="hljs-keyword">let</span> darkMode = darkModeMediaQuery.matches;

darkModeMediaQuery.addEventListener(<span class="hljs-string">"change"</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (e.matches) {
    darkMode = <span class="hljs-literal">true</span>;
  } <span class="hljs-keyword">else</span> {
    darkMode = <span class="hljs-literal">false</span>;
  }
});

. . .

function drawBuildings() {
  state.buildings.forEach(<span class="hljs-function">(<span class="hljs-params">building</span>) =&gt;</span> {
    ctx.fillStyle = darkMode ? <span class="hljs-string">"#254D7E"</span> : <span class="hljs-string">"#947285"</span>;
    ctx.fillRect(building.x, <span class="hljs-number">0</span>, building.width, building.height);
  });
}
</code></pre>
<p>Now, if you set the colors based on this <code>darkMode</code> variable, you should see that the game's appearance changes once you switch between light and dark mode in the OS settings. Check out this <a target="_blank" href="https://codepen.io/HunorMarton/pen/jOJZqvp">demo</a> to see it in action.</p>
<h2 id="heading-how-to-code-a-moon-icon-with-svg">How to Code a Moon Icon with SVG</h2>
<p>Before we discuss overriding the default OS setting with a toggle button, let's examine the other half of our toggle icon: Let's draw a moon.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.023.png" alt="Image" width="600" height="400" loading="lazy">
<em>The Sun and Moon icons</em></p>
<p>Start with an <code>svg</code> element of the same size and define a path inside it. You can define a <code>path</code> element by setting its <code>d</code> attribute. In this attribute, you build a path from a series of commands:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.026.png" alt="Image" width="600" height="400" loading="lazy">
<em>We define a <code>path</code> with a series of commands</em></p>
<p>You start with the move-to command to go to the initial position. This command consists of the letter <code>M</code> and the starting coordinate: </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.027.png" alt="Image" width="600" height="400" loading="lazy">
<em>Using the move-to command within a path</em></p>
<p>Then, use an arc command to draw the outer arc of the moon. This command might look a bit scary because it has several properties. Let's see what we have:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.028.png" alt="Image" width="600" height="400" loading="lazy">
<em>The arc command and it's several properties</em></p>
<p>A command always continues the previous command, so this arc will draw the arc from the coordinates of the move-to command. Commands also end with the coordinates of the endpoint. </p>
<p>Here, you set where the arc ends. The rest of the properties are about how to draw an arc from the starting point to the endpoint:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.029.png" alt="Image" width="600" height="400" loading="lazy">
<em>The last two properties of the arc command show the endpoint of the arc</em></p>
<p>The first two properties are the horizontal and vertical radius of our arc. In our case, we want to have the arc of a circle, so we set the same value for both. With the third argument, you can set a rotation. When both radiuses are the same, this property makes no difference. You can leave it at zero:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.030.png" alt="Image" width="600" height="400" loading="lazy">
<em>Horiyontal and vertical radius of the arc</em></p>
<p>Then, we have the large arc flag property. With this, you can decide whether to go the long or short way to our end coordinate. You can see that you can reach the endpoint in multiple ways, even with the same radiuses. </p>
<p>There are two arcs – in the case of the first one, you go the long way and in the case of the second one, you will go the short way. This is a flag, so the value here can be 0 or 1:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.032.png" alt="Image" width="600" height="400" loading="lazy">
<em>The large arc flag decides if we should reach the endpoint the short way or the long way</em></p>
<p>Finally, there is the sweep flag. This basically sets whether you should draw the arc clockwise or counterclockwise. The two options mirror each other. In the first case, you set this to zero – in the second, you set it to one:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.035.png" alt="Image" width="600" height="400" loading="lazy">
<em>The sweep flag decides if we should go clockwise or counterclockwise</em></p>
<p>Now that you have one arc, you set up the other one. Here, you set the endpoint to the beginning. To the same coordinates as you used for the move-to command.</p>
<p>Then you can use the same radiuses, but you have to change the large arc flag and the sweep flag to end up with a moon:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
    <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span>
    <span class="hljs-attr">d</span>=<span class="hljs-string">"
      M 23, 5
      A 12 12 0 1 0 23, 25
      A 12 12 0 0 1 23, 5"</span>
  /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.039.png" alt="Image" width="600" height="400" loading="lazy">
<em>The finished moon icon</em></p>
<p>How can you use these two icons in a button to toggle light mode and dark mode in JavaScript?</p>
<h2 id="heading-how-to-toggle-dark-mode-with-javascript">How to Toggle Dark Mode with JavaScript</h2>
<p>If you want to override the system or browser settings for dark mode with a manual switch, you can't rely on the CSS media query anymore. This works for rendering the UI based on the settings, but you can't override it from JavaScript.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Slides-1.041.png" alt="Image" width="600" height="400" loading="lazy">
<em>You can't override a CSS media query</em></p>
<p>Instead, you can define a <code>dark-mode</code> class and toggle it from JavaScript. </p>
<p>In CSS, define a class that will change the same settings the media query did before. Then, in JavaScript, you can use the same logic you had before to get the default setting and then add or remove this class. </p>
<p>You can set this class on our initial page load and toggle it if you click a button:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: Montserrat;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">500px</span>;
}

<span class="hljs-selector-class">.dark-mode</span> {
  <span class="hljs-attribute">background-color</span>: black;
  <span class="hljs-attribute">color</span>: white;
}
</code></pre>
<p>Now, how do you toggle this with a button? In your HTML file, add a button element with an event handler. Then, move both SVGs inside this button element and assign IDs for them. You will toggle the visibility of these icons from JavaScript:</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">title</span>&gt;</span>Dark Mode<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">"index.css"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"index.js"</span> <span class="hljs-attr">defer</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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">p</span>&gt;</span>
      How to detect dark mode in CSS and in JavaScript? How can we override it
      manually with a toggle button? In this quick tutorial, we look into
      detecting dark mode in CSS and JavaScript, and then we create a toggle
      button with SVG to override the default behavior.
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"toggleDarkMode()"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"light-icon"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"15"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span> /&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">line</span>
          <span class="hljs-attr">id</span>=<span class="hljs-string">"ray"</span>
          <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
          <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span>
          <span class="hljs-attr">stroke-linecap</span>=<span class="hljs-string">"round"</span>
          <span class="hljs-attr">x1</span>=<span class="hljs-string">"15"</span>
          <span class="hljs-attr">y1</span>=<span class="hljs-string">"1"</span>
          <span class="hljs-attr">x2</span>=<span class="hljs-string">"15"</span>
          <span class="hljs-attr">y2</span>=<span class="hljs-string">"4"</span>
        &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">line</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(45 15 15)"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(90 15 15)"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(135 15 15)"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(180 15 15)"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(225 15 15)"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(270 15 15)"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#ray"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(315 15 15)"</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"dark-icon"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
          <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span>
          <span class="hljs-attr">d</span>=<span class="hljs-string">"
          M 23, 5
          A 12 12 0 1 0 23, 25
          A 12 12 0 0 1 23, 5"</span>
        /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</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>You can also unset the default appearance of the button element in CSS, except the cursor property. You should have that as pointer:</p>
<pre><code class="lang-css">. . .

<span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">all</span>: unset;
  <span class="hljs-attribute">cursor</span>: pointer;
}

. . .
</code></pre>
<p>Now, let's implement the event handler in JavaScript. First you need to access the SVG icons by ID:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> lightIcon = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"light-icon"</span>);
<span class="hljs-keyword">const</span> darkIcon = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"dark-icon"</span>);

. . .
</code></pre>
<p>Then, add the <code>dark-mode</code> class to the <code>body</code> element in case you are in dark mode and hide one of the SVG icons based on the <code>darkMode</code> variable. You detect dark mode as you did before:</p>
<pre><code class="lang-js">. . .

<span class="hljs-comment">// Check if dark mode is preferred</span>
<span class="hljs-keyword">const</span> darkModeMediaQuery = <span class="hljs-built_in">window</span>.matchMedia(<span class="hljs-string">"(prefers-color-scheme: dark)"</span>);
<span class="hljs-keyword">let</span> darkMode = darkModeMediaQuery.matches;

<span class="hljs-comment">// Set dark-mode class on body if darkMode is true and pick icon</span>
<span class="hljs-keyword">if</span> (darkMode) {
  <span class="hljs-built_in">document</span>.body.classList.add(<span class="hljs-string">"dark-mode"</span>);
  darkIcon.setAttribute(<span class="hljs-string">"display"</span>, <span class="hljs-string">"none"</span>);
} <span class="hljs-keyword">else</span> {
  lightIcon.setAttribute(<span class="hljs-string">"display"</span>, <span class="hljs-string">"none"</span>);
}

. . .
</code></pre>
<p>And finally,  can implement the function that flips the <code>darkMode</code> property. This function toggles the <code>dark-mode</code> class on the body element, and toggles the SVG icons:</p>
<pre><code class="lang-js">. . .

<span class="hljs-comment">// Toggle dark mode on button click</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toggleDarkMode</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Toggle darkMode variable</span>
  darkMode = !darkMode;

  <span class="hljs-comment">// Toggle dark-mode class on body</span>
  <span class="hljs-built_in">document</span>.body.classList.toggle(<span class="hljs-string">"dark-mode"</span>);

  <span class="hljs-comment">// Toggle light and dark icons</span>
  <span class="hljs-keyword">if</span> (darkMode) {
    lightIcon.setAttribute(<span class="hljs-string">"display"</span>, <span class="hljs-string">"block"</span>);
    darkIcon.setAttribute(<span class="hljs-string">"display"</span>, <span class="hljs-string">"none"</span>);
  } <span class="hljs-keyword">else</span> {
    lightIcon.setAttribute(<span class="hljs-string">"display"</span>, <span class="hljs-string">"none"</span>);
    darkIcon.setAttribute(<span class="hljs-string">"display"</span>, <span class="hljs-string">"block"</span>);
  }
}
</code></pre>
<p>Now, this works: by default, you still have the setting from the OS or browser. But once you click this button, it will override this manually.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Screenshot-2024-03-07-at-12.31.42.png" alt="Image" width="600" height="400" loading="lazy">
<em>Final look in light mode</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Screenshot-2024-03-07-at-12.31.27.png" alt="Image" width="600" height="400" loading="lazy">
<em>Final look in dark mode</em></p>
<h2 id="heading-next-steps">Next Steps</h2>
<p>With all this in place, you have a functionality that takes the dark mode setting from the browser or the OS by default, and you can override it with a nice-looking toggle button. In the <a target="_blank" href="https://youtu.be/GUSUA72t7p0">YouTube version of this tutorial</a>, you can also learn how to use <code>localStorage</code> to save this setting for the next session.</p>
<p>If you want to learn more about SVGs, check out <a target="_blank" href="https://svg-tutorial.com/">SVG-tutorial.com</a>, where you can learn more about SVGs from beginner to advanced levels with many great examples.</p>
<p>If you want to use this behavior in a game, check out the <a target="_blank" href="https://www.freecodecamp.org/news/gorillas-game-in-javascript/">Gorillas JavaScript game tutorial</a>, where we build the entire game from scratch. It's a massive two-hour tutorial that covers drawing on a Canvas element with JavaScript and the whole game logic with plain JavaScript.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/2q5EufbUEQk" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p>Subscribe to my channel for more JavaScript game development tutorials:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/undefined" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to use the Fullscreen API in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ How do you run a game created for the web in fullscreen? In this quick tutorial, you'll see how to display a game or any other HTML element in fullscreen, how to exit fullscreen, and how to make a nice fullscreen toggle button in SVG. Recently I publ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-use-full-screen-api-in-js/</link>
                <guid isPermaLink="false">66c4c81129f446a67a4197ee</guid>
                
                    <category>
                        <![CDATA[ Fullscreen API ]]>
                    </category>
                
                    <category>
                        <![CDATA[ canvas ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hunor Márton Borbély ]]>
                </dc:creator>
                <pubDate>Thu, 22 Feb 2024 14:17:32 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/Untitled.022.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>How do you run a game created for the web in fullscreen? In this quick tutorial, you'll see how to display a game or any other HTML element in fullscreen, how to exit fullscreen, and how to make a nice fullscreen toggle button in SVG.</p>
<p>Recently I published a long <a target="_blank" href="https://www.freecodecamp.org/news/how-to-draw-a-gorilla-with-javascript-on-html-canvas/">JavaScript game tutorial</a>. While it was a very packed guide, there were still a few things we could not cover in it: how to display the game in fullscreen.</p>
<p>When you watch a video on YouTube, you have the option to also watch it on fullscreen. But did you know that the fullscreen feature isn't only for video elements?</p>
<p>In JavaScript, there’s a <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API">Fullscreen API</a>. And it’s surprisingly simple to use. Here's a quick demo of what we're about to implement. Let's see how it works.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/HunorMarton/embed/QWoRLXM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p>You can also <a target="_blank" href="https://www.youtube.com/watch?v=jX3mIQdQQ2w&amp;t=15s">watch this article as a video</a> on YouTube.</p>
<h2 id="heading-table-of-contents">Table of Contents:</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-enter-fullscreen-mode">How to Enter Fullscreen Mode</a></li>
<li><a class="post-section-overview" href="#heading-how-to-style-the-fullscreen">How to Style the Fullscreen</a></li>
<li><a class="post-section-overview" href="#heading-how-to-display-games-with-the-canvas-element-in-fullscreen">How to Display Games with the Canvas Element in Fullscreen</a></li>
<li><a class="post-section-overview" href="#heading-how-to-exit-fullscreen">How to Exit Fullscreen</a></li>
<li><a class="post-section-overview" href="#heading-how-to-code-a-fullscreen-icon-with-svg">How to Code a Fullscreen Icon with SVG</a></li>
<li><a class="post-section-overview" href="#heading-learn-more">Learn More</a></li>
</ul>
<h2 id="heading-how-to-enter-fullscreen-mode">How to Enter Fullscreen Mode</h2>
<p>Let’s say we have a simple website with some text. And at the bottom, we have a button that will display the text in full screen. We are going to refine the look of this button, but first, let’s get work on the main logic.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-21-at-18.20.51.png" alt="Image" width="600" height="400" loading="lazy">
<em>A simple website with some text and a Toggle Fullscreen button</em></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: Montserrat;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">500px</span>;
}
</code></pre>
<p>In the code above, we attached an event handler to the button in HTML. We can then implement the <code>toggleFullscreen</code> function logic in JavaScript.</p>
<p>In this function, all we have to do is call the <code>requestFullScreen</code> method on the <code>document</code>’s <code>documentElement</code> property. And that’s it:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toggleFullscreen</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">document</span>.documentElement.requestFullscreen();
}
</code></pre>
<p>If you click the button, your website will pop into fullscreen.</p>
<h2 id="heading-how-to-style-the-fullscreen">How to Style the Fullscreen</h2>
<p>Before we cover how to exit full screen and create a nice-looking toggle button, let’s see a few other things.</p>
<p>What you might notice right away is that, with more space, your content might get a bit lost on a full screen. Make sure you have a responsive styling that looks good on every screen size.</p>
<p>You can even style the layout specifically for fullscreen. In CSS you can set a media query that only applies the styling in case the <code>display-mode</code> is <code>fullscreen</code>. </p>
<p>For instance, you can change the font-size, or change the <code>background-color</code> to have a distinct look on full screen.</p>
<pre><code class="lang-css"><span class="hljs-keyword">@media</span> (<span class="hljs-attribute">display-mode:</span> fullscreen) {
  <span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f9bb86</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.2em</span>;
  }
}
</code></pre>
<h2 id="heading-how-to-display-games-with-the-canvas-element-in-fullscreen">How to Display Games with the Canvas Element in Fullscreen</h2>
<p>In this case, we want to make a game that uses the <code>canvas</code> element to be fullscreen – like the <a target="_blank" href="https://www.freecodecamp.org/news/how-to-draw-a-gorilla-with-javascript-on-html-canvas/">Gorillas</a> game – we also need to resize the <code>canvas</code> element to fit the whole screen.</p>
<p>In this case, we can use the <code>windows</code>’s <code>resize</code> event. The event is triggered both when we simply resize the browser window, and when we enter or exit fullscreen mode. </p>
<p>With the <code>resize</code> event, we can resize the <code>canvas</code> element to fit the whole screen, update the scaling, adjust any other properties we need to change on resize and redraw the whole scene.</p>
<pre><code class="lang-js"><span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"resize"</span>, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-comment">// Resize canvas element</span>
  canvas.width = <span class="hljs-built_in">window</span>.innerWidth;
  canvas.height = <span class="hljs-built_in">window</span>.innerHeight;

  <span class="hljs-comment">// Update scaling</span>
  <span class="hljs-comment">// . . .</span>

  <span class="hljs-comment">// Adjust size dependent properties</span>
  <span class="hljs-comment">// . . .</span>

  <span class="hljs-comment">// Redraw canvas</span>
  draw();
});
</code></pre>
<p>If you check the source code of the <a target="_blank" href="https://codepen.io/HunorMarton/pen/jOJZqvp">Gorillas game on CodePen</a>, you'll find similar steps.</p>
<h2 id="heading-how-to-exit-fullscreen">How to Exit Fullscreen</h2>
<p>Now that we know how to enter full screen, how do we exit from it?</p>
<p>By default, if you press the <code>Escape</code> key, the browser switches back to the normal view. In Google Chrome, you even get a notification at the top of the screen about this when you enter fullscreen mode.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-21-at-17.47.03-2.png" alt="Image" width="600" height="400" loading="lazy">
<em>Google Chrome shows a notification on top of the screen once you enter full screen</em></p>
<p>What if you want to exit fullscreen mode when you click the HTML button? Let’s change our button’s behavior to toggle fullscreen on or off.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toggleFullscreen</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">document</span>.fullscreenElement) {
    <span class="hljs-built_in">document</span>.documentElement.requestFullscreen();
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">document</span>.exitFullscreen();
  }
}
</code></pre>
<p>First, we start by checking if we are in fullscreen mode already. We can do this by checking the <code>document</code>’s <code>fullscreenElement</code> property. If it is undefined, then we enter fullscreen mode the same way we did before. And if we are already in fullscreen mode, then we can exit by calling the document’s <code>exitFullscreen</code> method. </p>
<p>It is really that simple. With a few lines of code, we can implement the logic for a fullscreen toggle button.</p>
<h2 id="heading-how-to-code-a-fullscreen-icon-with-svg">How to Code a Fullscreen Icon with SVG</h2>
<p>If you follow <a target="_blank" href="https://www.freecodecamp.org/news/author/hunor/">my tutorials</a>, you know I love creative coding, and <a target="_blank" href="https://www.freecodecamp.org/news/svg-tutorial-learn-to-code-images/">drawing from code</a>. So let’s update the look of our button, to make it look similar to what we have on YouTube.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-21-at-18.06.07.png" alt="Image" width="600" height="400" loading="lazy">
<em>Fullscreen icon</em></p>
<p>Let’s create an SVG image within our button. If you check the source code of YouTube, you will see that they also use an SVG.</p>
<p>Let’s define an SVG element within HTML. We'll set its size to 30 x 30 and define a <code>path</code> element:</p>
<pre><code class="lang-html">. . .

<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"toggleFullscreen()"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
      <span class="hljs-attr">stroke</span>=<span class="hljs-string">"black"</span>
      <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"3"</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
      <span class="hljs-attr">d</span>=<span class="hljs-string">"
        M 10, 2 L 2,2 L 2, 10
        M 20, 2 L 28,2 L 28, 10
        M 28, 20 L 28,28 L 20, 28
        M 10, 28 L 2,28 L 2, 20"</span>
    /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

. . .
</code></pre>
<p>To style the path, we set its color with the <code>stroke</code> property, set its <code>stroke-width</code>, and made sure that we didn't end up with a filled shape. SVG paths by default are filled, so we need to set explicitly that we don’t want to <code>fill</code> this shape.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-21-at-18.02.10.png" alt="Image" width="600" height="400" loading="lazy">
<em>Using the move-to and line-to commands within an SVG path</em></p>
<p>Then we defined the path with a few move-to and line-to commands. We can set these commands as a string in the <code>d</code> attribute of the path element.</p>
<p>We started with a move-to command: <code>M 10, 2</code>. The letter <code>M</code> signifies that we have a move-to command, and the 10 and 2 are the <code>x</code> and <code>y</code> coordinates of this command. We moved to the start of one of the four lines.</p>
<p>Then we continued the path with a line-to command that moves to the corner, and then with another line-to command. The line-to command works in a similar way. It starts with the letter <code>L</code>, then we set an <code>x, y</code> coordinates where the line should go to.</p>
<p>Then we did the same with the other corners. We move to the next line segment with another move-to command and draw a line with two more line-to commands.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Untitled.021.png" alt="Image" width="600" height="400" loading="lazy">
<em>Drawing a path on a Canvas element with JavaScript has some similarities to defining a path in SVG</em></p>
<p><strong>Note</strong>: If you read my <a target="_blank" href="https://www.freecodecamp.org/news/how-to-draw-a-gorilla-with-javascript-on-html-canvas/">previous tutorial</a> on how to make the gorillas game, then you might have noticed that we had something similar there. We also drew paths with move to and line to. Except that there we were drawn on a <code>canvas</code> element with JavaScript, and now we have the commands as a string within the HTML file.</p>
<h3 id="heading-how-to-toggle-the-icons-appearance">How to Toggle the Icon's Appearance</h3>
<p>Now the SVG is looking great, but what if we want to have a different look when we are in fullscreen mode? On YouTube, when we enter fullscreen mode, the button switches to a different icon.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-21-at-18.06.33.png" alt="Image" width="600" height="400" loading="lazy">
<em>The two faces of the fullscreen icon</em></p>
<p>You can do this in different ways. The easiest way is probably to define another path, within the same SVG element with a different look. Then make this path transparent by default. We are going to toggle the visibility of these two paths in JavaScript.</p>
<pre><code class="lang-html">. . .

<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"toggleFullscreen()"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
      <span class="hljs-attr">id</span>=<span class="hljs-string">"enter-fullscreen"</span>
      <span class="hljs-attr">stroke</span>=<span class="hljs-string">"black"</span>
      <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"3"</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
      <span class="hljs-attr">d</span>=<span class="hljs-string">"
        M 10, 2 L 2,2 L 2, 10
        M 20, 2 L 28,2 L 28, 10
        M 28, 20 L 28,28 L 20, 28
        M 10, 28 L 2,28 L 2, 20"</span>
    /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
      <span class="hljs-attr">id</span>=<span class="hljs-string">"exit-fullscreen"</span>
      <span class="hljs-attr">stroke</span>=<span class="hljs-string">"transparent"</span>
      <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"3"</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
      <span class="hljs-attr">d</span>=<span class="hljs-string">"
        M 10, 2 L 10,10 L 2, 10
        M 20, 2 L 20,10 L 28, 10
        M 28, 20 L 20,20 L 20, 28
        M 10, 28 L 10,20 L 2, 20"</span>
    /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

. . .
</code></pre>
<p>This second path is very similar to the previous one. Except that we used different coordinates for some of the line-to commands.</p>
<p>Then we set unique IDs for both of these paths, and we update our toggle function in JavaScript. In JavaScript, we get a reference to these paths by ID, and then in the toggle button’s event handler, we can switch the visibility of these elements back and forth.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> enterFullscreen = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"enter-fullscreen"</span>);
<span class="hljs-keyword">const</span> exitFullscreen = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"exit-fullscreen"</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toggleFullscreen</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">document</span>.fullscreenElement) {
    <span class="hljs-built_in">document</span>.documentElement.requestFullscreen();
    enterFullscreen.setAttribute(<span class="hljs-string">"stroke"</span>, <span class="hljs-string">"transparent"</span>);
    exitFullscreen.setAttribute(<span class="hljs-string">"stroke"</span>, <span class="hljs-string">"black"</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">document</span>.exitFullscreen();
    enterFullscreen.setAttribute(<span class="hljs-string">"stroke"</span>, <span class="hljs-string">"black"</span>);
    exitFullscreen.setAttribute(<span class="hljs-string">"stroke"</span>, <span class="hljs-string">"transparent"</span>);
  }
}
</code></pre>
<p>Now if you click this button, it toggles the fullscreen mode and changes its own appearance.</p>
<h2 id="heading-learn-more">Learn More</h2>
<p>If you want to learn more about SVGs, check out <a target="_blank" href="http://SVG-Tutorial.com">SVG-Tutorial.com</a> where you can find a lot of examples from the basics to more advanced levels. It’s a free site and you can also find the example that we discussed in this article.</p>
<p>To use the button to run a JavaScript game in full screen, check out the whole JavaScript Game Tutorial on how to remake the classic Gorillas game here on <a target="_blank" href="https://www.freecodecamp.org/news/how-to-draw-a-gorilla-with-javascript-on-html-canvas/">freeCodeCamp</a> or on <a target="_blank" href="https://www.youtube.com/watch?v=2q5EufbUEQk&amp;t=2337s">YouTube</a>. It’s a massive tutorial that covers things from drawing on an HTML Canvas element, to the entire game logic, from event handling, through the animation loop, hit detection, and even AI logic, for the enemy gorilla.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/2q5EufbUEQk" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p>You can subscribe to my channel for more JavaScript game development tutorials:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/undefined" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ SVG Tutorial – How to Code Images with 12 Examples ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever needed an icon for your website, but you couldn't quite find the right one? Or perhaps you wanted to have a simple diagram, but didn't want to learn a whole new library just for that? Well, good news – you can do all that and more witho... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/svg-tutorial-learn-to-code-images/</link>
                <guid isPermaLink="false">66c4c81d4173ed342943d0c4</guid>
                
                    <category>
                        <![CDATA[ image ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hunor Márton Borbély ]]>
                </dc:creator>
                <pubDate>Mon, 04 Dec 2023 12:29:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/10/Learn-SVG.001.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever needed an icon for your website, but you couldn't quite find the right one? Or perhaps you wanted to have a simple diagram, but didn't want to learn a whole new library just for that?</p>
<p>Well, good news – you can do all that and more without ever leaving your favorite code editor or using any third party tools or libraries.</p>
<p>Since HTML5, we can include the code of an SVG image inside an HTML document. We don’t even need to use the image tag that refers to a separate file. We can inline the code of an image right inside the HTML. We can do this because SVGs have a very similar syntax to HTML.</p>
<p>This opens up a lot of cool options. Suddenly we can access parts of an image from JavaScript or set the style from CSS. We can animate parts of an image from JavaScript or make it interactive. Or we can turn things around and generate graphics from code.</p>
<p>For more complicated images, you will still use a designer tool. But the next time you need a simple icon, a diagram, or animation, maybe you can code it yourself.</p>
<p>So how do SVGs look like under the surface? In this tutorial, we go through the source code of a few SVGs to cover the foundations.</p>
<p>The following examples are from <a target="_blank" href="https://svg-tutorial.com">svg-tutorial.com</a>. You can also <a target="_blank" href="https://youtu.be/kBT90nwUb_o">watch this article as a video</a> with even more fun examples.</p>
<h2 id="heading-the-svg-tag"><strong>The SVG tag</strong></h2>
<p>First, we have to talk about the <code>svg</code> tag itself. This tag contains the image elements and defines the frame of our image. It sets the inner size and the outer size of the image.</p>
<p>The <code>width</code> and <code>height</code> property define how much space the image takes up in the browser. There’s often a <code>viewBox</code> property as well. This defines a coordinate system for the elements inside the image. These two can be confusing because they both define a size.</p>
<p>You can think of the <code>width</code> and <code>height</code> of an SVG as an external size and the <code>viewBox</code> as an internal size.</p>
<p>The size defined by <code>width</code> and <code>height</code> is how the rest of HTML thinks of the image and how big it appears in the browser. The size defined by <code>viewBox</code> is how the image elements think of the image when they position themselves inside of it.</p>
<p>In the next example we have three SVGs that have the very same content. A <code>circle</code> element with the same center coordinate and same radius. They appear quite different, though.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.001-1.jpeg" alt="Learn-SVG.001-1" width="600" height="400" loading="lazy">
<em>The very same circle can appear different based on the size of the image and the <code>viewBox</code> property</em></p>
<p>At the example in the middle, the size defined by <code>width</code> and <code>height</code> matches the one defined by the <code>viewbox</code>. In the first example we see what happens if the <code>width</code> and <code>height</code> are smaller. The image simply shrinks down as all the coordinates and sizes defined within the image still align to the <code>viewbox</code>.</p>
<p>In the last example we see what happens if the <code>viewbox</code> is focusing on only part of the image. Things appear bigger in this case, but the actual size of the image is still defined by the <code>width</code> and <code>height</code> property.</p>
<p>The <code>viewBox</code> also defines the center of the coordinate system in which the image items place themselves.</p>
<p>The first two numbers define which coordinate should be at the top left corner of the image. Coordinates grow to the right and downwards. In this article, we will center the coordinate systems. The 0,0 coordinate will always be in the middle of the image.</p>
<p>One note before we start: while we can inline SVG images in an HTML file, that doesn’t mean we can freely combine any SVG tag with any HTML tag.</p>
<p>The SVG tag represents the frame of the image and every SVG element has to come within an SVG tag. The same is true in the opposite direction. HTML tags can’t be within an SVG tag, so we can’t have a div or a header tag inside an SVG. But don’t worry, there are similar tags available.</p>
<h2 id="heading-how-to-make-a-christmas-ornament-with-svg"><strong>How to Make a Christmas Ornament with SVG</strong></h2>
<p>Let’s start with a simple Christmas tree ornament. Here we'll only use simple shapes. A rectangle, and two circles.</p>
<p>We'll position and style these elements with attributes. For the circle, we define the center position and for the rectangle, we define the top left corner. These positions are always related to the coordinate system defined by the viewBox.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.002-1.jpeg" alt="Learn-SVG.002-1" width="600" height="400" loading="lazy">
<em>Christmas Ornament made out of circles and a rectangle. On the right you can see the coordinates we use in this example.</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200”&gt;
    &lt;circle cx="</span><span class="hljs-attr">0</span>" <span class="hljs-attr">cy</span>=<span class="hljs-string">"20"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"70"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#D1495B"</span> /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">circle</span>
      <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span>
      <span class="hljs-attr">cy</span>=<span class="hljs-string">"-75"</span>
      <span class="hljs-attr">r</span>=<span class="hljs-string">"12"</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
      <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#F79257"</span>
      <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span>
    /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-17.5"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-65"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"35"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"20"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#F79257"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Remember, we moved the center of the coordinate system to the middle of the image and the X-axis grows to the right and the Y-axis grows towards the bottom.</p>
<p>We also have presentational attributes that style our shapes. Unlike in HTML, we do not use <code>background-color</code> to set a color for a shape but we use the <code>fill</code> attribute.</p>
<p>And to set a border for a shape we use <code>stroke</code> and <code>stroke-width</code>. Note how we use the circle element both to draw a ring and a ball with different attributes.</p>
<h2 id="heading-how-to-build-a-christmas-tree-with-svg"><strong>How to Build a Christmas Tree with SVG</strong></h2>
<p>Let’s move on to a Christmas tree. We can’t always use basic shapes to assemble our image. A polygon is the simplest way to draw a freeform shape. Here we set a list of points that are connected with straight lines.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.003.jpeg" alt="Learn-SVG.003" width="600" height="400" loading="lazy">
<em>Christmas Tree made out of polygons and a rectangle</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 80,120 -80,120"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#234236"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,-40 60,60 -60,60"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#0C5C4C"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,-80 40,0 -40,0"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#38755B"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-20"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"120"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"brown"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>You might be wondering how we know before starting to code where our coordinates should be.</p>
<p>To be honest, this requires a bit of imagination. You can start with pen and paper and draw a draft first to get an estimate. Or you can just make a guess then adjust your values until it looks good.</p>
<h2 id="heading-how-to-make-a-gingerbread-figure-with-svg"><strong>How to Make a Gingerbread Figure with SVG</strong></h2>
<p>Let’s move on with a gingerbread figure. Since our SVG is living inside an HTML file now, we can assign CSS classes to each tag and move some attributes to CSS.</p>
<p>We can only move the presentation attributes, though. Position attributes and attributes that define the shape still have to stay in the HTML. But we can move colors, stroke, and font attributes to CSS.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.004.jpeg" alt="Learn-SVG.004" width="600" height="400" loading="lazy">
<em>Gingerbread figure example. On the right you can see how would it look if the <code>stroke-width</code> were one</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"gingerbread"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"body"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"-50"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"30"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"eye"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"-12"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"-55"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"3"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"eye"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"12"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"-55"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"3"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mouth"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-10"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-40"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"20"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"5"</span> <span class="hljs-attr">rx</span>=<span class="hljs-string">"2"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"limb"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"-40"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"-10"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-10"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"limb"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"-25"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-15"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"limb"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"25"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-15"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"-10"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"5"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"5"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.gingerbread</span> <span class="hljs-selector-class">.body</span> {
  <span class="hljs-attribute">fill</span>: <span class="hljs-number">#cd803d</span>;
}

<span class="hljs-selector-class">.gingerbread</span> <span class="hljs-selector-class">.eye</span> {
  <span class="hljs-attribute">fill</span>: white;
}

<span class="hljs-selector-class">.gingerbread</span> <span class="hljs-selector-class">.mouth</span> {
  <span class="hljs-attribute">fill</span>: none;
  <span class="hljs-attribute">stroke</span>: white;
  <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">2px</span>;
}

<span class="hljs-selector-class">.gingerbread</span> <span class="hljs-selector-class">.limb</span> {
  <span class="hljs-attribute">stroke</span>: <span class="hljs-number">#cd803d</span>;
  <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">35px</span>;
  <span class="hljs-attribute">stroke-linecap</span>: round;
}
</code></pre>
<p>We already saw the fill and some of the stroke properties, but here’s another one – the <code>stroke-linecap</code>. This can make our line cap round.</p>
<p>Note that the legs and the arms are simple lines here. If we remove the line cap and set a smaller <code>stroke-width</code>, then we can see that these are simple lines. But by setting a thick stroke width and a round line cap we can shape legs and arms for our figure.</p>
<p>Also note the <code>rx</code> property at the rectangle defining the mouth. This will make the edges round. You can think of it as <code>border-radius</code> if you like.</p>
<h2 id="heading-how-to-make-a-star-with-svg"><strong>How to Make a Star with SVG</strong></h2>
<p>Let’s move on to a star. A star is a simple shape, so we can define it as a bunch of polygons and set each point individually. But then we would need to know each coordinate.</p>
<p>Instead of that, we can just define one wing as a group, then repeat it five times with a rotation to get the star's shape. We use the <code>transform</code> attribute to set a rotation.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.005.jpeg" alt="Learn-SVG.005" width="600" height="400" loading="lazy">
<em>Star shape made out of transformed polygons. On the right we can see the coordianates of one arm of the star</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>      
  <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"translate(0 5)"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#EDD8B7"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 -36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#E5C39C"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(72)"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#EDD8B7"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 -36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#E5C39C"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(-72)"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#EDD8B7"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 -36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#E5C39C"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(144)"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#EDD8B7"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 -36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#E5C39C"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(-144)"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#EDD8B7"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0,0 -36,-50 0,-100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#E5C39C"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>In this example, each wing consists of two polygons. They need to be rotated the same way, so we can group them with a <code>g</code> tag and <code>rotate</code> them together.</p>
<p>You can think of the <code>g</code> tag as the <code>div</code> tag in HTML. On its own, it does not represent anything. But it can contain other elements and attributes defined on the group tag apply to its children.</p>
<p>Groups can be embedded. With the outer group we <code>translate</code> the whole star downwards by 5 units.</p>
<h2 id="heading-how-to-make-a-snowflake-with-svg"><strong>How to Make a Snowflake with SVG</strong></h2>
<p>Grouping elements is a nice trick, but we had to repeat the same code for each wing five times.</p>
<p>Instead of repeating the same code over and over again, we can also create a definition for a shape and reuse it by <code>id</code>. Here we define a branch of a snowflake then use it six times with different rotations.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.006.jpeg" alt="Learn-SVG.006" width="600" height="400" loading="lazy">
<em>Snowflake made out of reused image elements. On the right we can see the coordainates use for an arm</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">defs</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
      <span class="hljs-attr">id</span>=<span class="hljs-string">"branch"</span>
      <span class="hljs-attr">d</span>=<span class="hljs-string">"
        M 0 0 L 0 -90
        M 0 -20 L 20 -34
        M 0 -20 L -20 -34
        M 0 -40 L 20 -54
        M 0 -40 L -20 -54
        M 0 -60 L 20 -74
        M 0 -60 L -20 -74"</span>
      <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#E5C39C"</span>
      <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"5"</span>
    /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">defs</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#branch"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#branch"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(60)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#branch"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(120)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#branch"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(180)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#branch"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(240)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#branch"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"rotate(300)"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>The branch is defined as a <code>path</code>. The <code>path</code> is the most powerful SVG tag. We can define pretty much anything with paths, and if you open any SVG file, you will see mostly paths.</p>
<p>The shape of the path is defined by the <code>d</code> attribute. Here we define several drawing commands. A command always starts with a letter defining the command type and ends with a coordinate.</p>
<p>Here we only have the two most simple commands, move to (<code>M</code>) and line to (<code>L</code>). The <code>move to</code> command moves the cursor to a point without drawing a line and the <code>line to</code> command draws a straight line from the previous point.</p>
<p>A command always continues the previous command, so when we draw a line we only define the endpoint. The starting point will be the previous command’s endpoint.</p>
<p>This path is a bit unusual because there are several move to commands in it to draw the main branch and each side branch with the same path.</p>
<h2 id="heading-how-to-draw-a-forest-with-svg">How to Draw a Forest with SVG</h2>
<p>Rotation is not the only way we can generate images from simple shapes. In this example, we define a tree shape and then place it at various positions in different sizes to draw a forest.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/Screenshot-2023-11-30-at-21.21.23.png" alt="Image" width="600" height="400" loading="lazy">
<em>Forest made out of reused image elements</em></p>
<p>First, we create a background out of a rectangle and a circle. Then we define a tree shape from a simple polygon and a line.</p>
<p>Then we can reuse it in a similar way as we did in the snowflake example. We move it to the <code>defs</code> section, wrap it into a group element, set an ID for it, and then reuse it with the <code>use</code> element.</p>
<p>Here we also position the reused elements by setting an <code>x</code> and <code>y</code> coordinate and to add some perspective to the image we use the <code>scale</code> transformation.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">defs</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tree"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"-10,0 10,0 0 -50"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#38755b"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#778074"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">defs</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-100"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-100"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#F1DBC3"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"380"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"350"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#F8F4E8"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-30"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"25"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"scale(2)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-20"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"scale(1.2)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"40"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"scale(1.5)"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<h2 id="heading-how-to-make-a-curvy-tree-with-svg"><strong>How to Make a Curvy Tree with SVG</strong></h2>
<p>The path element becomes really powerful when we start using curves. One of them is the quadratic Bézier curve that not only defines an endpoint for a segment but also has a control point. The control point is an invisible coordinate towards which the line is bending, but not touching it.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.007.jpeg" alt="Learn-SVG.007" width="600" height="400" loading="lazy">
<em>Christmas Tree made using Quadratic Bézier curves</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"400"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -200 200 400"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
    <span class="hljs-attr">d</span>=<span class="hljs-string">"
      M 0 -80
      Q 5 -75 0 -70
      Q -10 -65 0 -60
      Q 15 -55 0 -50
      Q -20 -45 0 -40
      Q 25 -35 0 -30
      Q -30 -25 0 -20
      Q 35 -15 0 -10
      Q -40 -5 0 0
      Q 45 5 0 10
      Q -50 15 0 20
      Q 55 25 0 30
      Q -60 35 0 40
      Q 65 45 0 50
      Q -70 55 0 60
      Q 75 65 0 70
      Q -80 75 0 80
      Q 85 85 0 90
      Q -90 95 0 100
      Q 95 105 0 110
      Q -100 115 0 120
      L 0 140
      L 20 140
      L -20 140"</span>
    <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
    <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#0C5C4C"</span>
    <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"5"</span>
  /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>Here we have a series of quadratic Béziers curves (<code>Q</code>) where the control points get further and further away from the center of the tree as the path goes down.</p>
<h2 id="heading-how-to-make-a-bell-with-svg"><strong>How to Make a Bell with SVG</strong></h2>
<p>While the quadratic bezier curve (<code>Q</code>) is great when we want to bend a line, often it’s not flexible enough.</p>
<p>With a cubic Bezier (<code>C</code>), we not only one have one control point but two. The first control point sets the initial direction of the curve and the second one defines from which direction the curve should arrive to its endpoint.</p>
<p>If these directions match the directions of the line before and the line after the curve, then we have a smooth transition between the path segments.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.008.jpeg" alt="Learn-SVG.008" width="600" height="400" loading="lazy">
<em>With Cubic Bézier curves we can set two control points</em></p>
<p>The next example uses both quadratic and cubic Béziers to form a bell. Here the bottom of this bell is defined with straight lines. Then a quadratic Béziers starts the bell cloak. Next a cubic Bezier smoothly continues the quadratic bezier as it forms the top of the bell. Then we reach the bottom part with another quadratic bezier.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/Learn-SVG.001-2.jpeg" alt="Learn-SVG.001-2" width="600" height="400" loading="lazy">
<em>Bell example made out of different curves and straight lines</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"black"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"-45"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"7"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#4F6D7A"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#F79257"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
      <span class="hljs-attr">d</span>=<span class="hljs-string">"
        M -50 40
        L -50 50
        L 50 50
        L 50 40
        Q 40 40 40 10
        C 40 -60 -40 -60 -40 10   
        Q -40 40 -50 40"</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">"#FDEA96"</span>
    /&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<h2 id="heading-how-to-write-text-along-a-path">How to Write Text Along a Path</h2>
<p>Drawing shapes is not the only use case for paths. We can also use them to render text along an invisible path. We can define a path in the definitions section and use it in a <code>textPath</code> element to make the text go around the circle. Here we use arc again, but you can use any other path and the text will follow the stroke.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/Screenshot-2023-11-30-at-21.21.27.png" alt="Image" width="600" height="400" loading="lazy">
<em>With the <code>text-path</code> property we can make a text follow a path</em></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">defs</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text-arc"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M 0, 50 A 50 50 0 1 1 1,50"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">defs</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">text</span>
    <span class="hljs-attr">fill</span>=<span class="hljs-string">"#0c5c4c"</span>
    <span class="hljs-attr">font-family</span>=<span class="hljs-string">"Tahoma"</span>
    <span class="hljs-attr">font-size</span>=<span class="hljs-string">"0.77em"</span>
    <span class="hljs-attr">font-weight</span>=<span class="hljs-string">"bold"</span>
  &gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">textPath</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#text-arc"</span>&gt;</span>
      Happy Holidays! Happy Holidays! Happy Holidays!
    <span class="hljs-tag">&lt;/<span class="hljs-name">textPath</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">text</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<h2 id="heading-how-to-animate-an-svg-with-css">How to Animate an SVG with CSS</h2>
<p>To continue our forest example, we can add a snowing effect with a similar animation. We can animate the <code>transform</code> property from CSS.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/Learn-SVG.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Animation effect made with SVG and CSS</em></p>
<p>We extend our forest example with simple reusable snowflakes and add a bunch of them to the scene with various CSS classes to set some variation in speed and appearance. Then we add animation in CSS to make them look like falling snow. It’s a bit glitchy and not the most sophisticated animation, but you get the idea.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">defs</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tree"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"-10,0 10,0 0 -50"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#38755b"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#778074"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"big"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"5"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"white"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"small"</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"3"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"white"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">defs</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-100"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-100"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#F1DBC3"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"380"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"350"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#F8F4E8"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-30"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"25"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"scale(2)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-20"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"scale(1.2)"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"40"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#tree"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">transform</span>=<span class="hljs-string">"scale(1.5)"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake fast"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-50"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-20"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake fast opaque"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-40"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake fast"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-20"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake fast opaque"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-70"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-80"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow opaque"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#big"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-80"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow opaque"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#small"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-50"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#small"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-50"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-60"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow opaque"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#small"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"70"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">use</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#small"</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-80"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flake slow opaque"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.flake</span> {
  <span class="hljs-attribute">animation-duration</span>: inherit;
  <span class="hljs-attribute">animation-name</span>: snowing;
  <span class="hljs-attribute">animation-iteration-count</span>: infinite;
  <span class="hljs-attribute">animation-timing-function</span>: linear;
}

<span class="hljs-selector-class">.flake</span><span class="hljs-selector-class">.opaque</span> {
  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0.7</span>;
}

<span class="hljs-selector-class">.flake</span><span class="hljs-selector-class">.slow</span> {
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">5s</span>;
}

<span class="hljs-selector-class">.flake</span><span class="hljs-selector-class">.fast</span> {
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">3s</span>;
}

<span class="hljs-keyword">@keyframes</span> snowing {
  <span class="hljs-selector-tag">from</span> {
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translate</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">100px</span>);
  }
  <span class="hljs-selector-tag">to</span> {
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translate</span>(<span class="hljs-number">0</span>, <span class="hljs-number">100px</span>);
  }
}
</code></pre>
<h2 id="heading-how-to-make-a-clock-that-shows-the-actual-time">How to Make a Clock That Shows the Actual Time</h2>
<p>SVG elements can be manipulated from JavaScript the same way as any other HTML tag. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/Screenshot-2023-11-30-at-21.21.16.png" alt="Image" width="600" height="400" loading="lazy">
<em>Clock example made with SVG and JavaScript</em></p>
<p>In this example, we are using a short code snipped to show the actual time on a clock. We access the hour and minute hands in JavaScript with <code>getElementById</code> then set their <code>transform</code> attribute with a rotation that reflects the current time. Below you see the actual SVG showing the current time.</p>
<div>
    

  

  

  

  
    
    
  

</div>

<p>For a more detailed tutorial on how to make a clock with SVG and JavaScript, check out <a target="_blank" href="https://www.freecodecamp.org/news/svg-javascript-tutorial/">How to Code an Animated Watch</a>.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"-100"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"-100"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#CD803D"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"55"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#FCCE7B"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"white"</span> /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span>
    <span class="hljs-attr">r</span>=<span class="hljs-string">"45"</span>
    <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#B6705F"</span>
    <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"6"</span>
    <span class="hljs-attr">stroke-dasharray</span>=<span class="hljs-string">"6 17.56194490192345"</span>
    <span class="hljs-attr">stroke-dashoffset</span>=<span class="hljs-string">"3"</span>
    <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
  /&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#5f4c6c"</span> <span class="hljs-attr">stroke-linecap</span>=<span class="hljs-string">"round"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"hours"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-20"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"minutes"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-35"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"6"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"DOMContentLoaded"</span>, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> hoursElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"hours"</span>);
  <span class="hljs-keyword">const</span> minutesElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"minutes"</span>);

  <span class="hljs-keyword">const</span> hour = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getHours() % <span class="hljs-number">12</span>;
  <span class="hljs-keyword">const</span> minute = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getMinutes();

  hoursElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">12</span>) * hour}</span>)`</span>);
  minutesElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * minute}</span>)`</span>);
});
</code></pre>
<h2 id="heading-how-to-make-a-data-driven-diagram-with-svg-and-react">How to make a Data-driven Diagram with SVG and React</h2>
<p>SVGs also work well with frontend libraries. Here’s an example of a React component that generates a data-driven diagram. </p>
<p>In this example we have two things. We are generating a list of rectangles to create a column diagram based on some arbitrary data. And we also generate a series of coordinates for a polyline.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/Screenshot-2023-11-30-at-21.21.32.png" alt="Image" width="600" height="400" loading="lazy">
<em>We can use JavaScript to generate a Data-Driven Diagram</em></p>
<p>For simple use cases, you can code your own diagram like this. But if you need more complex diagrams then check out the <a target="_blank" href="https://d3js.org/">D3 library</a>. The D3 library uses SVG under to hood to create all sorts of diagrams.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Diagram</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> dataPoints = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">7</span>, <span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>];
  <span class="hljs-keyword">const</span> sineWave = <span class="hljs-built_in">Array</span>.from({ <span class="hljs-attr">length</span>: <span class="hljs-number">115</span> })
    .map(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> <span class="hljs-string">`<span class="hljs-subst">${index - <span class="hljs-number">55</span>}</span>,<span class="hljs-subst">${<span class="hljs-built_in">Math</span>.sin(index / <span class="hljs-number">20</span>) * <span class="hljs-number">20</span> + <span class="hljs-number">10</span>}</span>`</span>)
    .join(<span class="hljs-string">" "</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
      {dataPoints.map((dataPoint, index) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">rect</span>
          <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>
          <span class="hljs-attr">x</span>=<span class="hljs-string">{index</span> * <span class="hljs-attr">20</span> <span class="hljs-attr">-</span> <span class="hljs-attr">55</span>}
          <span class="hljs-attr">y</span>=<span class="hljs-string">{50</span> <span class="hljs-attr">-</span> <span class="hljs-attr">dataPoint</span> * <span class="hljs-attr">10</span>}
          <span class="hljs-attr">width</span>=<span class="hljs-string">"15"</span>
          <span class="hljs-attr">height</span>=<span class="hljs-string">{dataPoint</span> * <span class="hljs-attr">10</span>}
          <span class="hljs-attr">fill</span>=<span class="hljs-string">"#CD803D"</span>
        /&gt;</span>
      ))}

      <span class="hljs-tag">&lt;<span class="hljs-name">polyline</span> <span class="hljs-attr">points</span>=<span class="hljs-string">{sineWave}</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"black"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"5"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-next-steps-making-svgs-interactive-with-javascript"><strong>Next Steps – Making SVGs Interactive with JavaScript</strong></h2>
<p>Under the hood, SVGs can be quite confusing at first. Lots of coordinates, letters and strange parameters. Once you understand their foundations, though, you can use them to your advantage and start coding images.</p>
<p>And we are just getting started. Adding JavaScript to the mix will introduce a whole new level.</p>
<p>For more examples check out <a target="_blank" href="https://svg-tutorial.com">svg-tutorial.com</a> or my <a target="_blank" href="https://www.youtube.com/watch?v=kBT90nwUb_o">YouTube tutorial</a> with 12 more examples on how to use SVGs for your next project!</p>
<p><a target="_blank" href="http://svg-tutorial.com">Embedded content</a></p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/kBT90nwUb_o" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h3 id="heading-subscribe-for-more-tutorials-on-web-development"><strong>Subscribe for more tutorials on Web Development:</strong></h3>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/undefined" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use the CSS Box Model and Style SVG Images ]]>
                </title>
                <description>
                    <![CDATA[ By Njoku Samson Ebere Every programmer who wants to write clean CSS and build great user interfaces should understand the CSS Box Model.  Before I understood the foundations of CSS, I would often write unnecessary styles for margins and padding.    C... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-css-box-model-and-style-svg-images/</link>
                <guid isPermaLink="false">66d84fab39c4dccc43d4d4a1</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 03 Apr 2023 17:29:13 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/pexels-tiger-lily-4483610--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Njoku Samson Ebere</p>
<p>Every programmer who wants to write clean CSS and build great user interfaces should understand the CSS <strong>Box Model</strong>. </p>
<p>Before I understood the foundations of CSS, I would often write unnecessary styles for <strong>margins</strong> and <strong>padding</strong>.   </p>
<p>CSS Box Model forms the basis for styling any element on a website. So understanding the concept will help you target HTML elements and write fewer lines of code that are clean and easy to maintain.  </p>
<p>This article will teach how to target the properties of any HTML element and apply the right style. You will also learn what SVG images are and how to style them.</p>
<h2 id="heading-what-is-the-css-box-model">What is the CSS Box Model?</h2>
<p>The CSS Box Model is the relationship between an <strong>HTML element</strong> and the spaces around it – its <strong>padding, border,</strong> and <strong>margin</strong>.</p>
<ul>
<li>Padding is the space that surrounds a given HTML element</li>
<li>The border is the space that surrounds the padding</li>
<li>Margin is the space that surrounds the border</li>
</ul>
<p>In other words, the <strong>padding</strong> surrounds the HTML element, the <strong>border</strong> encloses the padding, and the <strong>margin</strong> houses the border. The image below illustrates it all:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/jsm663u6fvzpq4b5buzr.jpg" alt="Image" width="960" height="720" loading="lazy">
<em>CSS Box Model Illustration</em></p>
<p>Now, enough of the theory – let's translate the diagram into code.</p>
<h2 id="heading-lets-code">Let's Code</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/BWIpVT-QHbA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p>You can get your starter code <a target="_blank" href="https://github.com/EBEREGIT/box-model-tutorial/tree/starter-code">here</a> by cloning the repo.  </p>
<p>Otherwise you'll need to create a new HTML file and copy the following code into the file (if you don't want to clone the repo or don't know how to use Git).</p>
<pre><code>&lt;!DOCTYPE html&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</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">title</span>&gt;</span>Box Model Tutorial<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">style</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">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://www.w3schools.com/howto/img_avatar2.png"</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></span>
</code></pre><p>The code above is a basic HTML boiler plate. It contains a <code>title</code> and an <code>img</code> element. I got the image from <a target="_blank" href="https://www.w3schools.com/">w3schools</a>.</p>
<p>Load the file in a browser, and you should get the following result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/0qm111t5yctvgkks36ea.JPG" alt="Image" width="496" height="724" loading="lazy">
<em>Preview with margin</em></p>
<p>Notice that the image came with a default space around it. That is the default margin. Let's remove it.   </p>
<p>Enter the following CSS into the <code>style</code> tag of your HTML file:</p>
<pre><code>   body{
     <span class="hljs-attr">margin</span>: <span class="hljs-number">0</span>;
   }
</code></pre><p>This code removes all the margins around the image. Notice that there is no longer a space between the edges of the browser and the content in the image below.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/ruru86w57zd4k512kv2o.JPG" alt="Image" width="500" height="724" loading="lazy">
<em>Preview without margin</em></p>
<p>Now let's get down to the main business.  </p>
<p>Add a border with the following code:</p>
<pre><code>  img{
    <span class="hljs-attr">border</span>: <span class="hljs-number">5</span>px solid red;
  }
</code></pre><p>This code adds a red border around the content. The border's width is 5 pixels.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/plb3mv3dxg4oju57ok1r.JPG" alt="Image" width="501" height="719" loading="lazy">
<em>Preview with border</em></p>
<p>And add some padding with the code below:</p>
<pre><code>  img{
    <span class="hljs-attr">border</span>: <span class="hljs-number">5</span>px solid red;
    padding: <span class="hljs-number">20</span>px;
  }
</code></pre><p>The code above now creates a space between the content and the border. That space is called <strong>padding.</strong> It is 20 pixels wide.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/01zjji5x4ypu18rpo5u9.JPG" alt="Image" width="501" height="728" loading="lazy">
<em>Preview with padding</em></p>
<p>Type the following code to add a margin:</p>
<pre><code>  img{
    <span class="hljs-attr">border</span>: <span class="hljs-number">5</span>px solid red;
    padding: <span class="hljs-number">20</span>px;
    margin: <span class="hljs-number">20</span>px;
  }
</code></pre><p>You will recall that we removed the margin when we set the margin to 0px. The code above now adds our custom margin that is 20 pixels wide. It creates a space between the red border and the browser edges.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/7aowqpgt6or44ivtzz0y.JPG" alt="Image" width="502" height="727" loading="lazy">
<em>Preview with border</em></p>
<p>You can get the code for this section <a target="_blank" href="https://github.com/EBEREGIT/box-model-tutorial">here</a> or copy the code below:</p>
<pre><code>&lt;!DOCTYPE html&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</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">title</span>&gt;</span>Box Model Tutorial<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-tag">body</span>{
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
        }

        <span class="hljs-selector-tag">img</span>{
            <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid red;
            <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span>;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</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">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://www.w3schools.com/howto/img_avatar2.png"</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></span>
</code></pre><p>The project is live here – <a target="_blank" href="https://eberegit.github.io/box-model-tutorial/">https://eberegit.github.io/box-model-tutorial/</a>.  </p>
<p>YESSSS! We did it. We made it!</p>
<h2 id="heading-how-to-style-svg-images">How to Style SVG Images</h2>
<p>Now that you understand how the CSS box model works, let's try styling an important HTML element – an SVG. It is a bit different from other elements but the principles are the same.</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Scalable_Vector_Graphics">SVG images</a> are lightweight resources that can help speed up your applications. This section will help you look at SVGs from a friendly perspective and build better applications.  </p>
<p>SVG files consist of elements such as the <code>&lt;g&gt;</code> and <code>&lt;path&gt;</code> elements. You do not have to memorize them. You can inspect the SVG image in your browser to see the different parts and how you can target the element you want.  </p>
<p>These elements have a <code>border</code> (represented as <strong>stroke</strong>) and <code>background-color</code> (<strong>fill</strong>) attributes. We will be looking into those in a bit.  </p>
<p>You can download the SVG image for this tutorial <a target="_blank" href="https://freesvg.org/volleyball-player-caricature">here</a>. And you can get the starter project <a target="_blank" href="https://github.com/EBEREGIT/styling-svg-images/tree/starter-project">here</a>.  </p>
<p>In the starter project, I have gone ahead and:</p>
<ol>
<li>Added the downloaded <code>SVG</code> file to the project directory.</li>
<li>Created an <code>index.html</code> file.</li>
<li>Copied and pasted the SVG code from the SVG file into the <code>index.html</code> file.</li>
<li>Created a <code>style.css</code> file with the following code to <code>center</code> all contents:</li>
</ol>
<pre><code>body{
    text-align: center;
}
</code></pre><p>If you run the project in a browser, you should have the following output:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/l55mafu2iaj3xpcsrd3q.JPG" alt="Image" width="503" height="729" loading="lazy">
<em>Starter Project</em></p>
<h2 id="heading-how-to-style-the-svg-image">How to Style the SVG Image</h2>
<h3 id="heading-make-the-image-responsive">Make the Image Responsive</h3>
<p>Change the <code>width</code> and <code>height</code> properties of the SVG element to 50% and 100vh, respectively, in the <code>index.html</code> file to make the image responsive like so:</p>
<pre><code>&lt;svg
        version=<span class="hljs-string">"1.1"</span>
        id=<span class="hljs-string">"volleyball"</span>
        xmlns=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
        <span class="hljs-attr">xmlns</span>:xlink=<span class="hljs-string">"http://www.w3.org/1999/xlink"</span>
        x=<span class="hljs-string">"0px"</span>
        y=<span class="hljs-string">"0px"</span>
        width=<span class="hljs-string">"50%"</span>
        height=<span class="hljs-string">"100vh"</span>
        viewBox=<span class="hljs-string">"0 -0.5 167 267"</span>
        enable-background=<span class="hljs-string">"new 0 -0.5 167 267"</span>
        <span class="hljs-attr">xml</span>:space=<span class="hljs-string">"preserve"</span>
      &gt;

      ...

&lt;/svg&gt;
</code></pre><p>Now, your output should be like this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/y326rifv946zsphrabtq.JPG" alt="Image" width="505" height="728" loading="lazy">
<em>The image now takes up the whole page and it is responsive too</em></p>
<p>Looking sharp, let's move on!</p>
<h3 id="heading-how-to-change-the-border-color">How to Change the Border Color</h3>
<p>The SVG image we are using in this tutorial contains a <code>&lt;g&gt;</code> element, <code>&lt;path&gt;</code> element, and <code>&lt;circle&gt;</code> element.  </p>
<p>We will target the whole <code>path</code> and <code>circle</code> at once and give them <code>border-colors</code> and <code>width</code> with the following code:</p>
<pre><code>path{
    <span class="hljs-attr">stroke</span>: red;
    stroke-width: <span class="hljs-number">2</span>px;
}

circle{
    <span class="hljs-attr">stroke</span>: darkblue;
}
</code></pre><p>Check if your output matches mine below:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/sa8p8g8815lm6u2hb5fy.JPG" alt="Image" width="504" height="727" loading="lazy">
<em>Stroke Added</em></p>
<p>Notice that we changed the <code>path</code>'s border color to <code>red</code> with a reduced <code>width</code>. Then we changed the <code>circle</code>'s border color to dark blue. How Awesome!</p>
<h3 id="heading-how-to-change-the-background">How to Change the Background</h3>
<p>We could change the background color for the <code>paths</code> and <code>circles</code> as we did with the <code>border</code>, but let's do something different. We are going to give each <code>path</code> and <code>circle</code> different background colors.  </p>
<p>Each <code>path</code> and <code>circle</code> has a unique <code>id</code>.  </p>
<p>Let's add the following code to our <code>styles.css</code> file to give the <code>path</code> and <code>circle</code> different background colors with the following code:</p>
<pre><code>#torso{
    <span class="hljs-attr">fill</span>: blue;
}

#left_leg{
    <span class="hljs-attr">fill</span>: green;
}

#left_arm{
    <span class="hljs-attr">fill</span>: indigo;
}

#right_arm{
    <span class="hljs-attr">fill</span>: yellow;
}

#ball{
    <span class="hljs-attr">fill</span>: hotpink;
}

#head{
    <span class="hljs-attr">fill</span>: olive;
}
</code></pre><p>I now have a clown-like volleyball player:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/6yrdytn4p7hzm3qib64k.JPG" alt="Image" width="508" height="729" loading="lazy">
<em>Fill Added</em></p>
<p>If your clown looks like mine, then let's proceed...</p>
<h3 id="heading-how-to-add-a-hover-attribute">How to Add a Hover Attribute</h3>
<p>To add a hover property, add the following code to the <code>styles.css</code> file:</p>
<pre><code>path:hover{
    <span class="hljs-attr">stroke</span>: black;
    stroke-width: <span class="hljs-number">10</span>px;
}

<span class="hljs-attr">circle</span>:hover{
    <span class="hljs-attr">stroke</span>: black;
    stroke-width: <span class="hljs-number">10</span>px;
}
</code></pre><p>My output is the GIF image you see below:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/arr2k0echobmtrgo01ro.gif" alt="Image" width="372" height="552" loading="lazy">
<em>Hover added</em></p>
<p>With the clown looking that way, let's do one more thing.</p>
<h3 id="heading-how-to-add-achor-tags">How to Add Achor Tags</h3>
<p>Now we're going to wrap each <code>path</code> and <code>circle</code> with an anchor tag.  </p>
<p>Give the <code>anchor</code> tag a <code>title</code> (represented as <code>xlink:title</code>) and a href (as <code>xlink:href</code>) attribute in the following manner:</p>
<pre><code>&lt;a xlink:title=<span class="hljs-string">"a title"</span> xlink:href=<span class="hljs-string">"a url"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">path</span>&gt;</span> codes here <span class="hljs-tag">&lt;/<span class="hljs-name">path</span>&gt;</span></span>
&lt;/a&gt;

<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">xlink:title</span>=<span class="hljs-string">"a title"</span> <span class="hljs-attr">xlink:href</span>=<span class="hljs-string">"a url"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span>&gt;</span> codes here <span class="hljs-tag">&lt;/<span class="hljs-name">circle</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span></span>
</code></pre><p>Go ahead and use any <code>title</code> and <code>URL</code> of your choice. I added my social media profiles and other websites I built. Checkout mine below:  </p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_D592C23061BDAFBA9E611AEDC8048F685A5679FCF2C57746CD5AE80A3DAD15B0_1680184339979_ezgif.com-video-to-gif.gif" alt="Image" width="600" height="372" loading="lazy">
<em>Final Result</em></p>
<p>Apart from changing the stroke width, we can see <code>labels</code>, and each part of the <code>image</code> is linked to a different website.  </p>
<p>The <code>xlink:title</code> and <code>xlink:href</code> attributes add a <code>label</code> and <code>URL</code> individually.  </p>
<p>All codes for this section is <a target="_blank" href="https://github.com/EBEREGIT/styling-svg-images">here</a>. The project is live <a target="_blank" href="https://eberegit.github.io/styling-svg-images/">here</a>  </p>
<p><strong>YOU ROCK!</strong></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>CSS gets a bit easier when you understand the basics. This article aimed to teach them to you.   </p>
<p>You have learned the difference between <strong>margin</strong> and <strong>padding</strong>. You also saw how they are related. Once you understand this, moving elements around the page becomes easy. In all, use padding to move an element within its container or axis and use a margin to create space between elements.  </p>
<p>I enjoyed dissecting that SVG image with you. You now know how to work with any SVG image that comes your way. They might differ, but the principle is understanding how their elements are named. Then you can target those elements in your styling.  </p>
<p>Try out more SVG images and see how they come out.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Make a Clickable SVG Map With HTML and CSS ]]>
                </title>
                <description>
                    <![CDATA[ SVG, or Scalable Vector Graphics, is a versatile image format that you can use in a wide range of applications, from web design to print media and data visualization.  The scalability, small file size, and accessibility of SVG images make this format... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-clickable-svg-map-html-css/</link>
                <guid isPermaLink="false">66c17cc831d965ba683261bb</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Rufai Mustapha ]]>
                </dc:creator>
                <pubDate>Thu, 09 Mar 2023 19:43:56 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/amy-youtube--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>SVG, or Scalable Vector Graphics, is a versatile image format that you can use in a wide range of applications, from web design to print media and data visualization. </p>
<p>The scalability, small file size, and accessibility of SVG images make this format a popular choice among designers and developers. </p>
<p>Maps are a powerful tool for data visualization, providing a visual representation of data that can help viewers understand patterns and relationships that may not be evident in other types of visualizations.  </p>
<p>In this article, you will learn how to make interactive maps using HTML and CSS. These maps will remain light-weight because we'll be using SVGs.</p>
<p>If you have basic knowledge of HTML and CSS you'll be able to understand this tutorial.</p>
<h3 id="heading-heres-what-well-cover">Here's what we'll cover:</h3>
<p>I. Introduction</p>
<ul>
<li>What Is an SVG Map?</li>
<li>What Are the Benefits of an SVG Map?</li>
</ul>
<p>II. How to Set Up the HTML File</p>
<ul>
<li>How to Create the Basic HTML Structure</li>
<li>How to Import the SVG Map File</li>
<li>How to Set Up the <code>div</code> Container for the Map</li>
</ul>
<p>III. How to Add Interactivity with CSS</p>
<ul>
<li>How to Style the SVG Map</li>
<li>How to Add Hover Effects</li>
<li>How to Add Click Effects</li>
</ul>
<p>IV. How to Add Functionality</p>
<ul>
<li>How to Link Areas of the Map to External Webpages</li>
</ul>
<p>V. Conclusion</p>
<ul>
<li>Recap of Steps to Make a Clickable SVG Map</li>
<li>Final Thoughts and Suggestions for Further Customization</li>
</ul>
<h2 id="heading-what-is-an-svg-map">What Is an SVG Map?</h2>
<p>SVG maps are vector-based, meaning that the map elements are defined by mathematical equations rather than pixels. This allows you to scale them up or down without losing quality.</p>
<h2 id="heading-what-are-the-benefits-of-an-svg-map">What Are the Benefits of an SVG Map?</h2>
<p>Some benefits of using SVG maps include:</p>
<ol>
<li>Scalability: SVG maps can be scaled up or down without losing quality, which makes them ideal for use in responsive web design or for printing at different sizes.</li>
<li>Customizability: SVG maps can be easily customized to suit specific needs, such as changing colors, adding labels, or modifying the size and shape of map elements.</li>
<li>Interactivity: SVG maps can be made interactive through the use of JavaScript and CSS, allowing viewers to hover over or click on specific elements to see more detailed information.</li>
<li>Accessibility: SVG maps can be read by assistive technologies such as screen readers, making them more accessible to people with disabilities.</li>
<li>Cross-platform compatibility: SVG maps can be displayed on a wide range of devices and browsers, making them a versatile choice for web-based applications.</li>
</ol>
<h3 id="heading-example-of-svg-maps-in-the-wild">Example Of SVG Maps In the Wild</h3>
<p>Stears and The BBC used SVG Maps to visualize election data. Users can click on a region to see the distribution of ballots cast. See <a target="_blank" href="https://www.stears.co/elections/2023/president/">here</a> for more info.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/RCUkQhGGxxM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h3 id="heading-what-are-we-building">What  Are We Building?</h3>
<p>In this tutorial, we are building <code>Tour Afrique</code>, an interactive map of Africa.</p>
<p>It has 2 features:</p>
<ul>
<li>Users can hover to see the name of a country.</li>
<li>Users can click on a country to see more information on Wikipedia. </li>
</ul>
<h2 id="heading-how-to-build-our-interactive-map-of-africa">How to Build Our Interactive Map of Africa</h2>
<h3 id="heading-1-get-the-svg-map-of-africa">1. Get the SVG Map of Africa</h3>
<p>You can create an SVG map using vector graphics software such as <a target="_blank" href="https://www.youtube.com/watch?v=GsojLuJpe_0">Adobe Illustrator</a> or Inkscape, or with code using libraries such as D3.js or Leaflet.    </p>
<p>You can also download SVG maps from Wikipedia using a creative-commons license. Click <a target="_blank" href="https://upload.wikimedia.org/wikipedia/commons/2/24/West-Africa.svg">here</a> for the map we'll use in this tutorial.  </p>
<h3 id="heading-2-create-the-basic-html-structure">2. Create the basic HTML structure</h3>
<p>We need a basic HTML structure, which will look like this:</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">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">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">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">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">span</span>&gt;</span> Tour Afrique <span class="hljs-tag">&lt;/<span class="hljs-name">span</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>This HTML will give us the following result so far:</p>
<p>&lt;!DOCTYPE html&gt;</p>


    
    
    
    Document


    <span> Tour Afrique </span>



<h3 id="heading-3-import-the-svg-map-file">3. Import the SVG map file</h3>
<p>To import an SVG map file to an HTML document, you can use the <code>&lt;object&gt;</code>, <code>&lt;img&gt;</code>, or <code>&lt;svg&gt;</code> tag.  </p>
<p>We'll do so using the <code>&lt;svg&gt;</code> tag:</p>
<p>First, open your HTML file in a text editor. Then add the downloaded SVG map to the section where you want it to appear:</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">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">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">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">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">section</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
        <span class="hljs-attr">xmlns:dc</span>=<span class="hljs-string">"http://purl.org/dc/elements/1.1/"</span>
        <span class="hljs-attr">xmlns:cc</span>=<span class="hljs-string">"http://creativecommons.org/ns#"</span>
        <span class="hljs-attr">xmlns:rdf</span>=<span class="hljs-string">"http://www.w3.org/1999/02/22-rdf-syntax-ns#"</span>
      &gt;</span>
        ... all the country paths are here ...
      <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</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>&lt;!DOCTYPE html&gt;</p>

  
    
    
    
    Document
  
  
    <section>
      
        
          image/svg+xml
        
        
        
        
          
            
            
            
            

            

            
              1000 km
            

            
              1000 km
            

            

            

            

            

            
              1000 mi
            

            
              1000 mi
            

            

            

            

            

            
              0
            

            
              0
            

            

            

            

            

            
              0
            

            
              0
            

            

            
          
          
          
            
            
            
          
          
          
          
            
            
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
        
      
    </section>
  



<h3 id="heading-4-nest-each-path-in-an-anchor-tag">4. Nest each path in an anchor tag</h3>
<p>There are several <code>path</code> tags in our SVG. Each of them describes a country. We want to hover on each country to see the name of the country. And on clicking that country, we should be taken to its Wikipedia page.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">xlink:title</span>=<span class="hljs-string">"Mali"</span> <span class="hljs-attr">xlink:href</span>=<span class="hljs-string">"https://en.wikipedia.org/wiki/Mali"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"malic"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
          <span class="hljs-attr">inkscape:connector-curvature</span>=<span class="hljs-string">"0"</span>
          <span class="hljs-attr">d</span>=<span class="hljs-string">"..."</span>
          <span class="hljs-attr">id</span>=<span class="hljs-string">"Mali"</span>
          /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<p>In the code above, we've nested the <code>path</code> into an <code>anchor</code> tag. The <code>anchor</code> tag has the attribute <code>title</code> which shows the country name on-hover. Also, it has <code>href</code> which allows us to visit the Wikipedia page on clicking that link. <code>target="_blank"</code> makes sure that the link opens in a new tab.  </p>
<p>You may notice the <code>xlink</code> prefix on the attributes. This supports earlier browser versions. You can use the deprecated <code>xlink:href</code> attribute as a fallback in addition to the href attribute. See more info <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href">here</a>.</p>
<h3 id="heading-5-how-to-style-the-svg-map">5. How to style the SVG map</h3>
<p>We can style our SVG map using CSS. </p>
<p>The common attributes in SVG path styles are <code>fill</code>, <code>stroke</code>, and <code>stroke-width</code>. For example, if you draw a rectangular path, then you can use <code>fill</code> to make it blue.</p>
<pre><code class="lang-cs">&lt;svg&gt;
  &lt;style&gt;
    rect {
      fill: blue;
    }
  &lt;/style&gt;
  &lt;rect x=<span class="hljs-string">"10"</span> y=<span class="hljs-string">"10"</span> width=<span class="hljs-string">"500"</span> height=<span class="hljs-string">"100"</span>&gt;&lt;/rect&gt;
&lt;/svg&gt;
</code></pre>

  
  



<p>Now, we can give select countries different colors. Also, we should have some sort of visual feedback when hovering over a country. </p>
<pre><code class="lang-css"> &lt;<span class="hljs-selector-tag">style</span>&gt;
      <span class="hljs-selector-id">#mapsection</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
        <span class="hljs-attribute">height</span>: auto;
        <span class="hljs-attribute">fill</span>: <span class="hljs-number">#ccc</span>;
        <span class="hljs-attribute">stroke</span>: <span class="hljs-number">#333</span>;
        <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">2</span>;
      }
      <span class="hljs-selector-id">#mali</span> {
        <span class="hljs-attribute">fill</span>: brown;
      }

      <span class="hljs-selector-id">#Benin</span> {
        <span class="hljs-attribute">fill</span>: <span class="hljs-number">#00baba</span>;
      }

      <span class="hljs-selector-id">#Nigeria</span> {
        <span class="hljs-attribute">fill</span>: yellow;
      }

      <span class="hljs-selector-id">#Zimbabwe</span> {
        <span class="hljs-attribute">fill</span>: brown;
      }

      <span class="hljs-selector-id">#Zambia</span> {
        <span class="hljs-attribute">fill</span>: green;
      }

      <span class="hljs-selector-id">#Somalia</span> {
        <span class="hljs-attribute">fill</span>: darkseagreen;
      }

      <span class="hljs-selector-id">#Mali</span> {
        <span class="hljs-attribute">fill</span>: gray;
      }

      <span class="hljs-selector-tag">path</span> {
        <span class="hljs-attribute">fill</span>: <span class="hljs-number">#1da1f2</span>;
      }
      <span class="hljs-selector-tag">path</span><span class="hljs-selector-pseudo">:hover</span> {
        <span class="hljs-attribute">fill</span>: purple;
        <span class="hljs-attribute">stroke</span>: red;
        <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">3px</span>;
        <span class="hljs-attribute">transition</span>: fill <span class="hljs-number">0.4s</span>;
      }
    &lt;/<span class="hljs-selector-tag">style</span>&gt;
</code></pre>
<p>The above block of CSS code styles elements on our map. Here is an explanation of each rule:</p>
<ul>
<li><code>#mapsection</code>: This targets SVG elements with the ID <em>mapsection</em>. It sets the width to 50%, height to auto, and stroke properties for the element.</li>
<li><code>#mali</code>, <code>#Benin</code>, <code>#Nigeria</code>, <code>#Zimbabwe</code>, <code>#Zambia</code>, and <code>#Somalia</code>: These target specific <code>path</code> elements with IDs corresponding to their country names. They set the <em>fill</em> color for each country.</li>
<li><code>path</code>: This targets all <code>path</code> elements on the webpage. It sets the fill color to #1da1f2.</li>
<li><code>path:hover</code>: This targets all <code>path</code> elements on the webpage when they are hovered over with the mouse. It sets the fill color to purple, stroke color to red, and stroke width to 3px. It also sets a transition effect for the fill color change.</li>
</ul>
<p>&lt;!DOCTYPE html&gt;</p>

  
    
    
    
    Document
    
  
  
    
      

      
        
          
          
          
          

          

          
            1000 km
          

          
            1000 km
          

          

          

          

          

          
            1000 mi
          

          
            1000 mi
          

          

          

          

          

          
            0
          

          
            0
          

          

          

          

          

          
            0
          

          
            0
          

          

          
        

        
        
          

          

          
        

        
        
        
          
          
        
        <a href="https://en.wikipedia.org/wiki/Benin" id="Benin" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Botsuana" id="Botsuana" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Burundi" id="Burundi" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Burkina_Faso" id="Burkina_Faso" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Democratic_Republic_of_the_Congo" id="Demokratische_Republik_Kongolic" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Dschibuti" id="Dschibuti" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Ivory_Coast" id="Ivory_Coast" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Eritrea" id="Eritrea" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Gabon" id="Gabun" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Gambia" target="_blank" id="Gambia">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Ghana" target="_blank" id="Ghana">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Guinea" target="_blank" id="Guinea">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Guinea-Bissau" target="_blank" id="Guinea-Bissau">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Cameroon" target="_blank" id="Cameroon">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Kenya" target="_blank" id="Kenya">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Libya" target="_blank" id="Libya">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Liberia" target="_blank" id="Liberia">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Madagaskar" target="_blank" id="Madagaskar">
          
        </a>

        <a href="https://en.wikipedia.org/wiki/Mali" id="malic" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Malawi" id="Malawi" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Morocco" id="Morocco" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Mauretanien" id="Mauretanien" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Mozambique" id="Mozambique" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Namibia" id="Namibia" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Niger" id="malic" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Nigeria" id="Nigeria" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Republic_of_the_Congo" id="Republic_of_the_Congo" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Rwanda" id="Rwanda" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Zambia" id="Zambia" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Senegal" id="Senegal" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Sierra_Leone" id="Sierra_Leone" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Zimbabwe" id="Zimbabwe" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Somalia" id="Somalia" target="_blank">
          
        </a>
        
        <a href="https://en.wikipedia.org/wiki/Sudan" id="Sudan" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/South Africa" id="South Africa" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Swasiland" id="Swasiland" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Tanzania" id="Tanzania" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Togo" id="Togo" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Tunisia" id="Tunisia" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Chad" id="Chad" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Uganda" id="Uganda" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Western_Sahara" id="Western_Sahara" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Central_African_Republic" id="Central_African_Republic" target="_blank">
          
        </a>
        <a href="https://en.wikipedia.org/wiki/Lesotho" id="Lesotho" target="_blank">
          
        </a>
      
    
  



<p>Try clicking on each country. The anchor tag will open a new page and show more information.</p>
<p>Click here to see the live <a target="_blank" href="https://rufai.github.io/buildingx/TourAfrique/">demo</a>.   </p>
<p>Find the complete code <a target="_blank" href="https://github.com/rufai/rufai.github.io/tree/master/buildingx/TourAfrique">here</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This tutorial explained how to create a clickable SVG map by setting up the HTML file, adding interactivity with CSS (including hover and click effects), and adding functionality by linking areas of the map to external web pages. We also discussed the benefits of using SVG maps.</p>
<h3 id="heading-references">References</h3>
<p>There are plenty of resources available to learn more about SVGs. Here are some resources you can look up:</p>
<ol>
<li>MDN Web Docs - SVG: MDN Web Docs is a great resource for learning about web development, including SVG. The SVG section covers everything from basic concepts to advanced topics.</li>
<li>SVG on CSS-Tricks: CSS-Tricks is a popular blog about web development and design. The SVG section covers a range of topics, including how to create SVG graphics, animation, and interactivity.</li>
<li>Xutini Tutorials: the fun way to learn web digital skills. </li>
<li>SVG Pocket Guide: SVG Pocket Guide is a book by Joni Trythall that covers the basics of SVG, including shapes, paths, text, and filters. It's a concise guide that's easy to read and follow.</li>
<li>SVG Animations: SVG Animations is a book by Sarah Drasner that covers the basics of SVG animations, including how to create and control animations using CSS and JavaScript.</li>
</ol>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create Grainy CSS Backgrounds Using SVG Filters ]]>
                </title>
                <description>
                    <![CDATA[ In this article we're going to create more interesting website backgrounds by using grainy effects.  There's a full video walkthrough at the bottom of the article 👇, as well as a 15 second video in the middle for those of you who just want a quick s... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/grainy-css-backgrounds-using-svg-filters/</link>
                <guid isPermaLink="false">66b8ddfb38ac321fcd1e98e3</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Eamonn Cottrell ]]>
                </dc:creator>
                <pubDate>Thu, 16 Feb 2023 16:00:47 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/fCC-thumbp.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article we're going to create more interesting website backgrounds by using grainy effects. </p>
<p>There's a full video walkthrough at the bottom of the article 👇, as well as a 15 second video in the middle for those of you who just want a quick soundbite.</p>
<p>We'll cover this in two ways:</p>
<ol>
<li>Using a PNG image with transparency like on the site below.</li>
<li>Using our own SVG image &amp; code </li>
</ol>
<h2 id="heading-how-to-create-a-grainy-background-using-png-grainy-image">How to Create a Grainy Background Using PNG Grainy Image</h2>
<p>First, the PNG approach.</p>
<p>I came across this background the other day on <a target="_blank" href="https://arc.net/">Arc's site</a>, and I was intrigued. I'd seen <a target="_blank" href="https://dribbble.com/tags/grainy">grainy type illustrations</a> and colors before, and I'd even made a few in Illustrator. But it made me wonder how to produce the same effect as a background on a website.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-10.20.48-AM.png" alt="Image" width="600" height="400" loading="lazy">
<em>Screenshot of grainy background from https://arc.net/</em></p>
<p>Inspecting this showed me that they used two things: a <code>background: var(--colors-primary3)</code> and a <code>background-image: url(noise.png);</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-10.28.24-AM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The <code>.noise.png</code> file was simply a grainy image with transparency so the solid color from the CSS background could come through:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-10.33.09-AM.png" alt="Image" width="600" height="400" loading="lazy">
<em>Screenshot of the noise.png image</em></p>
<h2 id="heading-how-to-recreate-the-png-noise">How to Recreate the PNG Noise</h2>
<p>Let's download the <a target="_blank" href="https://arc.net/noise.png">noise.png file</a>. Then create an HTML document. We'll add a section to display it:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container noise"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Noise with PNG<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<p>In our CSS, we'll add some basic styling for the container:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">min-height</span>: <span class="hljs-number">33vh</span>;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">align-items</span>: center;
}
</code></pre>
<p>Then, for our <code>.noise</code> class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.noise</span>{
    <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">182</span>, <span class="hljs-number">34</span>, <span class="hljs-number">58</span>);
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(/assets/noise.png);
}
</code></pre>
<p>And voilà! We've got the same effect:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-11.05.15-AM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Pretty clever, but I dug further and found how to both create the noisy texture itself as well as generate it inline in our code.</p>
<h2 id="heading-what-are-svgs-with-video-overview">What are SVGs? With Video Overview</h2>
<p>First, a 15 second video summary of the inline part (full 10 min walkthrough is at the end of the article):</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/P4ByGInNhqI" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p>SVGs are Scalable Vector Graphics. </p>
<p>They are handy because they can scale up or down without losing quality. And they are versatile because you can mark them up with code just like you can use CSS to make stylistic changes to your HTML.</p>
<p>In fact, you can even hard code an SVG straight into your HTML or CSS.</p>
<p>Below I'll show you three things:</p>
<ol>
<li>How to create an SVG file to reference in similar fashion to the PNG above.</li>
<li>How to use an SVG file to create a grainy background</li>
<li>How to code the grainy SVG straight into CSS to apply the grainy-ness to elements in our HTML.</li>
</ol>
<h2 id="heading-how-to-create-an-svg">How to Create an SVG</h2>
<p>The <a target="_blank" href="https://grainy-gradients.vercel.app/">Grainy Gradient Playground</a> has been very helpful during this research. It allows for the quick creation of these SVGs.</p>
<p>Let's look at the code for the SVG they're using:</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- svg: first layer --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">'0 0 250 250'</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">'http://www.w3.org/2000/svg'</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">filter</span> <span class="hljs-attr">id</span>=<span class="hljs-string">'noiseFilter'</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">feTurbulence</span> 
      <span class="hljs-attr">type</span>=<span class="hljs-string">'fractalNoise'</span> 
      <span class="hljs-attr">baseFrequency</span>=<span class="hljs-string">'1'</span> 
      <span class="hljs-attr">numOctaves</span>=<span class="hljs-string">'3'</span> 
      <span class="hljs-attr">stitchTiles</span>=<span class="hljs-string">'stitch'</span>/&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">width</span>=<span class="hljs-string">'100%'</span> <span class="hljs-attr">height</span>=<span class="hljs-string">'100%'</span> <span class="hljs-attr">filter</span>=<span class="hljs-string">'url(#noiseFilter)'</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>The first line sets up the SVG with an initial viewBox.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">'0 0 250 250'</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">'http://www.w3.org/2000/svg'</span>&gt;</span>
</code></pre>
<p>Then, the filter is setup and given an id. We use the <code>feTurbulence</code> filter to create the grainy effect. MDN, as always, has more info on the details of <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feTurbulence">feTurbulence</a>.</p>
<pre><code>&lt;feTurbulence 
      type=<span class="hljs-string">'fractalNoise'</span> 
      baseFrequency=<span class="hljs-string">'1'</span> 
      numOctaves=<span class="hljs-string">'3'</span> 
      stitchTiles=<span class="hljs-string">'stitch'</span>/&gt;
  &lt;/filter&gt;
</code></pre><p>Then we're defining the shape (a rectangle), the size (100%) and applying the <code>feTurbulence</code> filter to it:</p>
<pre><code>&lt;rect width=<span class="hljs-string">'100%'</span> height=<span class="hljs-string">'100%'</span> filter=<span class="hljs-string">'url(#noiseFilter)'</span>/&gt;
</code></pre><p>If we throw this in our HTML now, it'll display as plain noise:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-11.17.46-AM.png" alt="Image" width="600" height="400" loading="lazy">
<em>SVG feTurbulence noise image</em></p>
<h2 id="heading-how-to-use-an-svg-as-a-background-image">How to Use an SVG as a Background-Image</h2>
<p>Because SVGs are essentially just code, we can create a <code>noise.svg</code> file in our project, and copy in the contents of the SVG example.</p>
<p>We'll create another <code>div</code> for this method in our HTML:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container noise2"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Noise using SVG File<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<p>I changed the <code>baseFrequency='1'</code> in the SVG file for appearance's sake, and then added the following to our CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.noise2</span>{
    <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">219</span>, <span class="hljs-number">255</span>, <span class="hljs-number">219</span>);
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(/assets/noise.svg);
}
</code></pre>
<p>This gives us a similar result for our green background section:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-11.38.04-AM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-svg-filter-inline-css">SVG Filter Inline CSS</h2>
<p>Finally, we have the option to forego the separate noise file altogether by putting the SVG inline.</p>
<p>In the Gradient Playground, you'll see the option for this in the third CSS+Gradient+CSSFilter box, and there's a toggle switch to produce the inline CSS directly:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-11.40.37-AM.png" alt="Image" width="600" height="400" loading="lazy">
<em>Screenshot from Grainy Gradient Playground</em></p>
<p>It uses gradients going from opaque to transparent to allow for the noise to show through. You can achieve the same effect on a solid color background by repeating some of our code from above.</p>
<p>We'll create a third <code>div</code> for this example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container noise3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Noise with SVG Inline<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<p>Then, we can convert the SVG into usable format in our CSS by using a URL-encoder <a target="_blank" href="https://yoksel.github.io/url-encoder/">like this one</a>. (You can also copy from the Grainy Gradient Playground box where the same code is generated).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-11.23.35-AM.png" alt="Image" width="600" height="400" loading="lazy">
<em>Screenshot of URL-encoder for SVG code</em></p>
<p>And just like that, we have within the URL parenthesis the code that we can use for our <code>background-image</code>. So the CSS for our <code>div</code> looks like this (I modified the baseFrequency more to get a more fine-grained look:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.noise3</span>{
    <span class="hljs-attribute">background</span>:<span class="hljs-built_in">rgb</span>(<span class="hljs-number">68</span>,<span class="hljs-number">0</span>,<span class="hljs-number">255</span>);
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"data:image/svg+xml,%3C!-- svg: first layer --%3E%3Csvg viewBox='0 0 250 250' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='4' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)'/%3E%3C/svg%3E"</span>);
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Screenshot-2023-02-15-at-11.35.14-AM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The parameters are easily editable on all these SVGs and filters to produce different grainy effects. I hope this has been a helpful tutorial for you to easily spice up your backgrounds!</p>
<h2 id="heading-video-walkthrough">Video Walkthrough</h2>
<p>Here's the video walkthrough of all the above: </p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/vi-vi4_UpqM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p>Will you consider subscribing to my <a target="_blank" href="https://www.youtube.com/@eamonncottrell">YouTube channel</a>? I'm making more content like this, and having a blast!</p>
<p>You can find me on <a target="_blank" href="https://www.linkedin.com/in/eamonncottrell/">LinkedIn</a> too 👋.</p>
<p>You're awesome; have a great one!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Import SVGs in a React and Vite app ]]>
                </title>
                <description>
                    <![CDATA[ Are you having difficulties importing SVGs into your React app? This is a problem that many developers face, especially when setting up a new React app with a module bundler. In this article, I will share with you the different ways of importing SVGs... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-import-svgs-in-react-and-vite/</link>
                <guid isPermaLink="false">66d45f35052ad259f07e4ae6</guid>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ vite ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Oyetunji ]]>
                </dc:creator>
                <pubDate>Fri, 01 Jul 2022 22:15:02 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/07/Blog-article-cover-images--3-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Are you having difficulties importing SVGs into your React app? This is a problem that many developers face, especially when setting up a new React app with a module bundler.</p>
<p>In this article, I will share with you the different ways of importing SVGs in React, as well as how the process works under the hood.</p>
<p>Let's get started.</p>
<h2 id="heading-what-is-an-svg">What Is an SVG?</h2>
<p>SVG, short for Scalable Vector Graphic, is an image format used for rendering two-dimensional (2D) graphics on the internet.</p>
<p>The SVG format stores images as <strong>vectors</strong> which are graphics made up of points, lines, and curves based on geometry and mathematical formulas.</p>
<p>Because they are based on numbers and values rather than a grid of pixels like <a target="_blank" href="https://en.wikipedia.org/wiki/Raster_graphics">raster images</a>(.png and.jpg), they do not lose quality when zoomed or resized.</p>
<p>They're also great for creating responsive websites that need to look good and function well across a variety of screen sizes.</p>
<p>Overall, SVGs are great as they are scalable, lightweight, customizable, and can be animated using CSS when used <a class="post-section-overview" href="#2usingsvgsbyaddingdirectlyasjsx">inline</a>.</p>
<h2 id="heading-how-to-import-svgs-in-react-apps">How to Import SVGs in React Apps</h2>
<p>Let's go through some of the most used methods when importing SVGs into React Apps.</p>
<h3 id="heading-1-how-to-import-svgs-using-the-image-tag">1. How to Import SVGs Using the Image Tag</h3>
<p>Importing SVGs using the image tag is one of the easiest ways to use an SVG. If you initialize your app using CRA (Create React App), you can import the SVG file in the image source attribute, as it supports it off the bat.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> YourSvg <span class="hljs-keyword">from</span> <span class="hljs-string">"/path/to/image.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{YourSvg}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Your SVG"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>But if you are not using CRA, you have to set up a file loader system in the bundler you're using (Webpack, Parcel, Rollup, and so on).</p>
<p>Webpack, for instance, has a loader for handling SVGs called <a target="_blank" href="https://v4.webpack.js.org/loaders/file-loader/">file-loader</a>.</p>
<p>To install the file-loader, add the following command:</p>
<pre><code class="lang-bash">npm install file-loader --save-dev
</code></pre>
<p>Next, add the loader to the <code>webpack.config.js</code> file:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">module</span>: {
    <span class="hljs-attr">rules</span>: [
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.(png|jpe?g|gif)$/i</span>,
        use: [
          {
            <span class="hljs-attr">loader</span>: <span class="hljs-string">"file-loader"</span>,
          },
        ],
      },
    ],
  },
};
</code></pre>
<p>Now, you can import your SVG files and use them:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> YourSvg <span class="hljs-keyword">from</span> <span class="hljs-string">"/path/to/image.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{YourSvg}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Your SVG"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>NOTE: While this approach is straightforward, it does have one disadvantage: unlike the other methods for importing, you cannot style the SVG imported in a <code>img</code> element. As a result, it will be suitable for an SVG that does not need customization, like logos.</p>
<h3 id="heading-2-how-to-import-svgs-by-adding-them-directly-as-jsx">2. How to Import SVGs by Adding them Directly as JSX</h3>
<p>JSX supports the <code>svg</code> tag, so we can copy-paste the SVG directly into our React components. This method is straightforward as it helps you take full advantage of SVGs without using a bundler.</p>
<p>The approach is possible because SVGs are in XML format, just like HTML. So, we can convert it to JSX syntax. You can also use a <a target="_blank" href="https://transform.tools/html-to-jsx">compiler</a> instead of manually converting.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
        <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">"ionicon"</span>
        <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 512 512"</span>
      &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
          <span class="hljs-attr">d</span>=<span class="hljs-string">"M160 136c0-30.62 4.51-61.61 16-88C99.57 81.27 48 159.32 48 248c0 119.29 96.71 216 216 216 88.68 0 166.73-51.57 200-128-26.39 11.49-57.38 16-88 16-119.29 0-216-96.71-216-216z"</span>
          <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
          <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
          <span class="hljs-attr">strokeLinecap</span>=<span class="hljs-string">"round"</span>
          <span class="hljs-attr">strokeLinejoin</span>=<span class="hljs-string">"round"</span>
          <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">{32}</span>
        /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>The advantage of including SVGs inline is that we have access to their different properties, which allows us to style and customize them as we see fit.</p>
<p>One thing to keep in mind is that if your SVG file size is large, your code may become complex, reducing readability and productivity. If this is the case, use a png or jpeg file..</p>
<h3 id="heading-3-how-to-import-svgs-as-react-components">3. How to Import SVGs as React Components</h3>
<p>If you use CRA, there's a chance you have imported and used SVGs directly as a React component at one point in time.</p>
<p>This method, which is possible with the help of a file loader, works by loading the image alongside the HTML rather than as a separate file.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { ReactComponent <span class="hljs-keyword">as</span> Logo } <span class="hljs-keyword">from</span> <span class="hljs-string">"./logo.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-4-how-to-convert-svgs-to-react-components">4. How to Convert SVGs to React Components</h3>
<p>This approach is similar to the one previously mentioned. Here, we can convert an SVG to a React component by returning it from inside a class or functional component.</p>
<p>To do this, open up the SVG file in a text editor, and copy-paste the code into a new component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> ArrowUndo = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
      <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"ionicon"</span>
      <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 512 512"</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M245.09 327.74v-37.32c57.07 0 84.51 13.47 108.58 38.68 5.4 5.65 15 1.32 14.29-6.43-5.45-61.45-34.14-117.09-122.87-117.09v-37.32a8.32 8.32 0 00-14.05-6L146.58 242a8.2 8.2 0 000 11.94L231 333.71a8.32 8.32 0 0014.09-5.97z"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
        <span class="hljs-attr">d</span>=<span class="hljs-string">"M256 64C150 64 64 150 64 256s86 192 192 192 192-86 192-192S362 64 256 64z"</span>
        <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
        <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
        <span class="hljs-attr">strokeMiterlimit</span>=<span class="hljs-string">{10}</span>
        <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">{32}</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span></span>
  );
};
</code></pre>
<p>Now, you can import and render the SVG component in another component like this:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { ArrowUndo } <span class="hljs-keyword">from</span> <span class="hljs-string">"./path/to/ArrowUndo.jsx"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Button = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ArrowUndo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
};
</code></pre>
<p>Again, this approach is only possible if your React app has a loader like SVGR's <a target="_blank" href="https://www.npmjs.com/package/@svgr/webpack">Webpack loader</a> included.</p>
<h3 id="heading-5-how-to-import-svgs-using-svgr">5. How to Import SVGs Using SVGR</h3>
<p><a target="_blank" href="https://react-svgr.com/">SVGR</a> is a tool that takes raw SVG files and transforms them into React components. It also has a large ecosystem with support for Create React App, Gatsby, Parcel, Rollup, and other technologies.</p>
<p>So, how do we set it up?</p>
<p>First, install the package by running the code below:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># with npm</span>
npm install --save-dev @svgr/webpack

<span class="hljs-comment"># with yarn</span>
yarn add --dev @svgr/webpack
</code></pre>
<p>Next, update your <code>webpack.config.js</code>:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">module</span>: {
    <span class="hljs-attr">rules</span>: [
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.svg$/i</span>,
        issuer: <span class="hljs-regexp">/\.[jt]sx?$/</span>,
        use: [<span class="hljs-string">"@svgr/webpack"</span>],
      },
    ],
  },
};
</code></pre>
<p>Now, you can import an SVG file as a React component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Logo <span class="hljs-keyword">from</span> <span class="hljs-string">"./logo.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-6-how-to-import-svgs-using-the-vite-plugin-for-svgr">6. How to Import SVGs Using the Vite Plugin for SVGR</h3>
<p><a target="_blank" href="https://www.npmjs.com/package/vite-plugin-svgr"><code>vite-plugin-svgr</code></a> is a plugin for Vite that uses svgr under the hood to transform SVGs into React components.</p>
<p>You can install it by running the following command:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># with npm</span>
npm i vite-plugin-svgr

<span class="hljs-comment"># with yarn</span>
yarn add vite-plugin-svgr
</code></pre>
<p>Next, add the plugin inside your app's <code>vite.config.js</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { defineConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">"vite"</span>;
<span class="hljs-keyword">import</span> react <span class="hljs-keyword">from</span> <span class="hljs-string">"@vitejs/plugin-react"</span>;
<span class="hljs-keyword">import</span> svgr <span class="hljs-keyword">from</span> <span class="hljs-string">"vite-plugin-svgr"</span>;

<span class="hljs-comment">// https://vitejs.dev/config/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> defineConfig({
  <span class="hljs-attr">plugins</span>: [svgr(), react()],
});
</code></pre>
<p>Now, you can import the SVG files as <a class="post-section-overview" href="#3importingsvgsasreactcomponents">React components</a>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { ReactComponent <span class="hljs-keyword">as</span> Logo } <span class="hljs-keyword">from</span> <span class="hljs-string">"./logo.svg"</span>;
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And it's a wrap! In this article, we've covered how to import SVGs in a React App using custom configuration from specific packages, how importing React components works, and how to use them in a Vite setup.</p>
<p>When working with Vite, I use the vite svgr plugin, which works flawlessly. You can also experiment with the other ways discussed in this article.</p>
<p>I hope you found this article insightful. If you do have any questions, feel free to send a message on <a target="_blank" href="https://twitter.com/israelmitolu">Twitter</a> or <a target="_blank" href="https://www.linkedin.com/in/israeloyetunji/">LinkedIn</a>.</p>
<p>Thanks for reading, and happy coding!</p>
<p>Before you go, check out these resources:</p>
<ul>
<li><p><a target="_blank" href="https://israelmitolu.hashnode.dev/why-you-should-ditch-create-react-app-for-vite">Why You Should Ditch Create-React-App for Vite</a></p>
</li>
<li><p><a target="_blank" href="https://rossbulat.medium.com/working-with-svgs-in-react-d09d1602a219">Working with SVGs in React</a></p>
</li>
<li><p><a target="_blank" href="https://twitter.com/i/communities/1532313139810906114">Twitter Community for Devs</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is an SVG File? ]]>
                </title>
                <description>
                    <![CDATA[ SVG stands for scalable vector graphics. It's a web-friendly vector-based file format used to render two-dimensional images on the internet. You can identify SVG files by their extension – .svg. Unlike other popular image formats like PNG, JPEG, and ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-an-svg-file/</link>
                <guid isPermaLink="false">66adf26388723f64bc4313a3</guid>
                
                    <category>
                        <![CDATA[ image ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Wed, 01 Jun 2022 16:05:04 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/06/svg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>SVG stands for scalable vector graphics. It's a web-friendly vector-based file format used to render two-dimensional images on the internet.</p>
<p>You can identify SVG files by their extension – <code>.svg</code>.</p>
<p>Unlike other popular image formats like PNG, JPEG, and JPG – which store image information in form of pixels because they are raster-based formats – SVGs store graphics information as a set of points and lines.</p>
<p>This means no matter how SVG files are reworked, zoomed, or resized, they don’t become blurred and pixelated like PNG, JPG, and other raster images.</p>
<p>This article will show you the possibilities of SVG image files and how you can make one for yourself by coding it.  </p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-make-an-svg-file">How to Make an SVG File</a><ul>
<li><a class="post-section-overview" href="#heading-how-to-make-an-svg-with-image-editing-programs">How to Make an SVG with Image Editing Programs</a> </li>
<li><a class="post-section-overview" href="#heading-how-to-make-an-svg-with-xml">How to Make an SVG with XML </a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-what-is-an-svg-file-used-for">What is an SVG File Used for?</a></li>
<li><a class="post-section-overview" href="#heading-how-to-open-an-svg-file">How to Open an SVG File </a></li>
<li><a class="post-section-overview" href="#heading-how-do-i-convert-an-svg-file-to-an-image">How do I Convert an SVG File to an Image?</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<h2 id="heading-how-to-make-an-svg-file">How to Make an SVG File</h2>
<h3 id="heading-how-to-make-an-svg-with-image-editing-programs">How to Make an SVG with Image Editing Programs</h3>
<p>You can make an SVG file with image editing software like Adobe Illustrator, CorelDraw, Adobe Photoshop, Microsoft Visio, and GIMP.</p>
<p>With these programs, your creativity is your limit when it comes to the SVGs you can draw. </p>
<p>It depends on how knowledgeable and experienced you are with the programs.</p>
<p>In addition, if you create an illustration and drawings with Google Docs, you can export them to SVG.</p>
<h3 id="heading-how-to-make-an-svg-with-xml">How to Make an SVG with XML</h3>
<p>If you don’t know how to use the image editing programs listed above but you can code, you can code up an SVG with XML.</p>
<p>To code an SVG, create a file with the <code>.svg</code> extension:
<img src="https://www.freecodecamp.org/news/content/images/2022/06/ss1.png" alt="ss1" width="600" height="400" loading="lazy"></p>
<p><strong>Step 1</strong>: Define your SVG opening and closing tags</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span>&gt;</span>
    <span class="hljs-comment">&lt;!--  --&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><strong>Step 2</strong>: Define the version and <code>xmlns</code> attributes inside the opening tag and set both to <code>1.1</code> and <code>"http://www.w3.org/2000/svg"</code> respectively.</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><strong>Step 3</strong>: Specify the shape you want to draw in a self-closing tag. For example, <code>&lt;rect&gt;</code> for rectangle.</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><strong>Step 4</strong>: Specify the width and height you want:</p>
<pre><code class="lang-xml"> width="200" height="100"
</code></pre>
<p><strong>Step 5</strong>: Define the color with which you want to fill the shape with the <code>fill</code> attribute:</p>
<pre><code class="lang-xml">fill="#2ecc71"
</code></pre>
<p>The code now looks as shown in the snippet below:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#2ecc71"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>And at the end, this is what shows in the browser:
<img src="https://www.freecodecamp.org/news/content/images/2022/06/ss2.png" alt="ss2" width="600" height="400" loading="lazy"></p>
<p>You can also define border-radius on the <code>x</code> and <code>y</code> axis with the <code>rx</code> and <code>ry</code> attributes:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#2ecc71"</span> <span class="hljs-attr">rx</span>=<span class="hljs-string">"4"</span> <span class="hljs-attr">ry</span>=<span class="hljs-string">"4"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/06/ss3.png" alt="ss3" width="600" height="400" loading="lazy"></p>
<p>After drawing the SVG, you can then use it as the value for the source (<code>src</code>) of an image:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"svgdraw.svg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"A rectangle made with SVG"</span> /&gt;</span>
</code></pre>
<p>If you want, you can embed the SVG straight into your HTML code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#2ecc71"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<h2 id="heading-what-is-an-svg-file-used-for">What is an SVG File Used for?</h2>
<p>Because SVG files remain the same for life, website icons and logos are usually made with them. </p>
<p>An excellent advantage of SVG is that the text in them can be read by search engines like Google, so SVG files are used for making infographics and illustrations.</p>
<h2 id="heading-how-to-open-an-svg-file">How to Open an SVG File</h2>
<p>Modern browsers like Google Chrome, Edge, Safari, and Firefox have built-in functionalities that make them open SVG files for you.     </p>
<p>You can also open SVG files in specialized editing software you can use to make them. Again, examples are Adobe Illustrator, CorelDraw, Adobe Photoshop, Microsoft Visio, and GIMP.</p>
<p>If you want to edit SVG files, you can open them with a Code Editor like VS Code, Atom, and Sublime Text then make your edits.</p>
<h2 id="heading-how-do-i-convert-an-svg-file-to-an-image">How do I Convert an SVG File to an Image?</h2>
<p>If you want to convert an SVG to other image formats like PNG, and JPG, you can use image editing programs like Adobe Photoshop.</p>
<p>You can also use an online tool called <a target="_blank" href="https://convertio.co/svg-png/">Convertio</a>.</p>
<p>All you need to do is to upload your SVG, then select the format you want to convert it to.
<img src="https://www.freecodecamp.org/news/content/images/2022/06/ss4.png" alt="ss4" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>There are a lot of reasons why you should be using SVG.</p>
<p>My favorite of all those reasons is that search engines can read the text on SVG files. This is because SVG files are written in pure XML – the markup language for transmitting digital data.</p>
<p>If Google and other search engines find relevant keywords in the SVG files, it can lead to a massive boost in SEO.</p>
<p>Thank you for reading.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create Reusable SVG Icon React Components ]]>
                </title>
                <description>
                    <![CDATA[ By Dillion Megida We use icons when building frontend applications all the time – for indications, pointers, and so on. Here's how to create a reusable React component for icons. When it comes to icons, you can use PNG or SVG images. PNGs come with a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-reusable-icon-react-components-for-colors-and-sizes-customization/</link>
                <guid isPermaLink="false">66d84f0863d2055c664a1a57</guid>
                
                    <category>
                        <![CDATA[ components ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Icons ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 26 Apr 2022 18:22:02 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/04/pexels-chait-goli-2088233.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Dillion Megida</p>
<p>We use icons when building frontend applications all the time – for indications, pointers, and so on. Here's how to create a reusable React component for icons.</p>
<p>When it comes to icons, you can use PNG or SVG images. PNGs come with a fixed color but allow dimension changes (like a regular image). In some cases this alters the image's quality.</p>
<p>SVGs, on the other hand, have better quality regardless of size and the colors are customizable even after downloading.</p>
<p>But you may agree with me that SVGs can be a pain when it comes to customizability.</p>
<p>In this article, I will show you how I currently go about creating customizable SVG icons as React components.</p>
<h2 id="heading-how-to-download-the-icons">How to Download the Icons</h2>
<p>I usually download the icons I use from <a target="_blank" href="https://remixicon.com/">Remixicon</a>. I haven't tried out other icon libraries yet, so the steps in this article may or may not apply if you use a different library.</p>
<p>That being said, I have worked with a client in the past who created custom icons on Figma. I applied the solution shared in this step, and it also worked for most icons. So follow along even if you don't use Remixicon :)</p>
<p>On Remixicon, I select an icon of my choice, select size <strong>18px</strong>, and select <strong>Copy SVG</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/image-154.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>I leave the color as black. If you choose a different color, it may conflict with the specified colors you provide later. So better to leave it black, which is the default color of SVGs.</p>
<h2 id="heading-how-to-create-the-react-component">How to Create the React Component</h2>
<p>Then I paste the SVG into a file, say, <code>home-line.js</code> with the following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">HomeLine</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 24 24"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"18"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"18"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M0 0h24v24H0z"</span>/&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M21 20a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V9.49a1 1 0 0 1 .386-.79l8-6.222a1 1 0 0 1 1.228 0l8 6.222a1 1 0 0 1 .386.79V20zm-2-1V9.978l-7-5.444-7 5.444V19h14z"</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span></span>
  )
}
</code></pre>
<p>As it is, it uses the default color and size that comes from Remixicon. I'll add two props to modify this component: <code>size</code> and <code>color</code>.</p>
<p>The <code>svg</code> element has four properties: <code>xmlns</code>, <code>viewBox</code>, <code>width</code> and <code>height</code>. I'll use the <code>size</code> prop to modify the value of <code>width</code> and <code>height</code>. Then I'll add an extra property, <code>fill</code>, which I will use for the <code>color</code> prop. </p>
<p>Here is the updated component:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">HomeLine</span>(<span class="hljs-params">{
  size = <span class="hljs-number">18</span>, <span class="hljs-regexp">//</span> or any default size of your choice
  color = <span class="hljs-string">"black"</span> <span class="hljs-regexp">//</span> or any color of your choice
}</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
      <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
      <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 24 24"</span>
      <span class="hljs-attr">width</span>=<span class="hljs-string">{size}</span> // <span class="hljs-attr">added</span> <span class="hljs-attr">size</span> <span class="hljs-attr">here</span>
      <span class="hljs-attr">height</span>=<span class="hljs-string">{size}</span> // <span class="hljs-attr">added</span> <span class="hljs-attr">size</span> <span class="hljs-attr">here</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">{color}</span> // <span class="hljs-attr">added</span> <span class="hljs-attr">color</span> <span class="hljs-attr">here</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M0 0h24v24H0z"</span>/&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M21 20a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V9.49a1 1 0 0 1 .386-.79l8-6.222a1 1 0 0 1 1.228 0l8 6.222a1 1 0 0 1 .386.79V20zm-2-1V9.978l-7-5.444-7 5.444V19h14z"</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span></span>
  )
}
</code></pre>
<p>I leave the <code>viewBox</code> as it is. And now I can use the component like this:</p>
<pre><code class="lang-js">&lt;HomeLine size={<span class="hljs-number">100</span>} color=<span class="hljs-string">"purple"</span> /&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">HomeLine</span> <span class="hljs-attr">size</span>=<span class="hljs-string">{50}</span> <span class="hljs-attr">color</span>=<span class="hljs-string">"red"</span> /&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">HomeLine</span> <span class="hljs-attr">size</span>=<span class="hljs-string">{30}</span> <span class="hljs-attr">color</span>=<span class="hljs-string">"green"</span> /&gt;</span></span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/image-155.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you use a non-square-shaped icon, you will have to specifically provide the <code>width</code> and <code>height</code> prop to change both attributes accordingly.</p>
<h2 id="heading-wrap-up">Wrap Up</h2>
<p>I know that Remixicon does not have every icon you may need, and when you are working with a design system, you may be provided with some custom icons.</p>
<p>But the idea shared here is something you can try with any library you're working with. And if you do, I would love to hear your experience trying this out.</p>
<p>If you liked this article and found it helpful, kindly share :)</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ SVG + JavaScript Tutorial – How to Code an Animated Watch ]]>
                </title>
                <description>
                    <![CDATA[ Since SVG images can be inlined in HTML, we can manipulate them with JavaScript. This means that we can animate parts of an image from code, make it interactive, or turn things around and generate graphics from data. In this example, we are going to ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/svg-javascript-tutorial/</link>
                <guid isPermaLink="false">66c4c81a95c356a0a664df9d</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hunor Márton Borbély ]]>
                </dc:creator>
                <pubDate>Thu, 23 Dec 2021 17:19:15 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.001.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Since SVG images can be inlined in HTML, we can manipulate them with JavaScript. This means that we can animate parts of an image from code, make it interactive, or turn things around and generate graphics from data.</p>
<p>In this example, we are going to create a watch. We will use SVG to paint the watch, and use JavaScript to animate the hands. </p>
<p>This tutorial is a bit more advanced, going in-depth with some of the less obvious SVG properties and focusing on animation with JavaScript. If you'd like to get a more general overview of SVGs, then check out my <a target="_blank" href="https://www.freecodecamp.org/news/svg-tutorial-learn-to-code-images/">earlier article</a> where we walk through the code of 7 simple SVG images. </p>
<p>You can also <a target="_blank" href="https://www.youtube.com/watch?v=ULomsOSk4JA">watch this article as a video</a> with a bit more content. In the video we also cover interaction.</p>
<h2 id="heading-svg-in-html"><strong>SVG in HTML</strong></h2>
<p>In the previous article, we learned that SVG images can be inlined in an HTML document. We talked about the SVG tag itself, which defines the size of the image, and the placement of the image elements.</p>
<p>Image elements are placed within the image by their position. The <code>viewBox</code> defines how these positions should be interpreted. </p>
<p>The first two numbers of the property set the position at the top-left corner. Together with the size defined by the last two numbers, they form a coordinate system.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.001-1.jpeg" alt="Image" width="600" height="400" loading="lazy"></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</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">title</span>&gt;</span>Watch<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">"./index.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">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span>
        <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span>
        <span class="hljs-attr">cy</span>=<span class="hljs-string">"0"</span>
        <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span>
        <span class="hljs-attr">fill</span>=<span class="hljs-string">"transparent"</span>
        <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#f0f0c9"</span>
        <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"7"</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./index.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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>In this example, we center the coordinate system. The <code>0,0</code> coordinate is in the middle of the image. We set with the <code>viewBox</code> that the top-left corner should be the <code>-100,-100</code> coordinate and both the width and height should be 200 units.</p>
<p>In this example, the size defined by <code>width</code> and <code>height</code> and the size defined by <code>viewBox</code> are the same. This means that one unit within the image will be one pixel in the browser. This is not always true. If the two don't match then the image scales up or scales down. </p>
<h2 id="heading-how-to-make-the-watchs-minute-and-hour-hands"><strong>How to Make the Watch's Minute and Hour Hands</strong></h2>
<p>Now that we've established our foundations, let's start coding the watch itself. We start with the minute and hour hands. </p>
<p>There are many ways to draw these little lines. We could draw each line one by one, but probably the most effective way to draw it is to draw a circle with a special dash property.</p>
<p>The <code>circle</code> tag in our initial example has a center position, a radius for the size, a fill and border color, and a border width. </p>
<p>SVG elements often have similar styling options as HTML elements with CSS. But these options have different property names. You can think of the <code>fill</code> property as <code>background-color</code> in CSS. And the <code>stroke</code> and <code>stroke-width</code> properties are also similar to the <code>border-color</code> and <code>border-width</code> properties. Just keep in mind that they are not exactly the same. </p>
<p>We will also use the <code>fill</code> property for setting text color, and we will use the <code>stroke</code> property to set the color of a line.</p>
<p>Now how do we turn a continuous circle into minute markers? You might be familiar with the <code>border-style</code> property in CSS. Mostly you would use a solid border, but you can also have a dotted or a dashed one. These border styles are not very common, as you don’t have that many options to fine-tune them in CSS. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.001-2.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The <code>border-style</code> property in CSS for HTML elements</em></p>
<p>In SVG we have similar possibilities with much more customization options. We can use the <code>stroke-dasharray</code>, the <code>stroke-dashoffset</code>, and the <code>pathLength</code> properties. </p>
<p>Let’s have a few examples. In the first example, we set a single number as <code>stroke-dasharray</code>. This will result in a dashed border where the line segment and the gap both have the same length. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.002.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The <code>stroke-dasharray</code> property for SVG</em></p>
<p>This property is an array though. If we set two numbers, then the first one will be the length of the line segment, and the second will be the length of the gap. You can even set more than two numbers, and then the length of the line and the gap will always take the next number. Until it runs out of the array and then it starts at the beginning.</p>
<p>We will set two numbers. One for the length of the minute marker, and one for the gap between them. The sum of these two should be exactly the length of one minute on the circle. We know that one hour is 60 minutes. So we can calculate the circumference of the circle, then divide it by 60 to get the length of one minute. </p>
<p>But there’s a better way. Instead of calculating the circumference of the circle, we can go the other way. We can set the <code>pathLength</code> property. </p>
<p>This property is a bit tricky. It does not resize the circle but affects how the dasharray property is interpreted. The dashes will be drawn as if the circle had a circumference defined by <code>pathLength</code>.</p>
<p>So let’s set the <code>pathLength</code> to <code>60</code>, representing 60 minutes. Now the sum of the line and gap segment has to be 1 in total. I set it to <code>0.2</code> and <code>0.8</code> in this example.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.001-3.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>Using the <code>pathLength</code> property. Note that the sum of the two numbers at the <code>stroke-dasharray</code> property is one, matching the length of one minute.</em></p>
<p>Now we are almost done, but one little piece is still missing. The dashing starts at the wrong position. To fix it we have to shift it by half of the line segment’s length using the <code>stroke-dashoffset</code> property. </p>
<p>The dash offset property can be a bit counterintuitive, as a positive value here shifts the dashing backwards. You can also set it to a positive number to shift it forward.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.002-1.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>Example with and without <code>stroke-dashoffset</code></em></p>
<p>In the same way, we can set an hour marker. We add a new circle tag with almost the same properties. The only thing that is different is the color and we have longer gaps in the dash array. </p>
<pre><code class="lang-html">. . .

    <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span>
        <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span>
        <span class="hljs-attr">cy</span>=<span class="hljs-string">"0"</span>
        <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span>
        <span class="hljs-attr">fill</span>=<span class="hljs-string">"transparent"</span>
        <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#0f0e0e"</span>
        <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"7"</span>
        <span class="hljs-attr">stroke-dasharray</span>=<span class="hljs-string">"0.2 0.8"</span>
        <span class="hljs-attr">stroke-dashoffset</span>=<span class="hljs-string">"0.1"</span>
        <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span>
      /&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span>
        <span class="hljs-attr">cx</span>=<span class="hljs-string">"0"</span>
        <span class="hljs-attr">cy</span>=<span class="hljs-string">"0"</span>
        <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span>
        <span class="hljs-attr">fill</span>=<span class="hljs-string">"transparent"</span>
        <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#f0f0c9"</span>
        <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"7"</span>
        <span class="hljs-attr">stroke-dasharray</span>=<span class="hljs-string">"0.2 4.8"</span>
        <span class="hljs-attr">stroke-dashoffset</span>=<span class="hljs-string">"0.1"</span>
        <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>

. . .
</code></pre>
<p>It's important to note here that layering in SVG matters. Tags added later in the document will be on top of the previous ones. If we add these two circles in the opposite order, then the minutes would fully cover the hour markers. </p>
<p>As SVG lives in HTML now, we can move some of these properties from CSS. We can’t move all the properties though. There’s a difference between properties defining the style and those defining the shape of an element. </p>
<p>The radius, for instance, defines the shape of the circle, so it has to stay with the SVG code. The fill and stroke properties on the other hand we can move. </p>
<pre><code class="lang-html">. . .

    <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"minute_marker"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hour_marker"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>

. . .
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.hour_marker</span> {
  <span class="hljs-attribute">fill</span>: transparent;
  <span class="hljs-attribute">stroke</span>: <span class="hljs-number">#f0f0c9</span>;
  <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">7</span>;
  <span class="hljs-attribute">stroke-dasharray</span>: <span class="hljs-number">0.2</span>, <span class="hljs-number">4.8</span>;
  <span class="hljs-attribute">stroke-dashoffset</span>: <span class="hljs-number">0.1</span>;
}

<span class="hljs-selector-class">.minute_marker</span> {
  <span class="hljs-attribute">fill</span>: transparent;
  <span class="hljs-attribute">stroke</span>: <span class="hljs-number">#0f0e0e</span>;
  <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">7</span>;
  <span class="hljs-attribute">stroke-dasharray</span>: <span class="hljs-number">0.2</span>, <span class="hljs-number">0.8</span>;
  <span class="hljs-attribute">stroke-dashoffset</span>: <span class="hljs-number">0.1</span>;
}
</code></pre>
<h2 id="heading-how-to-draw-the-watch-hands"><strong>How to Draw the Watch Hands</strong></h2>
<p>Let's add the hands that show the time. Initially, we draw these to point upwards, then turn them into position with JavaScript. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/12/SVG-Watch.001-4.jpeg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We use the <code>line</code> element to draw the hands. To define a line element we have to set the starting and ending coordinates, along with a <code>stroke</code> color and the <code>stroke-width</code> property. </p>
<p>To make things a bit nicer, we can also add the <code>stroke-linecap</code> property to have rounded line caps. These styling properties we add with CSS. </p>
<pre><code class="lang-html">. . . 

    <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"minute_marker"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hour_marker"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span> /&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-50"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand hand--thick"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"-12"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-50"</span> /&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-80"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand hand--thick"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"-12"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-80"</span> /&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand hand--second"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"12"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-80"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>

. . .
</code></pre>
<pre><code class="lang-css">. . .

<span class="hljs-selector-class">.hand</span> {
  <span class="hljs-attribute">stroke</span>: <span class="hljs-number">#ffffff</span>;
  <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">2</span>;
  <span class="hljs-attribute">stroke-linecap</span>: round;
}

<span class="hljs-selector-class">.hand--thick</span> {
  <span class="hljs-attribute">stroke-width</span>: <span class="hljs-number">7</span>;
}

<span class="hljs-selector-class">.hand--second</span> {
  <span class="hljs-attribute">stroke</span>: yellow;
}
</code></pre>
<h2 id="heading-how-to-point-the-watch-hands-in-the-right-direction">How to Point the Watch Hands in the Right Direction</h2>
<p>Now how do we turn these lines into position? If we assign an ID to an element we can access it and manipulate it from JavaScript. </p>
<p>Which element should we assign an ID, though? We have two elements for one hand. To solve this problem we can group these two line elements in a group tag. You can think of a group tag as the <code>div</code> element in HTML.</p>
<p>We can assign an ID to this group, then we can rotate the whole group into position from JavaScript. </p>
<pre><code class="lang-html">. . .

    <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"800"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"800"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"-100 -100 200 200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"minute_marker"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hour_marker"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"90"</span> <span class="hljs-attr">pathLength</span>=<span class="hljs-string">"60"</span> /&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"hour_hand"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-50"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand hand--thick"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"-12"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-50"</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"minute_hand"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-80"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand hand--thick"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"-12"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-80"</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"second_hand"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hand hand--second"</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"12"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"-80"</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>

. . .
</code></pre>
<p>In the JavaScript file, first, we get the hand elements by ID. Then we create a Date object and we get the current hour, minute, and second. And finally, we set the elements' <code>transform</code> attribute based on these values. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> hoursElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"hour_hand"</span>);
<span class="hljs-keyword">const</span> minutesElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"minute_hand"</span>);
<span class="hljs-keyword">const</span> secondsElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"second_hand"</span>);

<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();

<span class="hljs-keyword">const</span> hour = date.getHours();
<span class="hljs-keyword">const</span> minute = date.getMinutes();
<span class="hljs-keyword">const</span> second = date.getSeconds();

hoursElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">12</span>) * hour}</span>)`</span>);
minutesElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * minute}</span>)`</span>);
secondsElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * second}</span>)`</span>);
</code></pre>
<p>The transform attribute can include multiple transformations like scaling, translating, or skewing. </p>
<p>We are setting the <code>rotate</code> transformation, which requires a number. This number is a rotation between 0 and 360 degrees. For the hour hand, we divide 360 by 12 to get how much rotation we need per hour and multiply it with the current hour. This should turn the hour hand towards the current hour. </p>
<p>For the minute and second hand, we do the same thing, except we divide 360 by 60, as one hour consists of 60 minutes and 1 minute is 60 seconds.</p>
<p>Luckily for us, the transformation center by default is the origin, the <code>0,0</code> coordinate. If this wouldn’t be the case we could set another transformation origin, but because of our <code>viewBox</code> settings, we don’t need that.  </p>
<h2 id="heading-how-to-animate-the-watch-hands"><strong>How to Animate </strong>the<strong> Watch H</strong>ands<em>**</em></h2>
<p>Now, this should already show the current time, but our image is static. To keep up with time we can use the <code>requestAnimationFrame</code> function to move the hands. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> hoursElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"hour_hand"</span>);
<span class="hljs-keyword">const</span> minutesElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"minute_hand"</span>);
<span class="hljs-keyword">const</span> secondsElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"second_hand"</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">animate</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();

  <span class="hljs-keyword">const</span> hour = date.getHours() % <span class="hljs-number">12</span>;
  <span class="hljs-keyword">const</span> minute = date.getMinutes();
  <span class="hljs-keyword">const</span> second = date.getSeconds();

  hoursElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">12</span>) * hour}</span>)`</span>);
  minutesElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * minute}</span>)`</span>);
  secondsElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * second}</span>)`</span>);

  requestAnimationFrame(animate);
}

requestAnimationFrame(animate);
</code></pre>
<p>We move the rotation logic into an animate function, and use the requestAnimationFrame function. </p>
<p>First, we trigger it by calling requestAnimationFrame outside the animate function. Then, to keep on with the animation we also request another frame at the end of every animation cycle. </p>
<p>If you want to have a smoother animation, then you can refine the positioning. Instead of having discrete positions for the hands, we can define them in a way that they can point to split seconds, minutes, and hours. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> hoursElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"hour_hand"</span>);
<span class="hljs-keyword">const</span> minutesElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"minute_hand"</span>);
<span class="hljs-keyword">const</span> secondsElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"second_hand"</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">animate</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();

  <span class="hljs-keyword">const</span> hour = date.getHours() + date.getMinutes() / <span class="hljs-number">60</span>;
  <span class="hljs-keyword">const</span> minute = date.getMinutes() + date.getSeconds() / <span class="hljs-number">60</span>;
  <span class="hljs-keyword">const</span> second = date.getSeconds() + date.getMilliseconds() / <span class="hljs-number">1000</span>;

  hoursElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">12</span>) * hour}</span>)`</span>);
  minutesElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * minute}</span>)`</span>);
  secondsElement.setAttribute(<span class="hljs-string">"transform"</span>, <span class="hljs-string">`rotate(<span class="hljs-subst">${(<span class="hljs-number">360</span> / <span class="hljs-number">60</span>) * second}</span>)`</span>);

  requestAnimationFrame(animate);
}

requestAnimationFrame(animate);
</code></pre>
<p>The hour hand won’t get its position only based on the hour, but it will make a slight turn based on the current minutes as well. </p>
<p>The minute hand will consider the current second in its rotation. And the second hand will also consider milliseconds. This way our hands will have a continuous movement. They won’t jump from second to second, but they will animate.</p>
<h2 id="heading-next-steps-how-to-make-the-watch-interactive"><strong>Next steps – How to Make the Watch Interactive</strong></h2>
<p>Now if we check the result, we should have a smoothly animated watch. </p>
<p>To go further, you can also add a calendar window showing the current date, with the <code>text</code> element. And to take it to the next level you can even add an event handler for this element, which toggles its content between the current date and the AM/PM indicator. </p>
<p>If you are stuck, check out the video below, where we also cover this part.</p>
<p>Mixing SVG with JavaScript opens up a lot of cool options. You can animate things, add interactions, and generate graphics. Can't wait to see what you come up with.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/ULomsOSk4JA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-subscribe-for-more-tutorials-on-web-development"><strong>Subscribe for more tutorials on Web Development:</strong></h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/undefined" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use SVG Images in CSS and HTML – A Tutorial for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ By Edidiong Asikpo SVG stands for Scalable Vector Graphics. It is a unique type of image format for vector-based graphics written in Extensible Markup Language (XML). In this tutorial, I will explain why you'd want to use SVG images and how you can u... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-svg-images-in-css-html/</link>
                <guid isPermaLink="false">66d45e48052ad259f07e4aa5</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ image ]]>
                    </category>
                
                    <category>
                        <![CDATA[ image optimization  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ xml ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 16 Nov 2020 22:44:20 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/11/Screen-Shot-2020-11-15-at-3.59.07-PM.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Edidiong Asikpo</p>
<p>SVG stands for Scalable Vector Graphics. It is a unique type of image format for vector-based graphics written in Extensible Markup Language (XML).</p>
<p>In this tutorial, I will explain why you'd want to use SVG images and how you can use them in CSS and HTML.</p>
<h2 id="heading-why-should-you-use-svg-images">Why Should You Use SVG Images?</h2>
<p>There are a number of reasons to use SVG images, some of which are:</p>
<ul>
<li><p>SVG images do not lose their quality when zoomed or resized.</p>
</li>
<li><p>They can be created and edited with an IDE or text editor.</p>
</li>
<li><p>They are accessible and animatable.</p>
</li>
<li><p>They have a small file size and are highly scalable.</p>
</li>
<li><p>And they can be searched, indexed, scripted, and compressed.</p>
</li>
</ul>
<p>Now let's see how you can actually work with SVG images.</p>
<h2 id="heading-how-to-download-the-svg-image-used-in-this-tutorial">How to Download the SVG Image Used in This Tutorial</h2>
<p>If you want to work with the SVG image I've used in this tutorial, follow the steps (and diagram) below to download it.</p>
<ul>
<li><p>Go to <a target="_blank" href="https://undraw.co">unDraw</a>.</p>
</li>
<li><p>Change the background color to yellow.</p>
</li>
<li><p>In the search box, search for the word <strong>happy</strong>.</p>
</li>
</ul>
<p><img src="https://i.imgur.com/ncSY7Rn.png" alt="unDraw's Homepage" width="1836" height="888" loading="lazy"></p>
<ul>
<li><p>Click on the image named <strong>Happy news</strong>.</p>
</li>
<li><p>On the pop-up window, click on the <strong>Download SVG to your projects</strong> button.</p>
</li>
</ul>
<p><img src="https://i.imgur.com/qGrT73n.png" alt="Download the SVG file" width="1336" height="581" loading="lazy"></p>
<p>If you followed the steps above correctly, the SVG image should be on your computer now.</p>
<p><img src="https://i.imgur.com/3uCGy6B.png" alt="Image" width="1003" height="183" loading="lazy"></p>
<p>Now, open the SVG image in your favorite IDE or text editor. Rename it to <strong>happy.svg</strong> or whatever name you prefer.</p>
<h2 id="heading-how-to-use-svg-images-in-css-and-html">How to Use SVG Images in CSS and HTML</h2>
<p>There are several different ways to use SVG images in CSS and HTML. We will explore six different methods in this tutorial.</p>
<h3 id="heading-1-how-to-use-an-svg-as-an">1. How to use an SVG as an <code>&lt;img&gt;</code></h3>
<p>This method is the simplest way to add SVG images to a webpage. To use this method, add the <code>&lt;img&gt;</code> element to your HTML document and reference it in the <code>src</code> attribute, like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span> = <span class="hljs-string">"happy.svg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"My Happy SVG"</span>/&gt;</span>
</code></pre>
<p>Assuming you downloaded the SVG image from unDraw and renamed it to <strong>happy.svg</strong>, you can go ahead and add the code snippet above into your HTML document.</p>
<p>If you did everything correctly, your webpage should look exactly like the demo below. 👀</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-mppxs?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<p>When you add an SVG image using the <code>&lt;img&gt;</code> tag without specifying the size, it assumes the size of the original SVG file.</p>
<p>For instance, in the demo above, I didn't modify the size of the SVG image, so it assumed its original size (which was a width of <code>915.11162px</code> and a height of <code>600.53015px</code> ).</p>
<p><strong>Note:</strong> to change the original size, you have to specify the <code>width</code> and <code>height</code> with CSS as you can see in the demo below. You can also update the original <code>width</code> and <code>height</code> directly.</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-1-ey5me?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<p>Even though we can change the size of SVG images added via the <code>&lt;img&gt;</code> tag, there are still some restrictions if you want to make major style changes to the SVG image.</p>
<h3 id="heading-2-how-to-use-svg-as-a-css-background-image">2. How to use SVG as a CSS <code>background-image</code></h3>
<p>This is similar to adding SVG to an HTML document using the <code>&lt;img&gt;</code> tag. But this time we do it with CSS instead of HTML as you can see in the code snippet below.</p>
<pre><code class="lang-bash">body {
  background-image: url(happy.svg);
}
</code></pre>
<p>When you use an SVG as a CSS background image, it has similar limitations to using <code>&lt;img&gt;</code>. Still, it allows a bit more customization.</p>
<p>Check out the demo below and feel free to make modifications to it using CSS.</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-2-ftn6n?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<h3 id="heading-3-how-to-use-inline-svg-images">3. How to use inline SVG images</h3>
<p>SVG images can be written directly into the HTML document using the<code>&lt;svg&gt; &lt;/svg&gt;</code> tag.</p>
<p>To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <code>&lt;body&gt;</code> element in your HTML document.</p>
<pre><code class="lang-bash">&lt;body&gt;
 // Paste the SVG code here.
&lt;/body&gt;
</code></pre>
<p>If you did everything correctly, your webpage should look exactly like the demo below.</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-3-zunkd?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<p>When you use SVG inline in the HTML document, it reduces load time because it serves as an HTTP request.</p>
<p>Using this method lets you perform more customization as opposed to using either the <code>&lt;img&gt;</code> or <code>background-image</code> methods.</p>
<h3 id="heading-4-how-to-use-an-svg-as-an">4. How to use an SVG as an <code>&lt;object&gt;</code></h3>
<p>You can also use an HTML <code>&lt;object&gt;</code> element to add SVG images to a webpage using the code syntax below:</p>
<pre><code class="lang-bash">&lt;object data=<span class="hljs-string">"happy.svg"</span> width=<span class="hljs-string">"300"</span> height=<span class="hljs-string">"300"</span>&gt; &lt;/object&gt;
</code></pre>
<p>You use the <code>data</code> attribute to specify the URL of the resource that you'll use by the object, which is the SVG image in our case.</p>
<p>You use the <code>width</code> and <code>height</code> to specify the size of the SVG image.</p>
<p>Again, below is a demo for you to explore. 😃</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-4-3ge0n?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<p>Using the <code>&lt;object&gt;</code> is supported across all browsers that support SVG.</p>
<h3 id="heading-5-how-to-use-svg-as-an">5. How to use SVG as an <code>&lt;iframe&gt;</code></h3>
<p>Even though this isn't advisable, you can also add an SVG image using an <code>&lt;iframe&gt;</code> as seen in the demo below.</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-5-co3hg?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<p>Just keep in mind, though, that <code>&lt;iframe&gt;</code>s can be difficult to maintain and will be bad for your site's Search Engine Optimization (SEO).</p>
<p>Using <code>&lt;iframe&gt;</code> also defeats the purpose of the <em>Scalable</em> in the name <em>Scalable Vector Graphics</em> because SVG images added with this format are not scalable.</p>
<h3 id="heading-6-how-to-use-svg-as-an">6. How to use SVG as an <code>&lt;embed&gt;</code></h3>
<p>The HTML <code>&lt;embed&gt;</code> element is another way to use an SVG image in HTML and CSS using this syntax: <code>&lt;embed src="happy.svg" /&gt;</code>.</p>
<p>Keep in mind, however, that this method has limitations, too. According to MDN, most modern browsers have deprecated and removed support for browser plug-ins. This means that relying upon <code>&lt;embed&gt;</code> is generally not wise if you want your site to be operable on the average user's browser.</p>
<p>Below is a demo of using the HTML <code>&lt;embed&gt;</code> element to add an SVG image.</p>
<div class="embed-wrapper"><iframe src="https://codesandbox.io/embed/svg-demo-6-iwy0s?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" title="Embedded content" loading="lazy"></iframe></div>

<h2 id="heading-conclusion">Conclusion</h2>
<p>I hope you were able to learn about the different ways of using SVG images in CSS and HTML. This will hopefully guide you towards choosing the right method when adding SVG images to a website.</p>
<p>If you have any questions, you can send me a <a target="_blank" href="https://twitter.com/Didicodes">message on Twitter</a>, and I'll be happy to answer every single one.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use SVG Icons in React with React Icons and Font Awesome ]]>
                </title>
                <description>
                    <![CDATA[ Icons are a way to visually communicate concepts and meaning without the use of words. This could be for categorization, an action, or even a warning.  How can we add icons using SVG to our React apps to improve our visual communication? What is SVG... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-svg-icons-in-react-with-react-icons-and-font-awesome/</link>
                <guid isPermaLink="false">66b8e37a6a98b2a27ee1f34e</guid>
                
                    <category>
                        <![CDATA[ Icons ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Colby Fayock ]]>
                </dc:creator>
                <pubDate>Thu, 24 Sep 2020 16:07:39 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/09/react-icons.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Icons are a way to visually communicate concepts and meaning without the use of words. This could be for categorization, an action, or even a warning. </p>
<p>How can we add icons using SVG to our React apps to improve our visual communication?</p>
<ul>
<li><a class="post-section-overview" href="#heading-what-is-svg">What is SVG?</a></li>
<li><a class="post-section-overview" href="#heading-what-makes-svg-great-for-the-web">What makes SVG great for the web?</a></li>
<li><a class="post-section-overview" href="#heading-part-0-creating-a-react-app">Part 0: Creating a React app</a></li>
<li><a class="post-section-overview" href="#heading-part-1-adding-svg-icons-with-react-icons">Part 1: Adding SVG icons with react-icons</a></li>
<li><a class="post-section-overview" href="#heading-part-2-manually-adding-svg-files-to-a-react-component">Part 2: Manually adding SVG files to a React component</a></li>
</ul>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/OtcA2EAlldo" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-what-is-svg">What is SVG?</h2>
<p><a target="_blank" href="https://www.w3.org/Graphics/SVG/">SVG</a> stands for Scalable Vector Graphics. It’s a file format based on a markup language similar to XML that allows developers and designers to create vector-based images using path definitions.</p>
<h2 id="heading-what-makes-svg-great-for-the-web">What makes SVG great for the web?</h2>
<p>SVG was born for the web. It’s an open standard that was created by W3C to provide a better way to add images to the web. If you open an SVG file on your computer, you might be surprised to find that it’s all code!</p>
<p>This plays a part in the small file size. Typically when using SVG, you can take advantage of its smaller size compared to larger image files that might be required to deliver the same high resolution.</p>
<p>Additionally, since we’re defining SVG as paths, they can scale as large or as small as we want. This makes them extra flexible for web usage, especially when making experiences responsive.</p>
<h2 id="heading-what-are-we-going-to-create">What are we going to create?</h2>
<p>We’re first going to walk through using a package called <a target="_blank" href="https://react-icons.github.io/react-icons/">react-icons</a> that will allow us to easily import icons from popular icon sets like <a target="_blank" href="https://fontawesome.com/">Font Awesome</a> right into our app.</p>
<p>We’ll also take a look at how we can manually add SVG files right into our app by copying the code of an SVG file right into a new component.</p>
<h2 id="heading-part-0-creating-a-react-app">Part 0: Creating a React app</h2>
<p>For this walkthrough, you can use any React framework you’d like whether that’s <a target="_blank" href="https://create-react-app.dev/">Create React App</a> or <a target="_blank" href="https://nextjs.org/">Next.js</a>. You can even use an existing project.</p>
<p>Because we don’t need anything special to add our icons, I’m going to use create-react-app.</p>
<p>To get started with create-react-app, you can create a new project using the following command in your terminal:</p>
<pre><code>yarn create react-app [project-name]
# or
npx create-react-app [project-name]
</code></pre><p><em>Note: replace <code>[project-name]</code> with the name you want to use for your project. I’m going to use <code>my-svg-icons</code>.</em></p>
<p>Once you have your new app or your existing app, we should be ready to go!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/new-create-react-app.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>New Create React App</em></p>
<h2 id="heading-part-1-adding-svg-icons-with-react-icons">Part 1: Adding SVG icons with react-icons</h2>
<h3 id="heading-adding-react-icons-to-your-project">Adding react-icons to your project</h3>
<p>To get started with react-icons, we want to install it in our project.</p>
<p>Inside of your project, run the following command:</p>
<pre><code>yarn add react-icons
# or
npm install react-icons --save
</code></pre><p>Once it’s completed, we should be ready to use it in our project.</p>
<h3 id="heading-selecting-icons-for-a-project">Selecting icons for a project</h3>
<p>One of the cool things about react-icons is the extensive library they make available within the single package.</p>
<p>Not only do we have Font Awesome immediately available, we have <a target="_blank" href="https://primer.style/octicons">GitHub’s Octicons</a>, <a target="_blank" href="https://heroicons.com/">Heroicons</a>, <a target="_blank" href="https://google.github.io/material-design-icons/">Material Design Icons</a>, and <a target="_blank" href="https://react-icons.github.io/">a whole bunch more</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/react-icons-heroicons.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>react-icons Heroicons</em></p>
<p>When choosing icons, you pretty much have the ability to use any icon you want at any time. That said, I would recommend trying to keep a consistent look and feel with your icons.</p>
<p>If you primarily use Font Awesome for your website, it might look a bit strange and inconsistent if you were to start adding <a target="_blank" href="https://react-icons.github.io/icons?name=fc">Flat Color Icons</a> to the mix. You ultimately want to provide an experience that people will be able to easily identify the patterns that you create.</p>
<h3 id="heading-using-react-icons-in-your-project">Using react-icons in your project</h3>
<p>Once you’ve found the icons you want to use, we can now add them to our project.</p>
<p>react-icons’s website makes it easy for us to look up the name of the icon we want to use to import to our project.</p>
<p>If we wanted to use the Font Awesome rocket icon, we can navigate to Font Awesome in the sidebar, search the page for “rocket” (CMD+F or CTRL+F), and then click the icon which will copy its name to your clipboard.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/font-awesome-rocket.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Font Awesome rocket icon</em></p>
<p>We could also search for “rocket” in the search form at the top left of the page, which shows us the result “rocket” throughout all icon sets.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/react-icons-rocket.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Rocket icons in react-icons</em></p>
<p>Inside of our project, we can now import that icon. Similar to the instructions at the top of the react-icons page, we want to import that specific icon from the <code>react-icons/fa</code>, which refers to the Font Awesome module of react-icons.</p>
<p>Add the following to the top of the file you want to import the icon in. If using a new create-react-app project, you can add it to the top of <code>src/App.js</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { FaRocket } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-icons/fa'</span>;
</code></pre>
<p>To test this out, let’s replace the React logo with our icon.</p>
<p>Remove the <code>&lt;img</code> component and instead add:</p>
<pre><code class="lang-jsx">&lt;FaRocket /&gt;
</code></pre>
<p>And if we reload the page, we can see our rocket!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/create-react-app-rocket-icon.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Rocket icon in React app</em></p>
<p>Our rocket isn’t spinning like the React logo though, so let’s fix that.</p>
<p>Add the class <code>.App-logo</code> to the <code>FaRocket</code> component:</p>
<pre><code class="lang-jsx">&lt;FaRocket className=<span class="hljs-string">"App-logo"</span> /&gt;
</code></pre>
<p>And the rocket should now be spinning!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/create-react-app-rocket-spin.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Spinning rocket icon in React app</em></p>
<p>But it’s also a little small. If we look inside of <code>src/App.css</code>, we’re setting the height of <code>.App-logo</code> to <code>40vmin</code>. While that’s working, for our icon to fill the space, we need to also provide a width for it to fill.</p>
<p>Add the following to the <code>.App-logo</code> class to add a width:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">width</span>: 40<span class="hljs-selector-tag">vmin</span>;
</code></pre>
<p>And while it’s probably a little too big now, we’re at a more appropriate size and we have our icon!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/create-react-app-icon-rocket-spin-large.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Increased size of spinning rocket icon in React app</em></p>
<p><a target="_blank" href="https://github.com/colbyfayock/my-svg-icons/commit/036112c3e2ffc5f42a53c68e8025fe70a87e3b13">Follow along with the commit</a>.</p>
<h2 id="heading-part-2-manually-adding-svg-files-to-a-react-component">Part 2: Manually adding SVG files to a React component</h2>
<p>Sometimes you don’t want to add a new library just to get an icon. Sometimes it’s a custom icon that’s not available in a public library.</p>
<p>Luckily with React, we can create a new SVG component pretty easily that allows us to add our custom SVG icons anywhere we want.</p>
<p>First, let’s find an icon.</p>
<p>While all Heroicons are available inside react-icons, let’s use it as an example since it’s easy to find and copy some SVG code.</p>
<p>Go to heroicons.com and search for an icon that you’d like to use for this example. I’m going to use “globe”.</p>
<p>After finding the icon you want, hover over that icon, where you’ll see options to copy that icon as SVG or JSX, and copy it as JSX.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/heroicons-copy-svg.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Copy as JSX in Heroicons</em></p>
<p>With that icon copied, create a new file under <code>src</code> called <code>Globe.js</code>.</p>
<p>Inside of that file, we’re going to create a new component called Globe and paste in our SVG within that component.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">const</span> Globe = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 24 24"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">stroke-linecap</span>=<span class="hljs-string">"round"</span> <span class="hljs-attr">stroke-linejoin</span>=<span class="hljs-string">"round"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Globe;
</code></pre>
<p><em>Note: when copying normal SVG to a React component, it might not work "as is". Sometimes SVG files include CSS classes or element attributes that aren't valid with JSX.</em> </p>
<p><em>If you run into errors, try fixing the attributes and looking at the web console to see the warnings and errors React throws. Because we copied as JSX, we were able to make it work right away.</em></p>
<p>Now, go back to <code>src/App.js</code> and import our new component:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Globe <span class="hljs-keyword">from</span> <span class="hljs-string">'./Globe'</span>;
</code></pre>
<p>Then we can replace our rocket icon with our new component:</p>
<pre><code class="lang-jsx">&lt;Globe /&gt;
</code></pre>
<p>And if we open up our browser, we can see our globe!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/create-react-app-globe-large.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Large globe icon in REact app</em></p>
<p>Though, it’s a little big.</p>
<p>We want to apply our <code>.App-logo</code> class to our Globe component, so we need to update that component to take a <code>className</code> prop.</p>
<p>Back at <code>src/Globe.js</code>, add a <code>className</code> prop argument:</p>
<pre><code><span class="hljs-keyword">const</span> Globe = <span class="hljs-function">(<span class="hljs-params">{ className }</span>) =&gt;</span> {
</code></pre><p>Then, add a new prop with that <code>className</code> to the <code>&lt;svg</code> component:</p>
<pre><code class="lang-jsx">&lt;svg className={className}
</code></pre>
<p>Now, we can update our Globe component in <code>src/App.js</code> to include that class:</p>
<pre><code class="lang-jsx">&lt;Globe className=<span class="hljs-string">"App-logo"</span> /&gt;
</code></pre>
<p>And if we reload the page, we can see our logo is back at the right size and it’s spinning again!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/create-react-app-globe-icon-spinning.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Spinning, normal size globe icon in React app</em></p>
<p><a target="_blank" href="https://github.com/colbyfayock/my-svg-icons/commit/87b00748fc9b38d80336ddb5f6f823388c2edead">Follow along with the commit</a>.</p>
<h2 id="heading-why-dont-we-include-it-as-an-img-file">Why don’t we include it as an img file?</h2>
<p>While we can include it as an image file just like React does in the default create-react-app code, we get a few benefits from adding our SVG files “inline”.</p>
<p>For one, when adding SVG inline, we can access the different paths with CSS properties. This gives us more flexibility for customizing it dynamically.</p>
<p>It’s also going to provide fewer HTTP requests. The browser will know how to load that SVG, so we don’t need to bother the browser to request that file to include in the page.</p>
<p>That said, it’s still a valid option for adding an SVG file to the page.</p>
<div id="colbyfayock-author-card">
  <p>
    <a href="https://twitter.com/colbyfayock">
      <img src="https://res.cloudinary.com/fay/image/upload/w_2000,h_400,c_fill,q_auto,f_auto/w_1020,c_fit,co_rgb:007079,g_north_west,x_635,y_70,l_text:Source%20Sans%20Pro_64_line_spacing_-10_bold:Colby%20Fayock/w_1020,c_fit,co_rgb:383f43,g_west,x_635,y_6,l_text:Source%20Sans%20Pro_44_line_spacing_0_normal:Follow%20me%20for%20more%20JavaScript%252c%20UX%252c%20and%20other%20interesting%20things!/w_1020,c_fit,co_rgb:007079,g_south_west,x_635,y_70,l_text:Source%20Sans%20Pro_40_line_spacing_-10_semibold:colbyfayock.com/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_68,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_145,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_222,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_295,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/v1/social-footer-card" alt="Follow me for more Javascript, UX, and other interesting things!" width="2000" height="400" loading="lazy">
    </a>
  </p>
  <ul>
    <li>
      <a href="https://twitter.com/colbyfayock">? Follow Me On Twitter</a>
    </li>
    <li>
      <a href="https://youtube.com/colbyfayock">? Subscribe To My Youtube</a>
    </li>
    <li>
      <a href="https://www.colbyfayock.com/newsletter/">✉️ Sign Up For My Newsletter</a>
    </li>
    <li>
      <a href="https://github.com/sponsors/colbyfayock">? Sponsor Me</a>
    </li>
  </ul>
</div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Organize and Sync SVG Files with Iconset ]]>
                </title>
                <description>
                    <![CDATA[ SVG is an awesome way to bring vector images into a design and development workflow. But collecting and organizing SVG files on your computer can be challenging.  How can Iconset help take away the pain and get us more productive? What is SVG? What ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-organize-and-sync-svg-files-with-iconset/</link>
                <guid isPermaLink="false">66b8e3620a89d796f29a16f5</guid>
                
                    <category>
                        <![CDATA[ Icons ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Productivity ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Colby Fayock ]]>
                </dc:creator>
                <pubDate>Tue, 08 Sep 2020 15:47:35 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/09/iconset.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>SVG is an awesome way to bring vector images into a design and development workflow. But collecting and organizing SVG files on your computer can be challenging. </p>
<p>How can Iconset help take away the pain and get us more productive?</p>
<ul>
<li><a class="post-section-overview" href="#heading-what-is-svg">What is SVG?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-iconset">What is Iconset?</a></li>
<li><a class="post-section-overview" href="#heading-what-are-we-going-to-learn">What are we going to learn?</a></li>
<li><a class="post-section-overview" href="#heading-part-1-getting-started-with-iconset">Part 1: Getting started with Iconset</a></li>
<li><a class="post-section-overview" href="#heading-part-2-adding-icons-to-iconset">Part 2: Adding icons to Iconset</a></li>
<li><a class="post-section-overview" href="#heading-part-3-using-iconset-with-design-software-like-figma">Part 3: Using Iconset with design software like Figma</a></li>
<li><a class="post-section-overview" href="#part-4-using-iconset-in-development-like-react">Part 4: Using Iconset in development like with React</a></li>
<li><a class="post-section-overview" href="#heading-part-5-syncing-iconset-across-multiple-computers-with-dropbox">Part 5: Syncing Iconset across multiple computers with Dropbox</a></li>
</ul>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/KXBf5l4rbL4" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-what-is-svg">What is SVG?</h2>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/SVG">SVG</a> is a nearly 20 year old image file format. And while it’s been around for a while, it has only recently been gaining momentum in the development  community.</p>
<p>SVG is great for a number of reasons. First of all, it’s a vector format, meaning it scales as big or small as you want. </p>
<p>But it’s also flexible in that you can use SVG right in your development project with a typically small file size and infinite scale. You can even <a target="_blank" href="https://frontend.horse/issues/6/#slash">animate it</a>!</p>
<p>But trying to collect and organize a bunch of files can be challenging. What do you name them? Do you have a computer that can easily preview them? What about search?</p>
<h2 id="heading-what-is-iconset">What is Iconset?</h2>
<p><a target="_blank" href="https://iconset.io/">Iconset</a> is a free cross-platform tool that allows you to collect and manage all of your SVG files in one place.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-font-awesome.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset library</em></p>
<p>Love to quickly move between <a target="_blank" href="https://fontawesome.com/">Font Awesome</a> and <a target="_blank" href="https://heroicons.com/">heroicons</a> but don’t want to keep switching libraries? You can use Iconset to make a quick search and drag it right into your project.</p>
<p>If you’re planning on using it for a <a target="_blank" href="https://reactjs.org/">React</a> project, you can even copy the file as JSX and dump it right into your project!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-copy-as.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset "Copy As"</em></p>
<h2 id="heading-what-are-we-going-to-learn">What are we going to learn?</h2>
<p>We’re going to walk through a few different scenarios that'll show us how Iconset is useful. </p>
<p>We’re also going to walk through how you can easily manage Iconset between different computers or environments so you can have the same great library anywhere you work.</p>
<h2 id="heading-part-1-getting-started-with-iconset">Part 1: Getting started with Iconset</h2>
<p>To get started, you’ll need to first install Iconset locally. It should be a similar installation process as any other application.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-no-icons.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset with no icons</em></p>
<p>Once it’s ready and available locally, you should be able to open it up and see a UI with No Icons, which is expected, as it doesn’t come with any icons out of the box.</p>
<h2 id="heading-part-2-adding-icons-to-iconset">Part 2: Adding icons to Iconset</h2>
<p>Adding icons to Iconset is as easy as dragging in, but you have a few options during the process.</p>
<p>To get started, let’s download the free icon set <a target="_blank" href="https://heroicons.com/">heroicons</a>.</p>
<p>Download at: <a target="_blank" href="https://heroicons.com/">https://heroicons.com/</a>.</p>
<p>On the heroicons website, you should see a big Download all button, which will download a zip file including all of the files.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/download-heroicons.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Downloading heroicons</em></p>
<p>If you navigate to the optimized folder, you’ll see that there are two different versions: solid and outline.</p>
<p>Now to get these into Iconset, select each folder individually and drag it right into Iconset.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-drag-in-icons.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Dragging heroicons into Iconset</em></p>
<p>Once there, you have a few options.</p>
<ul>
<li><strong>Set:</strong> Since this is our first set, you’ll automatically be creating a new one. If you had existing sets, you could import into those sets.</li>
<li><strong>Set Name:</strong> Here we can name the set something that we’ll remember. For this, I recommend naming it “heroicons - Outline”.</li>
<li><strong>Import Options:</strong> these are optional settings, but I typically select the optimize and clean option to make sure any icons are immediately ready to get working with.</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-heroicons.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset with heroicons set</em></p>
<p>And once you click Import, it will do it’s thing, and you’ll now have your first set of icons in Iconset!</p>
<p>You can go ahead and do the same thing with the solid directory so then we’ll now have our two sets ready to go.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-heroicons-solid-outline.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset with Outling and Solid sets of heroicons</em></p>
<p>At this point, you can use Iconset to search through your icons and see all of your files currently available in your collection.</p>
<h2 id="heading-part-3-using-iconset-with-design-software-like-figma">Part 3: Using Iconset with design software like Figma</h2>
<p>The great thing about Iconset is how easy it is to use it with design software like <a target="_blank" href="http://figma.com/">Figma</a>.</p>
<p>If I wanted to add an envelope icon to my website so people could contact me, I could search for the mail icon, and simply drag it onto my canvas:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-drag-icon-into-figma.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Dragging mail icon into Figma</em></p>
<p>I can then treat it like any other vector design element and immediately use it in my project.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/svg-icon-in-figma.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>SVG Mail icon in Figma</em></p>
<h2 id="heading-part-4-using-iconset-in-development-like-with-react">Part 4: Using Iconset in development like with React</h2>
<p>Another cool thing is how easy it is to use in a project like React.</p>
<p>Out of the box, you get a few different ways you can copy the file and use it in development like copying it as JSX.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/nextjs-starter-sass.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Next.js Sass Starter</em></p>
<p>If I feel like my <a target="_blank" href="https://github.com/colbyfayock/next-sass-starter">Next.js Sass Starter</a> could use some icons on the page, I can right-click any icon I want and under Copy As select JSX I can paste it right into my project!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-copy-icon-as-jsx.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Copying icon from Iconset as JSX</em></p>
<p>And while it will need some styling once you drop it in just like any other image or icon, it’s immediately ready to go.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/svg-icon-nextjs-project.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Using icon JSX in Next.js React app</em></p>
<h2 id="heading-part-5-syncing-iconset-across-multiple-computers-with-dropbox">Part 5: Syncing Iconset across multiple computers with Dropbox</h2>
<p>With Iconset, you have the ability to switch between different libraries. But importantly, you can also set the location of your library.</p>
<p>When Iconset creates your library, it stores everything as files and a database on your computer.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-location.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset library folder</em></p>
<p>And inside the Iconset UI, we can both Move and Switch the location we use.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-settings.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset library settings</em></p>
<p>If this is your first time setting up Iconset, you can start by clicking Move and then selecting the location you want to sync it to.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-move-location.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Moving Iconset library location</em></p>
<p>And once you click Move, it will move it to that directory, like a folder on Dropbox, and sync to the cloud like any other folder and file.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-sync-to-dropbox.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Syncing Iconset library with Dropbox</em></p>
<p>Alternatively, if you already have a shared Iconset library or if you’re setting this up on a new computer, you can use the Switch option, and select that option right from Dropbox.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-switch-locations.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Switching Iconset location</em></p>
<p>And once you hit Switch, you’ll now load up your shared library and be ready to get productive.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/iconset-full-library.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Iconset with full library</em></p>
<h2 id="heading-what-else-can-you-do">What else can you do?</h2>
<h3 id="heading-publish-and-share-icon-sets">Publish and share icon sets</h3>
<p>Another cool feature is that you can export sets and share them. If you’ve spent a lot of time on a collection and want to share it with others, export it, publish it, and share it with the community!</p>
<h3 id="heading-more-organization">More organization</h3>
<p>Iconset also supports features like Folders and Favorites. This makes it even easier to group and collect the icons however it makes sense to you to keep you productive.</p>
<p>It also supports tagging, making it even easier to search.</p>
<div id="colbyfayock-author-card">
  <p>
    <a href="https://twitter.com/colbyfayock">
      <img src="https://res.cloudinary.com/fay/image/upload/w_2000,h_400,c_fill,q_auto,f_auto/w_1020,c_fit,co_rgb:007079,g_north_west,x_635,y_70,l_text:Source%20Sans%20Pro_64_line_spacing_-10_bold:Colby%20Fayock/w_1020,c_fit,co_rgb:383f43,g_west,x_635,y_6,l_text:Source%20Sans%20Pro_44_line_spacing_0_normal:Follow%20me%20for%20more%20JavaScript%252c%20UX%252c%20and%20other%20interesting%20things!/w_1020,c_fit,co_rgb:007079,g_south_west,x_635,y_70,l_text:Source%20Sans%20Pro_40_line_spacing_-10_semibold:colbyfayock.com/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_68,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_145,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_222,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_295,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/v1/social-footer-card" alt="Follow me for more Javascript, UX, and other interesting things!" width="2000" height="400" loading="lazy">
    </a>
  </p>
  <ul>
    <li>
      <a href="https://twitter.com/colbyfayock">? Follow Me On Twitter</a>
    </li>
    <li>
      <a href="https://youtube.com/colbyfayock">? Subscribe To My Youtube</a>
    </li>
    <li>
      <a href="https://www.colbyfayock.com/newsletter/">✉️ Sign Up For My Newsletter</a>
    </li>
    <li>
      <a href="https://github.com/sponsors/colbyfayock">? Sponsor Me</a>
    </li>
  </ul>
</div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What Is an SVG File? SVG Image and Tags Explained ]]>
                </title>
                <description>
                    <![CDATA[ SVG SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded directly into HTML ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/svg-basics-what-are-scalable-vector-graphics-and-how-do-you-use-them/</link>
                <guid isPermaLink="false">66c3601a258ebfc3dc8f1fbc</guid>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 31 Jan 2020 22:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9d39740569d1a4ca3693.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="heading-svg"><strong>SVG</strong></h2>
<p>SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded directly into HTML for display or saved to a <code>.svg</code> file and inserted like any other image. </p>
<p>You can write SVG by hand, but more complicated graphics can be designed in vector graphics editors such as Illustrator or InkScape and exported to SVG files or code.</p>
<h2 id="heading-svg-basics"><strong>SVG Basics</strong></h2>
<p>Developers start an SVG graphic with the <code>&lt;svg&gt;</code> tag and XML namespace like so:</p>
<pre><code class="lang-svg"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>The sample also includes a <code>version</code> attribute. The <code>version</code> attribute is optional but it is recommended for complaince with XML specifications.</p>
<p>This sample won’t display anything, it merely established a viewport. You can add <code>height</code> and <code>width</code> attributes to set a display size for the viewport this essentially establishes a canvas for you to work in.</p>
<p>With a viewport in place you can add basic graphics, text, and path elements.</p>
<pre><code class="lang-svg"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
     <span class="hljs-attr">version</span>=<span class="hljs-string">"1.1"</span>
     <span class="hljs-attr">width</span>=<span class="hljs-string">"100%"</span>
     <span class="hljs-attr">viewbox</span>=<span class="hljs-string">"0 0 600 300"</span>
     <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#f7b2c1"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"240"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"60"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"#c1b2f7"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#b2c1f7"</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"15"</span>/&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">text</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"450"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"70"</span> <span class="hljs-attr">font-size</span>=<span class="hljs-string">"20"</span> <span class="hljs-attr">text-anchor</span>=<span class="hljs-string">"middle"</span>&gt;</span>SVG Text is browser readable!<span class="hljs-tag">&lt;/<span class="hljs-name">text</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#b2c1f7"</span>&gt;</span> <span class="hljs-comment">&lt;!-- g is for group --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"2"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M10 170 l590 0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"4"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M10 190 l590 0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">stroke-width</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M10 210 l590 0"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>  
<span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
</code></pre>
<p>You can see the output and play with the code in <a target="_blank" href="https://codepen.io/SgiobairOg/pen/OxbNpW">this codepen</a>.</p>
<p>In the opening <code>svg</code> tag we add a width attribute to set the width of the viewport to 100% of the container width, you can use percentages or pixel widths. The opening svg tag also has <code>viewbox</code> attribute which defines a window through which elements of your svg are visible, in this case, the viewbox spans from (0,0) to (600,300). In SVG space the X-axis starts with zero on the left and increases to the right; the Y-axis starts with zero at the top and increases towards the bottom.</p>
<p>The first new tag is the <code>&lt;rect /&gt;</code> tag which defines a rectangle in the SVG viewport. In this case we define a square which is 10 units from the top and left and 100 units tall and wide. The <code>fill</code> attribute sets the fill color for the shape.</p>
<p>Next we define a circle or oval with the <code>&lt;circle /&gt;</code> tag. The sample defines a circle centered at (240,60) with a radius of 50 units. The <code>stroke</code> and <code>stroke-width</code> attributes set a stroke color and a size for the stroke.</p>
<p>You can add text to the graphic with the <code>text</code> tag. The sample text is anchored from the middle of the text to a point at (450, 70) and has a font size of 20 units. The nice thing about text in SVG is it will scale with the rest of your graphic, but it is still readable by the browser and web bots.</p>
<p>When you want to apply the same attributes or CSS styles to multiple SVG elements you can group them with the <code>&lt;g&gt;</code> tag. Attributes assigned to the <code>&lt;g&gt;</code> tag, like the <code>stroke</code> attribute in the example, will be applied to all elements within the group. In this case three <code>&lt;path /&gt;</code> elements.</p>
<p>The <code>&lt;path /&gt;</code> element defines a vector path in the viewport. The path is defined by the <code>d</code> attribute. In the first example the definition reads ‘move to the absolute coordinate (10, 170) <em>and</em> draw a line to the relative coordinates 590 in the X direction and 0 in the Y direction.</p>
<p>The following commands can be used to create your path:</p>
<p>M = move to L = line to H = horizontal line to V = vertical line to Z = close path C = (cubic bezier) curve to S = smooth curve to Q = quadratic bezier curve to T = smooth quadratic bezier curve to A = arc</p>
<h3 id="heading-the-canvas-element"><strong>The canvas element</strong></h3>
<p>Canvas graphics can be drawn onto a</p>
<p>A context is created through the getContext method on the</p>
<pre><code class="lang-text">&lt;p&gt; Before canvas&lt;/p &gt;
&lt;canvas width ="120" height ="60"&gt; &lt;/canvas&gt;
&lt;p &gt;After canvas&lt;/p&gt;
&lt;script&gt;
    var canvas = document.querySelector("canvas");
    var context = canvas.getContext("2d");
    context.fillStyle = "red";
    context.fillRect (10, 10, 100, 50);
&lt;/script&gt;
</code></pre>
<p><img src="http://www.crwflags.com/fotw/images/s/sly@stt.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After creating the context object, the example draws a red rectangle 100 pixels wide and 50 pixels high, with its top-left corner at coordinates (10,10).</p>
<h3 id="heading-drawing-a-pie-chart"><strong>Drawing a pie chart</strong></h3>
<p>The results variable contains an array of objects that represent the survey responses.</p>
<pre><code class="lang-text">var results = [
{ name : "Satisfied", count: 1043, color: "lightblue"} ,
{ name : Neutral", count: 563 , color: "lightgreen"} ,
{ name : Unsatisfied", count: 510 , color: "pink"} ,
{ name : "No comment", count: 175 , color: "silver"}
];
</code></pre>
<p>To draw a pie chart, we draw a number of pie slices, each made up of an arc and a pair of lines to the center of that arc. We can compute the angle taken up by each arc by dividing a full circle (2 π ) by the total number of responses and then multiplying that number (the angle per response) by the number of people who picked a given choice.</p>
<pre><code class="lang-text">&lt;canvas width ="200" height ="200"&gt;&lt;/canvas&gt;
&lt;script&gt;
    var cx = document.querySelector("canvas").getContext("2d");
    var total = results.reduce(function (sum, choice) {
    return sum + choice.count;
    }, 0);

    // Start at the top

    var currentAngle = -0.5 * Math.PI;
    results.forEach (function (result) {
    var sliceAngle = (result.count / total) * 2 * Math.PI;
    cx.beginPath() ;
    // center = 100, 100, radius = 100
    // from current angle, clockwise by slice's angle
    cx.arc(100, 100, 100, currentAngle, currentAngle + sliceAngle);
    currentAngle += sliceAngle;
    cx.lineTo(100, 100);
    cx.fillStyle = result.color ;
    cx.fill() ;
    });
&lt;/script&gt;
</code></pre>
<p>This draws the following chart:</p>
<p><img src="https://pbs.twimg.com/media/CTDvkA8UwAAdJg5.png" alt="Image" width="900" height="860" loading="lazy"></p>
<h3 id="heading-browser-support"><strong>Browser Support</strong></h3>
<p><a target="_blank" href="https://caniuse.com/#feat=svg">Browser support for SVG</a> is available in all modern browsers. There are some issues with scaling in IE 9 through IE 11 however they can be overcome with the use of the <code>width</code>, <code>height</code>, <code>viewbox</code>, and CSS.</p>
<h2 id="heading-editors"><strong>Editors</strong></h2>
<ul>
<li><a target="_blank" href="https://vectr.com/">Vectr</a> - web and desktop tool fot creating and editing SVG graphics, free of charge</li>
</ul>
<h2 id="heading-tools-to-create-svg"><strong>Tools to create SVG</strong></h2>
<p>There are few tools available to create SVG in the form of drawing program.</p>
<ul>
<li><a target="_blank" href="https://www.inkscape.org/">Inkscape</a> - It is an open source tool for state-of-the-art vector drawing with an easy to use graphical interface.</li>
<li><a target="_blank" href="https://www.adobe.com/products/illustrator/">Adobe Illustrator</a> - Adobe Illustrator is a commercial tool for Vector Imagery.</li>
</ul>
<p>For more tools, refer to <a target="_blank" href="https://https//www.w3.org/Graphics/SVG/WG/wiki/Implementations">W3C list of tools that supports SVG</a></p>
<h2 id="heading-why-you-should-use-svgs"><strong>Why you should use SVGs</strong></h2>
<p>As a vector image format, it allows you to resize an image without any loss of quality and a particularly light weight. As an XML format, it allows you to benefit from the full power of JavaScript and especially CSS.</p>
<h2 id="heading-more-info-on-svgs">More info on SVGs:</h2>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/a-fresh-perspective-at-why-when-and-how-to-use-svg/">Why you should use SVG images</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/things-you-need-to-know-about-working-with-svg-in-vs-code-63be593444dd/">What you need to know to work with SVG in VS Code</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/how-to-make-your-fancy-svg-button-accessible-83c9172c3c15/">How to make your fancy SVG button accessible</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ SVG Rectangle and Other SVG Shapes ]]>
                </title>
                <description>
                    <![CDATA[ Several shapes can be created using SVG drawing. An SVG drawing can use and combine seven shapes: Path, Rectangle, Circle, Ellipse, Line, Polyline, and Polygon. Path The path element is the most commonly seen, and is usually generated by programs des... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/svg-rectangle-and-other-svg-shapes/</link>
                <guid isPermaLink="false">66c3601d9539e75f2cc24a17</guid>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 25 Jan 2020 22:34:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9d89740569d1a4ca3848.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Several shapes can be created using SVG drawing. An SVG drawing can use and combine seven shapes: Path, Rectangle, Circle, Ellipse, Line, Polyline, and Polygon.</p>
<h3 id="heading-path"><strong>Path</strong></h3>
<p>The <code>path</code> element is the most commonly seen, and is usually generated by programs designed to export SVG code.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z"</span> /&gt;</span>
</code></pre>
<p>The example <code>path</code> above will generate a “plus” symbole (+) when used inside an SVG drawing. SVG <code>path</code> elements are not built manually, but generated through design programs that can manipulate vector graphics, such as Illustrator or Inkscape.</p>
<h3 id="heading-rectangle"><strong>Rectangle</strong></h3>
<p>The rectangle element <code>rect</code> draws a rectangle on the screen, and it accepts six attributes.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">rect</span> <span class="hljs-attr">x</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">y</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">rx</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">ry</span>=<span class="hljs-string">"10"</span> /&gt;</span>
</code></pre>
<p><code>x</code> and <code>y</code> assign the position of the top-left corner of the rectangle, and <code>width</code> and <code>height</code> assign the size of the rectangle. <code>rx</code> and <code>ry</code> assign the radius of the rectangle corners, similar to the CSS border-radius property.</p>
<h3 id="heading-circle"><strong>Circle</strong></h3>
<p>The circle element <code>circle</code> accepts three attributes.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">circle</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">r</span>=<span class="hljs-string">"50"</span> /&gt;</span>
</code></pre>
<p><code>cx</code> and <code>cy</code> assign the position of the center of the circle, and <code>r</code> assigns the radius (size) of the circle.</p>
<h3 id="heading-ellipse"><strong>Ellipse</strong></h3>
<p>The ellipse element, <code>ellipse</code>, is similar to the <code>circle</code> element except the radius is split into two attributes.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">ellipse</span> <span class="hljs-attr">cx</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">cy</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">rx</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">ry</span>=<span class="hljs-string">"20"</span> /&gt;</span>
</code></pre>
<p>Again, <code>cx</code> and <code>cy</code> assign the position of the center of the ellipse, and now <code>rx</code> and <code>ry</code> assign the horizontal and vertical radius of the ellipse, respectively. A larger <code>rx</code> will give a “fat” ellipse, and a larger <code>ry</code> will give a skinnier ellipse. If <code>rx</code> and <code>ry</code> are equal, it will form a circle.</p>
<h3 id="heading-line"><strong>Line</strong></h3>
<p>The <code>line</code> element is simple, and accepts four attributes.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"100"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"70"</span> /&gt;</span>
</code></pre>
<p>The <code>x1</code> and <code>y1</code> attributes assign the first point of the line, and the <code>x2</code> and <code>y2</code> attributes assign the second point of the line.</p>
<h3 id="heading-polyline"><strong>Polyline</strong></h3>
<p>A <code>polyline</code> is a series of connected straight lines, assigned in a single attribute.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">polyline</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0 100, 50 70, 60 40, 20 0"</span> /&gt;</span>
</code></pre>
<p>The <code>points</code> attribute assigns a list of points, each point separated by a comma.</p>
<h3 id="heading-polygon"><strong>Polygon</strong></h3>
<p>The <code>polygon</code> element is also a series of connected straight lines, but in this case, the last line will automatically reconnect to the initial point.</p>
<pre><code class="lang-svg">  <span class="hljs-tag">&lt;<span class="hljs-name">polygon</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"0 100, 50 70, 60 40, 20 0"</span> /&gt;</span>
</code></pre>
<p>This example will draw the same shape as the <code>polyline</code> above, but it will draw an extra line to “close” the series of lines.</p>
<h2 id="heading-more-information"><strong>More Information</strong></h2>
<p>MDN Documentation: <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Shapes">MDN</a></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
