Let's talk about SVGs
Aubrey Barnes

Aubrey Barnes @hbarnesau

About: I'm Aubrey Barnes, a Frontend Developer looking to make the world (wide web) a more beautiful place.

Location:
Lafayette, LA
Joined:
Jul 29, 2019

Let's talk about SVGs

Publish Date: Jan 27
24 5

Good To Knows About SVG's

In my time as a Web Developer, I've been working with SVG's on more projects than not, with no real true understanding of how they work and how to fully utilize what they have to offer. Today, I'm going to share what knowledge I have gained so that whomever may have questions and curiosities like I did will need look no further.

Primarily, when I've needed to use SVG's it was for icons and logos that represent the client and their services rendered.

Why would Scalable Vector Graphics be the go to? There are four primary reasons that I can think of, including

  • Scalability, meaning, regardless of size there is no pixelation or distortion.
  • Accessible, in that SVGs can have alt text and descriptions added just like any other image can, right on the tag attribute.
  • Animations, which add a level of movement and interest that static images cannot do.
  • Interactivity, which would be done through JavaScript. I personally do not use much JavaScript in my workday, so I cannot speak to this one as much. With time I intend to shorten this gap in knowledge.

An example of what an SVG looks like could be something like this:

Wine Glass

The code to render this as an SVG looks like:

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- License: CC0. Made by SVG Repo: https://www.svgrepo.com/svg/11389/wine-glass -->
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="198.484px" height="198.484px" viewBox="0 0 198.484 198.484" style="enable-background:new 0 0 198.484 198.484;"
     xml:space="preserve">
<g>
    <g>
        <path d="M146.208,88.344c10.12-8.688,16.1-20.224,17.295-33.363c2.775-30.533-11.607-52.552-12.222-53.475
            C150.655,0.565,149.601,0,148.472,0H50.012c-1.129,0-2.184,0.565-2.81,1.506c-0.614,0.922-14.997,22.941-12.221,53.475
            c1.194,13.139,7.174,24.675,17.294,33.363c8.785,7.542,20.268,12.65,33.319,14.852v66.337c-11.87,0.581-22.657,1.906-30.646,3.783
            c-10.89,2.557-15.962,5.902-15.962,10.525c0,5.077,6.187,8.661,19.471,11.279c10.901,2.148,25.317,3.339,40.634,3.362v0.001
            c0.051,0,0.301-0.001,0.301-0.001c15.317-0.023,29.733-1.214,40.635-3.362c13.284-2.618,19.47-6.202,19.47-11.279
            c0-4.623-5.072-7.969-15.962-10.525c-7.987-1.876-18.774-3.201-30.644-3.782v-66.338
            C125.942,100.992,137.423,95.885,146.208,88.344z M140.017,179.447c8.598,1.812,11.521,3.627,12.435,4.402
            c-1.256,1.062-5.521,3.216-15.908,5.054c-10.237,1.812-23.475,2.813-37.301,2.829c-13.826-0.014-27.063-1.018-37.301-2.829
            c-10.387-1.838-14.652-3.991-15.908-5.054c0.913-0.775,3.837-2.592,12.433-4.402c7.278-1.534,16.808-2.637,27.129-3.156v4.68
            c0,1.864,1.511,3.375,3.375,3.375s3.375-1.511,3.375-3.375l0.008-80.73c0-1.864-1.511-3.375-3.375-3.375
            c-0.048,0-0.095,0.005-0.142,0.007c-0.004-0.001-0.007-0.002-0.011-0.002c-21.184-2.982-44.687-15.713-47.122-42.501
            C39.543,30.603,48.91,12.02,51.919,6.75h94.646c3.009,5.27,12.376,23.854,10.215,47.619
            c-2.436,26.788-25.938,39.518-47.122,42.501c-0.003,0-0.007,0.001-0.011,0.002c-0.047-0.002-0.094-0.007-0.143-0.007
            c-1.863,0-3.375,1.511-3.375,3.375l0.012,81.181c0,1.864,1.511,3.375,3.375,3.375c1.863,0,3.375-1.511,3.375-3.375v-5.13
            C123.212,176.811,132.74,177.913,140.017,179.447z"/>
        <path d="M142.413,72.616c6.496-9.941,6.572-19.851,6.572-20.268c0-1.852-1.479-3.456-3.33-3.473h-7.453
            c-1.87-0.031-3.393,1.471-3.414,3.336c-0.021,1.864,1.471,3.392,3.335,3.414l3.791,0.064c-1.323,8.367-8.095,29.4-42.673,30.148
            c-34.578-0.748-41.349-21.781-42.673-30.148l64.313-0.064c0.012,0,0.022,0,0.034,0c1.849,0,3.356-1.49,3.374-3.342
            c0.018-1.864-1.479-3.39-3.342-3.408l-68.107,0.098c-1.851,0.017-3.343,1.523-3.343,3.375c0,0.417,0.075,10.326,6.572,20.268
            c5.799,8.874,23.521,19.52,43.022,19.52C118.593,92.136,136.614,81.49,142.413,72.616z"/>
    </g>
</g>
</svg>

Enter fullscreen mode Exit fullscreen mode

Yikes.
So now I'm going to strip out the unnecessary code here, as I prefer to remove whatever is not required for rendering. For instance, style="enable-background:new 0 0 198.484 198.484;" is deprecated as of 2014 (Source). This SVG was downloaded from svgviewer.dev a couple weeks before time of writing.

After stripping out the parts that don't affect presentation, this is what I'm left with.

When you click the HTML tab up there in the codepen box, you may notice that I have also added

fill="currentColor"

