HTML SVG


SVG (Scalable Vector Graphics) is an XML-based format used to define vector-based graphics for the web. Unlike raster images (e.g., PNG, JPEG), SVG graphics do not lose quality when scaled or zoomed.

SVG is used to create lines, shapes, text, and images that are scalable, stylable, and scriptable using CSS and JavaScript.


🔹 Why Use SVG?

  • Scales without losing quality (perfect for logos and icons)

  • Easily styled with CSS

  • Can be animated with JavaScript or CSS

  • Smaller file size for simple graphics


🔹 Basic SVG Syntax

<svg width="200" height="100">
  <rect width="200" height="100" style="fill:lightblue;" />
</svg>

🔹 Common SVG Elements

Element Description
<svg> Container for SVG content
<rect> Draws a rectangle
<circle> Draws a circle
<ellipse> Draws an ellipse
<line> Draws a line
<polyline> Series of connected straight lines
<polygon> Closed shape with multiple sides
<path> Defines complex shapes and curves
<text> Adds text to SVG

💻 Examples

✅ Rectangle
<svg width="300" height="100">
  <rect width="300" height="100" style="fill:tomato;" />
</svg>

✅ Circle
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" fill="green" />
</svg>

✅ Line
<svg height="100" width="200">
  <line x1="0" y1="0" x2="200" y2="100" stroke="black" stroke-width="2" />
</svg>

✅ Text
<svg width="300" height="100">
  <text x="20" y="50" font-family="Arial" font-size="24" fill="blue">
    Hello SVG
  </text>
</svg>

✅ Path (Custom Shapes)
<svg width="200" height="200">
  <path d="M10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80"
        stroke="black" fill="transparent"/>
</svg>

Practice Questions

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.


Go Back Top