HTML
π What is SVG?
SVG stands for Scalable Vector Graphics.
This means graphics are defined using mathematical shapes rather than pixels, so they can be resized without losing quality.
SVG is based on XML, which means shapes like circles, rectangles, and lines are created using tags./p>
Each element inside SVG is treated as a separate object.
π Because of this:
- You can style SVG elements with CSS
- You can manipulate them with JavaScript
- You can interact with them just like normal HTML elements
SVG works seamlessly with modern web technologies such as the DOM, CSS, and JavaScript.
π Basic Structure & Examples
πΉ 1. <svg> Container
All SVG graphics are placed inside the <svg> element.
π This creates a drawing area of 200Γ200 pixels.
πΉ 2. Drawing a Circle
π Explanation:
- cx, cy β center position
- r β radius
- stroke β border color
- fill β inside color
This creates a yellow circle with a green border.
πΉ 3. Rectangle
π
- x, y β starting position
- width, height β size
Rounded Corners Example
HTML
<rect x="10" y="10" width="180" height="80"
rx="20" ry="20"
fill="red" opacity="0.7" />
π rx and ry make the corners rounded.
πΉ 4. Polygon (Example: Star Shape)
π The points attribute defines all corner coordinates.
πΉ 5. Gradient and Text
π
- Gradients are defined inside
<defs> - Shapes can use them via fill="url(...)"
-
<text>allows adding and styling text
Summary
- SVG is used for vector-based graphics
- It does not lose quality when resized
- Each shape is an independent, controllable element
- Works perfectly with CSS, JavaScript, and DOM
Ideal for:
- Icons
- Logos
- Charts
- Interactive visuals
- π SVG is one of the most powerful tools for creating modern, scalable web graphics.