to the svg and d tags. This is so that I can control the color of the svg using CSS, where all styling should live. This allows the flexibility of having colors update based on events such as hover, mouseIn, mouseOut, etc.

You'll see in this codepen that I've added in CSS to assign blue to the fill attribute.

If you're feeling more fancy, it's possible to add gradients to our svg. To set that up, we would need to add

<defs>
    <linearGradient id="gradient-blue" x1="0" y1="0" x2="85" y2="85" gradientUnits="userSpaceOnUse">
      <stop offset="0.4" stop-color="#090979"></stop>
      <stop offset="1" stop-color="#00d4ff"></stop>
    </linearGradient>
  </defs>
Enter fullscreen mode Exit fullscreen mode

Inside of the defs tag we have declared a graphics object. This object is a linearGradient, which an id is required to reference the object either in HTML or in the CSS file as is shown in the codepen above. The x and y coordinates on the linearGradient tag are saying "starting from the top left corner (0, 0), I want to extend the gradient 85 units to the right, and 85 units downwards". The next attribute gradientUnits="userSpaceOnUse", as I understand it, avoids distorting the shape when used on svg's that aren't square. I am also not certain of this so please do correct me!

Inside of the linearGradient we have two stops, for each color. Essentially, #090979 up to 40%, and then #00d4ff all the way to the finish line! translated to css: linear-gradient(to bottom right, #090979 40%, #00d4ff 100%);

One last thing, I wanted to animate the gradients on hover, which I can't lie I learned a lot of new things about svg gradients on the fly while writing this. I was not able to transition fill:url on hover, but what I was able to do was animate the stops inside of the linearGradient on hover which was a wild discovery for me. Look at this fine glass of wine.

As far as styling and interactivity is concerned, this is the full extent of my knowledge and experience in working with SVG's, thank you for following along with me, and I hope this has helped at least one other person!

Comments 5 total

  • Anna Villarreal
    Anna VillarrealJan 28, 2025

    Svgs are great. I started investigating them a bit when I wanted custom animations on my github page. I saw that most of those tags/stack declarations everyone has on their github/readme page were just svgs. I decided I needed my own. Very deep rabbit hole those svgs! XD 😆
    stack

  • Best Codes
    Best CodesFeb 4, 2025

    Nice article! Just for fun, I simplified the SVG code some more. The result is about two times smaller:

    <svg width="199" height="199" viewBox="0 0 199 199">
      <path d="M148.5 0H50c-1.1 0-2.2.6-2.8 1.5C46.6 2.4 32.2 24.4 35 55c1.2 13.1 7.2 24.7 17.3 33.4 8.8 7.5 20.3 12.6 33.3 14.8v66.3c-11.9.6-22.7 1.9-30.6 3.8C44 175.9 39 179.2 39 183.8c0 5.1 6.2 8.7 19.5 11.3 10.9 2.1 25.3 3.3 40.6 3.4h.3c15.3 0 29.7-1.2 40.6-3.4 13.3-2.6 19.5-6.2 19.5-11.3 0-4.6-5.1-8-16-10.5-8-1.9-18.8-3.2-30.6-3.8v-66.3c13-2.2 24.5-7.3 33.3-14.8 10.1-8.7 16.1-20.2 17.3-33.4 2.8-30.5-11.6-52.5-12.2-53.5A3.4 3.4 0 0 0 148.5 0zM52 6.8h94.6c3 5.3 12.4 23.9 10.2 47.6-2.4 26.8-25.9 39.5-47.1 42.5h-.2c-1.9 0-3.4 1.5-3.4 3.4l.1 81.2a3.4 3.4 0 0 0 6.7 0v-5.1c10.3.5 19.8 1.6 27.1 3.2 8.6 1.8 11.5 3.6 12.4 4.4-1.3 1.1-5.5 3.2-15.9 5.1-10.2 1.8-23.5 2.8-37.3 2.8-13.8 0-27.1-1-37.3-2.8-10.4-1.8-14.7-4-15.9-5.1.9-.8 3.8-2.6 12.4-4.4 7.3-1.5 16.8-2.6 27.1-3.2v4.7a3.4 3.4 0 0 0 6.8 0V100c0-1.9-1.5-3.4-3.4-3.4h-.2c-21.2-3-44.7-15.7-47.1-42.5C39.5 30.6 49 12 52 6.8z"/>
      <path fill="currentColor" d="M142.4 72.6c6.5-9.9 6.6-19.9 6.6-20.3 0-1.9-1.5-3.5-3.3-3.5h-7.5c-1.9 0-3.4 1.5-3.4 3.3-.1 1.9 1.5 3.4 3.3 3.4l3.8.1c-1.3 8.4-8.1 29.4-42.7 30.1-34.6-.7-41.3-21.8-42.7-30.1l64.3-.1h.1c1.8 0 3.3-1.5 3.3-3.3 0-1.9-1.5-3.4-3.3-3.4l-68.1.1c-1.9 0-3.3 1.5-3.3 3.4 0 .4 0 10.3 6.6 20.3 5.8 8.9 23.5 19.5 43 19.5s37.2-10.6 43-19.5z"/>
    </svg>
    
    Enter fullscreen mode Exit fullscreen mode

    I enjoyed reading this; keep up the good writing!

  • Adam
    AdamFeb 5, 2025

    SVGs are awesome! Really enjoyed reading this article!

  • Besworks
    BesworksMar 13, 2025

    Learning and working with SVG is definitely the path to be on. They can be used to build some amazing interactive things. Here is a demo I put together of a browser-based svg editor. It's not fully featured and does have a few quirks but the code is unminified and contains many examples of how to work with SVG using JS.

  • cz
    czMar 24, 2025

    try SVG viewer & converter

    I use SVG to ICO most.

Add comment