Scalable Vector Graphics (SVG) is a powerful, XML-based markup language used for creating two-dimensional vector graphics. It’s a widely adopted technology in web development because it allows graphics to scale up or down without losing quality, making it ideal for responsive design. SVG files are text-based, making them lightweight and easy to manipulate through CSS or JavaScript.
In this chapter, we’ll explore SVG from its basics to advanced usage, providing examples with thorough explanations and output demonstrations.
SVG stands for Scalable Vector Graphics. Unlike raster images (JPEG, PNG), which are made of pixels, SVG images are made of vectors—geometric shapes like points, lines, and curves. Because they are vector-based, they can scale indefinitely without losing clarity.
Before diving into the technical details, let’s look at why you should use SVG over other image formats like PNG or JPEG.
There are several ways to embed SVG in your HTML document:
You can directly write SVG code in your HTML file.
Inline SVG Example
svg tag defines the SVG container.circle element creates a circle with center (cx, cy), radius r, a black stroke, and a red fill.Output: A red circle with a black border.
You can link to an SVG file using the standard <img> tag.
object tagThe <object> tag allows you to embed SVG files with additional fallback content.
embed tagThe <embed> tag is similar to <object>, and it directly embeds the SVG file.
SVG supports various basic shapes such as rectangles, circles, ellipses, lines, and polygons.
rect element draws a rectangle starting at position (10, 10) with a width and height of 100.Output: A yellow square with a blue border.
ellipse element creates an ellipse with center at (100, 100), and radii of 50 for the x-axis and 80 for the y-axis.Output: A pink ellipse with a green border.
polygon element creates a shape by connecting points. In this case, the points form a triangle.Output: A green triangle with a purple border.
The <path> element is one of the most versatile elements in SVG. It allows for the creation of complex shapes, lines, and curves.
M moves the starting point of the path.C creates a cubic Bézier curve.S creates a smooth cubic Bézier curve.Output: A curve that flows through the specified control points.
You can use the <text> element to display text inside an SVG graphic.
text element places the text at position (50, 50), with a font-family of Verdana and a font-size of 20 pixels.Output: The text “Hello, SVG!” appears in blue.
<defs> element defines reusable elements such as gradients.linearGradient element creates a gradient from yellow to red.Output: A rectangle filled with a gradient that transitions from yellow to red.
SVG provides powerful transformations such as rotate, scale, and translate, along with animation support.
Explanation: The transform attribute scales the rectangle by 1.5 times its original size.
animate element moves the circle horizontally from cx=100 to cx=150 over 2 seconds.SVG elements can be styled with CSS and manipulated with JavaScript just like any other HTML elements.
While SVG files are usually small, optimizing them further can improve web performance. Here are some optimization tips:
SVG is a versatile and powerful tool in modern web development. It offers scalability, interactivity, and the ability to create complex graphics while maintaining small file sizes. By understanding how to use SVG, you can create responsive, high-quality graphics that are easily manipulated with CSS and JavaScript, making it an essential skill for any web developer. With the examples provided, you should be well-equipped to begin incorporating SVG into your web projects confidently. Happy coding !❤️
