SVG is one of the most useful technologies in modern web development. It allows developers to create graphics directly using code rather than relying only on image formats like PNG or JPEG.
Whether you're building a simple website, designing icons, or creating interactive illustrations, understanding SVG can make your frontend development skills much stronger.
I'll explain SVG step by step with some examples.
What is SVG?
SVG stands for Scalable Vector Graphics.
It is an XML-based image format used to create two-dimensional graphics using mathematical shapes, paths, and coordinates.
Unlike PNG and JPEG, which are raster images made up of pixels, SVG graphics are vector-based.
It means SVG images can be scaled up or down without losing their quality.
For example,
- A company logo can look sharp on a mobile screen and a large desktop monitor.
- An icon can be resized without becoming blurry.
- An illustration can be animated using CSS or JavaScript.
SVG Example
Here's a simple SVG example.
<svg width="200" height="120">
<circle
cx="100"
cy="60"
r="40"
fill="blue"
/>
</svg>Result
Here's what is happening.
svgcreates the SVG drawing area.widthdefines its width.heightdefines its height.circlecreates a circular shape.cxdefines the horizontal center.cydefines the vertical center.rdefines the radius.filldefines the circle's color.
That's it. This is how you create an SVG.
Understanding the SVG viewBox
The viewBox is one of the most important SVG attributes. The viewBox attribute defines the internal coordinate system of the SVG.
See this example.
<svg width="680" height="260" viewBox="0 0 680 260">
<circle
cx="340"
cy="130"
r="80"
fill="tomato"
/>
</svg>Result
The "viewBox" attribute accepts four values.
viewBox="min-x min-y width height"
- 0: Starting X coordinate
- 0: Starting Y coordinate
- 680: Internal width
- 260: Internal height
You can think of "viewBox" as a virtual canvas on which you draw your graphics.
Why the viewBox is important?
Because it allows SVG graphics to scale according to the available display size while maintaining their proportions. For example, you can display the same SVG at 200 pixels or 600 pixels without manually changing the coordinates of its shapes.
Create Different Shapes with SVG
SVG provides several built-in elements for creating shapes.
Example 1. Circle
<svg viewBox="0 0 200 150"> <circle cx="100" cy="75" r="50" fill="orange" stroke="black" stroke-width="3" /> </svg>
Example 2. Rectangle
The rect element creates a rectangle. The rx attribute adds rounded corners.
<svg viewBox="0 0 200 150"> <rect x="30" y="25" width="140" height="100" rx="12" fill="skyblue" /> </svg>
Example 3. Line
The line element draws a straight line between two points.
<svg viewBox="0 0 200 150"> <line x1="20" y1="20" x2="180" y2="130" stroke="red" stroke-width="5" /> </svg>
Example 4. Polygon (Attribute)
A polygon connects multiple points to create a closed shape. You can use the "polygon" attribute to create triangles, stars, and many other geometric shapes.
<svg viewBox="0 0 200 150"> <polygon points="100,10 190,130 10,130" fill="purple" stroke="black" stroke-width="3" /> </svg>
Understanding SVG Paths
Now let's explore and understand one of the most powerful SVG elements, the path.
The "path: element allows you to create almost any shape, including complex illustrations, curves, icons, and logos. For example,
<svg viewBox="0 0 200 150"> <path d="M 20 100 L 100 20 L 180 100 Z" fill="orange" stroke="black" stroke-width="3" /> </svg>
| Command | Meaning |
|---|---|
| M | Move to a coordinate |
| L | Draw a line to a coordinate |
| H | Draw a horizontal line |
| V | Draw a vertical line |
| C | Draw a cubic Bézier curve |
| Q | Draw a quadratic Bézier curve |
| Z | Close the path |
M 20 100 moves to coordinate (20, 100).
Similarly, L 100 20 draws a line to (100, 20).
The Z closes the shape.
SVG paths are especially useful when creating custom icons and illustrations.
Add Text Inside SVG
You can write text inside an SVG. For example,
<svg viewBox="0 0 400 120"> <text x="20" y="15" font-size="20" font-weight="bold" fill="royalblue"> Hello, I am Arun Banik. </text> </svg>
You can customize the text's font size, font family, font weight, and text color etc. This is useful for creating labels, graphical headings, and custom illustrations.
Create an SVG Icon
Now let's create a simple Home icon using SVG.
<svg viewBox="0 0 100 100" width="100" height="100" fill="none" stroke="currentColor" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"> <path d="M10 45 L50 10 L90 45" /> <path d="M20 40 V90 H80 V40" /> <path d="M42 90 V60 H58 V90" /> </svg>
Here we are combining multiple paths to create one simple icon.
These attributes are important.
stroke-linecapcontrols the appearance of line endpoints.stroke-linejoincontrols how lines connect.currentColorallows the SVG to inherit its color from CSS.
This is good for creating reusable website icons.
SVG Animation using CSS
SVG graphics can also be animated. For example, let's create a bouncing circle.
<svg viewBox="0 0 500 150"> <circle class="ball" cx="100" cy="50" r="20" fill="orange" /> </svg> <style> .ball { animation: bounce 1s infinite alternate ease-in-out; } @keyframes bounce { from { transform: translateY(0); } to { transform: translateY(60px); } } </style>
Result
What are the Advantages of SVG?
Here are some important benefits of using SVG.
- Resolution independent: Graphics remain sharp at different sizes.
- Lightweight: Simple SVG graphics can have very small file sizes.
- CSS support: You can style SVG elements using CSS.
- JavaScript support: SVG elements can be manipulated dynamically.
- Animation: You can animate SVG graphics using CSS and JavaScript.
- Accessibility: SVG can include titles and descriptions for assistive technologies.
- Responsive: SVG can adapt to different screen sizes using
viewBox.
Important SVG elements to remember.
| Element | Purpose |
|---|---|
<g> |
Groups SVG elements |
<circle> |
Creates circles |
<rect> |
Creates rectangles |
<ellipse> |
Creates ellipses |
<line> |
Creates lines |
<path> |
Creates custom shapes |
<text> |
Displays text |
<defs> |
Stores reusable definitions |
