Site icon Tech Shaadi

Pocket Guide to Writing your own SVG

We live in the age of pixels. As designers & developers of the web, pixels can be both our friends and our enemies. We want everything to look nice and sharp on all devices, but we need to keep file sizes down for performance.

There is pretty much only one way to go with icons, logos and illustrations on the web — SVG. Scalable Vector Graphics can look crisp at all screen resolutions, can have super small file sizes, and can be easily edited and modified.


Citing from the official spec at W3.org, SVG is described as:

SVG is a language based on XML for describing two-dimensional vector and mixed vector/raster graphics. SVG content is stylable, scalable to different display resolutions, and can be viewed stand-alone, mixed with HTML content, or embedded using XML namespaces within other XML languages. SVG also supports dynamic changes; script can be used to create interactive documents, and animations can be performed using declarative animation features or by using script.

It has actually been around since 1999 and as of 16 August 2011, it became a W3C Recommendation.

SVG Advantages:
How to draw Basic Shapes with SVG
  1. Line

To draw a line in SVG we can use the <line> element. This element is used to draw a single straight line, so it will only consist of two points, start and end.

 <svg><line x1="0" y1="0" x2="200" y2="200" stroke-width="1" stroke="rgb(0,0,0)"/></svg>


Line via https://hongkiat.github.io/scalable-vector-graphic/

  1. Polyline

It’s almost similar to the <line>, but with the <polyline> element we can draw multiple lines instead of just one. Here is an example:

<svg><polyline points="0,0 50,0 150,100 250,100 300,150" fill="rgb(249,249,249)" stroke-width="1" stroke="rgb(0,0,0)"/></svg>


Polyline via https://assets.hongkiat.com/uploads/scalable-vector-graphic/svg-polyline.jpg

  1. Circle

We can also draw a circle with the element. In the following example, we will create a circle with 100 radius which is defined with r attribute:

<svg><circle cx="102" cy="102" r="100" fill="rgb(234,234,234)" stroke-width="1" stroke="rgb(0,0,0)"/></svg>


Circle via Circle via https://hongkiat.github.io/scalable-vector-graphic/

  1. Ellipse

We can draw an ellipse with . It works quite similar to the circle, but this time we can control specifically the x line radius and y line radius with rxand ry attribute;

Here is the Link to My First Hand Coded SVG:

Simplified Code:

Circle :
     Radius : 23
     Center Postion 25,25
Polyline:
     pairs of x and y coordinates ="15,60 40,80 85,20"

Actual Code:

<button id="submit">
   <svg class="icon" width="30" height="30" viewBox="0 0 100 100">
       <polyline class="check" fill="none" stroke="#19cc95" stroke-width="10" stroke- miterlimit="20" points="15,60 40,80 85,20" />
   </svg>
<span class="text">Ok, Sure</span>
 <span class="spinner">
     <svg width="50" height="50">
        <circle r="23" cx="25" cy="25" />
     </svg>
 </span>
</button>

Resources

There’s a lot more you can do with SVG so don’t stop here, keep digging and discovering more of the awesome things that can be achieved! If you’re interested in learning more, here are some resources I’ve found very useful:

Exit mobile version