Learn to Create SVG by Coding in Minutes
Tags: coding, svgcoding, animation, webdev
Published: 2024-07-04 05:21:59

**_Learn full SVG creating by coding and adding effects to it like animations._**

![A post by Halim Shams](https://cdn-images-1.medium.com/max/1024/1*-qLA36JMdX_jzWw3vrQMqA.png)

Scalable Vector Graphics or SVG is an image format that is like HTML for 2d graphics.

They differ from raster images like PNG, JPEG… that is use a grid of tiny pixels to create an image and as you zoom in, the pixels become larger making the image grainy in contrast, a vector image can be scaled to any size without losing its resolution because instead of fixed pixels its appearance is based on geometry.

![The Different Between SVG and Raster Image](https://cdn-images-1.medium.com/max/1024/1*IJT-qHsIGtqxiKgu4x1zLA.png)

You can make SVG by tools like Figma or illustrator, or by writing the code directly, which is easier than you might think and opens the door to animation and interactivity.

Create one by opening an SVG tag, then define a coordinate system with the _“viewBox”_ attribute that gives us a frame with a _“width”_ and _“height”_ of 100 units on which we can draw graphics.

![Starting with SVG tag](https://cdn-images-1.medium.com/max/1024/1*7qMIZPG4u2VNs1rWZcyhkA.png)

Draw basic shapes by adding elements like rectangle, circle and polygon.

![Basic Shapes in SVG](https://cdn-images-1.medium.com/max/1024/1*1sK-tfEOjSkInpdZwsahag.png)

Position the rectangle by its _“x-y”_ values on the _“viewBox”_, then give it a size which can take value that are either explicit or responsive, we can change the color of the shape by defining its _“fill”_ or define an outline with the _“stroke”_ attribute.

![Designing your first rectangle](https://cdn-images-1.medium.com/max/1024/1*z6ubz16MBY7aZ8qoAnUMeg.png)

And if our styling gets too complex, we can extract everything into a separate CSS stylesheet, by applying a class to it just like any HTML element.

![Extracting into CSS stylesheet](https://cdn-images-1.medium.com/max/1024/1*x7kfpjXwtuOO2nftll0p9Q.png)

We have the full power of CSS at our fingertips, which means we can react to events on the shapes and then change their styling or animation accordingly.

![Adding some effects to our SVG](https://cdn-images-1.medium.com/max/1024/1*bsdGLg7HpjC3yXqOhhAL8A.png)

But most graphics are more than just basic shapes, they contain complex art work with all kinds of twists and turns, and that’s where the path element comes in.

The shape of a path is determined by one attribute _“d”_ for draw. Coding a path is like controlling the tip of a pen with a series of commands.

The most basic command is _“M”_ for move which will move the pen tip to an _“x-y”_ coordinate, an upper letter means move relative to the view box and a lowercase letter means move relative to the last point in the path, but _“move (M)”_ doesn’t draw anything.

![The Move attribute](https://cdn-images-1.medium.com/max/1024/1*Izf3tth-ApjynU4gg73MCw.png)

To put the pen tip down on the paper and draw something use the _“I”_ command, it works exactly like _“move”_ but draws a line that can be styled.

![The Line attribute to start drawing](https://cdn-images-1.medium.com/max/1024/1*nRh1ftSOlVm6uEiVi13K6w.png)

Straight lines are cool but what if we wanted to add a curve like we would with the handles in illustrator, Create Bézier curves with the _“C”_ and _“Q”_ commands, define the position of two control points, then _“x”_ and _“y”_ coordinates where the curve should end, then SVG will automatically calculate a smooth curve for you at any scale.

![Creating Bézier curves in SVG](https://cdn-images-1.medium.com/max/1024/1*8RDmA0g_nyjjO60Bp7o9qQ.png)

---

This was **SVG** (Scalable Vector Graphics) in minutes.

Hope reading this short article helped you to know what really SVG is and able to create a simple SVG. 😊

Don’t forget to support and inspire me by just following me, clapping and sharing your experience in the comment section. 🥰

Thanks for reading till the end and if you see any issue with this article you can share with me in the comments. 🙏

> … HAPPY CODDING :) …

— You can follow me on [Twitter/X](https://x.com/HalimOFFI) also.😊
— Check out my [newsletter](https://halimshams.substack.com/) as well! 💛