-
Hajipur, Bihar, 844101
SVG stands for Scalable Vector Graphics. It is an XML-based format used to draw shapes, lines, icons, logos and illustrations directly in the browser. Unlike images made of pixels, SVG graphics are made of mathematical paths. This means they do not lose quality when resized. SVG is ideal for responsive websites, dashboards, diagrams and anywhere you need sharp graphics at any size. In this chapter, you will learn how SVG works, how to write SVG code, how to draw common shapes and how it differs from canvas.
SVG is a markup language, similar to HTML, but created for graphics. Shapes in SVG are elements like <circle>, <rect>, <line> and <path>. You style these shapes using attributes such as fill, stroke and stroke-width. The browser reads the SVG and draws each shape as a separate object. Because SVG elements live in the DOM, you can style them with CSS and interact with them with JavaScript.
<svg width="200" height="150">
<circle cx="80" cy="75" r="40" fill="lightblue" />
</svg>
This simple example shows a circle drawn inside an SVG container.
SVG is useful because it stays sharp at any size. If you zoom in, the edges remain crisp. This makes SVG a popular choice for:
icons
charts
logos
buttons
UI illustrations
interactive diagrams
Because SVG can be animated and styled with CSS, it works well for modern web interfaces.
Every SVG graphic starts with the <svg> tag. You define its width and height like a normal element. SVG also supports a special attribute called viewBox that helps control the internal drawing area.
<svg width="300" height="200" viewBox="0 0 300 200">
</svg>
The viewBox controls scaling. It is very useful when you want your SVG to resize properly on different screens.
Rectangles are created using the <rect> element. You can control position, size, corner radius and colors.
<svg width="300" height="200">
<rect x="20" y="20" width="120" height="80" fill="lightgreen" stroke="black" />
</svg>
x and y define the position. rx and ry can be added for rounded corners.
SVG has separate elements for circles and ellipses.
<svg width="200" height="150">
<circle cx="80" cy="70" r="40" fill="orange" />
</svg>
<svg width="200" height="150">
<ellipse cx="100" cy="75" rx="60" ry="30" fill="pink" />
</svg>
Circles use one radius. Ellipses use two (horizontal and vertical).
Lines are created using the <line> element. For more complex shapes, you can use <polyline>.
<svg width="300" height="150">
<line x1="20" y1="20" x2="200" y2="20" stroke="red" stroke-width="3" />
</svg>
<svg width="300" height="150">
<polyline points="20,20 100,50 180,20 260,80"
fill="none" stroke="blue" stroke-width="2" />
</svg>
A polyline is useful for graphs and diagrams.
Polygons are closed shapes with straight lines.
<svg width="300" height="150">
<polygon points="30,30 80,100 130,30"
fill="yellow" stroke="black" />
</svg>
This example shows a triangle.
The <path> element is the most powerful drawing tool in SVG. It uses commands to draw curves, straight lines and complex shapes.
<svg width="300" height="200">
<path d="M20 20 L150 20 C200 60 200 100 150 140"
stroke="purple" fill="none" />
</svg>
Some basic path commands:
M – move to
L – line to
C – curve
Z – close path
Most icons and logos you see on websites are made using <path>.
SVG shapes support these styling attributes:
fill
stroke
stroke-width
stroke-linejoin
stroke-linecap
<svg width="200" height="150">
<rect x="20" y="20" width="120" height="60"
fill="skyblue" stroke="black" stroke-width="3"
stroke-linejoin="round" />
</svg>
These styling options give you complete control over the appearance.
SVG text behaves like normal shapes. You can position it anywhere.
<svg width="300" height="150">
<text x="20" y="40" font-size="24" fill="green">
SVG Title
</text>
</svg>
You can rotate, animate and style this text using CSS.
SVG supports images using the <image> tag.
<svg width="300" height="200">
<image href="photo.jpg" x="20" y="20" width="150" height="100" />
</svg>
This is helpful when mixing vector graphics with photos.
SVG supports animation through the <animate> element. You can animate colors, positions, sizes and more.
<svg width="200" height="150">
<circle cx="40" cy="70" r="20" fill="tomato">
<animate attributeName="cx" from="40" to="160" dur="2s" repeatCount="indefinite" />
</circle>
</svg>
This moves the circle left to right continuously.
Both SVG and canvas can draw graphics, but they work differently.
sharp at any size
easier for diagrams and UI icons
supports CSS styling
supports interactivity with DOM events
better for animations
faster for large data
ideal for games and continuous drawing
Choose SVG for static designs or UI elements. Choose canvas for dynamic graphics.
<svg width="400" height="220">
<rect x="20" y="20" width="120" height="70" fill="lightblue" stroke="black" />
<circle cx="200" cy="60" r="40" fill="pink" />
<line x1="20" y1="130" x2="350" y2="130" stroke="gray" stroke-width="2" />
<text x="20" y="180" font-size="20" fill="black">
SVG Demo Graphic
</text>
</svg>
This combines shapes and text to form a simple illustration.
SVG allows you to create sharp, scalable graphics directly in your web pages. You learned how to draw rectangles, circles, ellipses, lines, polygons, polylines and paths. You also learned how to style these shapes, add text, embed images and even animate elements. SVG is ideal for icons, logos, diagrams and responsive graphics that stay crisp on any screen size.
Q1. Draw a red rectangle of 200x100 size using SVG.
Q2. Create a green circle with a radius of 40 pixels.
Q3. Add a line from top-left to bottom-right of a 200x100 area.
Q4. Display the text "Welcome to SVG" using the <text> element.
Q5. Create a blue ellipse with rx=50 and ry=25.
Q6. Use <polygon> to create a triangle.
Q7. Style an SVG shape using CSS instead of style attribute.
Q8. Animate a circle’s radius using CSS or JavaScript.
Q9. Use <path> to draw a complex wave shape.
Q10. Create a group of SVG shapes using <g> tag and apply a transformation.