HTML SVG Tutorial
Learn how to create scalable vector graphics directly in your HTML
Introduction to SVG
SVG (Scalable Vector Graphics) is an XML-based markup language for describing two-dimensional vector graphics. SVG is resolution-independent and can scale to any size without losing quality.
Basic SVG Example:
Basic SVG Examples
1. Basic Shapes
SVG includes basic shape elements like rectangles, circles, and lines.
2. Path Element
The <path> element is the most powerful SVG element, allowing you to create complex shapes.
3. Text and Styling
SVG includes powerful text capabilities and CSS styling options.
SVG Elements Reference
Element | Description | Example |
---|---|---|
<svg> | The container element that defines the SVG document | <svg width="100" height="100"></svg> |
<rect> | Creates a rectangle | <rect x="10" y="10" width="80" height="60" fill="blue" /> |
<circle> | Creates a circle | <circle cx="50" cy="50" r="40" fill="red" /> |
<ellipse> | Creates an ellipse | <ellipse cx="50" cy="50" rx="40" ry="30" fill="green" /> |
<line> | Creates a line | <line x1="10" y1="10" x2="90" y2="90" stroke="black" /> |
<polyline> | Creates a series of connected straight lines | <polyline points="10,10 40,40 10,70" fill="none" stroke="black" /> |
<polygon> | Creates a closed shape consisting of connected straight lines | <polygon points="50,10 90,90 10,90" fill="yellow" /> |
<path> | Creates complex shapes using path commands | <path d="M10 10 L90 10 L90 90 L10 90 Z" fill="blue" /> |
<text> | Creates text | <text x="10" y="50" font-family="Arial" font-size="20">Hello</text> |
<g> | Groups SVG elements together | <g fill="red"><circle cx="50" cy="50" r="40" /></g> |
<defs> | Defines reusable elements | <defs><linearGradient id="grad1">...</linearGradient></defs> |
Advanced SVG Techniques
1. Gradients and Patterns
SVG supports linear and radial gradients, as well as pattern fills.
2. Filters and Effects
SVG provides powerful filter effects like blurring, lighting, and more.
3. SVG Animation
SVG can be animated using SMIL (Synchronized Multimedia Integration Language) or CSS.
Conclusion
SVG is a powerful tool for creating resolution-independent graphics that can be styled with CSS and animated with JavaScript. It's perfect for icons, logos, charts, and complex illustrations.