SVG (Scalable Vector Graphics) Tutorial for Beginners 🎯

beginner
9 min

SVG (Scalable Vector Graphics) Tutorial for Beginners 🎯

Welcome to the SVG (Scalable Vector Graphics) tutorial! In this lesson, we'll dive deep into the world of SVG, a versatile graphics format that's essential for web developers. By the end of this tutorial, you'll be able to create, modify, and optimize scalable vector graphics for your projects. Let's get started! 📝

What is SVG? 💡

SVG stands for Scalable Vector Graphics, an XML-based vector graphics format for two-dimensional graphics with support for interactivity and animation. Unlike raster graphics (like PNG and JPEG), SVG graphics are composed of vectors, which are mathematical representations of graphics primitives such as lines, curves, shapes, and images. This makes SVG images scalable and crisp, regardless of the size or resolution.

Why Use SVG? 💡

  1. Scalability: SVG graphics can be resized without losing quality, making them ideal for high-resolution devices and responsive web design.
  2. Small File Size: Compared to raster graphics, SVG files are smaller in size, resulting in faster page load times and reduced bandwidth usage.
  3. Accessibility: SVG supports screen readers, making your content more accessible to users with visual impairments.
  4. Animations and Interactivity: SVG is highly interactive and can be easily animated using CSS, JavaScript, or SMIL (Synchronized Multimedia Integration Language).

SVG Basics 💡

SVG Syntax

An SVG file consists of an XML declaration, an SVG root element, and one or more SVG elements that define the graphics. The root element for an SVG document is <svg>.

xml
<?xml version="1.0" standalone="no"?> <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <!-- Your SVG content here --> </svg>

SVG Attributes

SVG elements have attributes that control various aspects of the graphics, such as dimensions, color, and position. Some common SVG attributes include:

  • width and height: Define the size of the SVG canvas
  • viewBox: Define the visible part of the SVG and its scaling properties
  • fill: Define the filling color or pattern for shapes
  • stroke: Define the outline color and thickness for shapes
  • x and y: Position the element on the canvas

SVG Examples 💡

Example 1: Simple Shapes

Let's create a simple SVG with a square and a circle.

xml
<?xml version="1.0" standalone="no"?> <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <rect x="20" y="20" width="100" height="100" fill="blue"/> <circle cx="100" cy="100" r="50" fill="red"/> </svg>

Example 2: Advanced SVG: Animated Pie Chart

In this example, we'll create an animated pie chart using SVG, CSS, and JavaScript.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> svg { width: 400px; height: 400px; } path { fill: #f5f5dc; } </style> </head> <body> <svg viewBox="0 0 400 400"> <circle cx="200" cy="200" r="180"/> <path id="path1" d="M 200,200 m -130,0 a 130,130 0 1 1 0,-260 a 130,130 0 1 1 0,260"/> <path id="path2" d="M 200,200 m 30,0 a 30,30 0 1 0 0,-60 a 30,30 0 1 0 0,60"/> </svg> <script> const path1 = document.getElementById('path1'); const path2 = document.getElementById('path2'); const duration = 2; const delay = 0; const percent = 50; path1.style.transition = `fill-opacity ${duration}s ease-out ${delay}s`; path2.style.transition = `fill-opacity ${duration}s ease-out ${delay + 0.5}s`; path1.style.fillOpacity = 1 - (percent / 100); path2.style.fillOpacity = percent / 100; </script> </body> </html>

Quiz 💡

Quick Quiz
Question 1 of 1

What is the root element for an SVG document?

Quick Quiz
Question 1 of 1

Which SVG attribute defines the visible part of the SVG and its scaling properties?

That's it for this introductory lesson on SVG! In the next lesson, we'll dive deeper into more advanced SVG concepts, including paths, clipping, and transformations. 🚀 Happy coding! 🚀