HTML svg tag

Introduction to SVG

Scalable Vector Graphics (SVG) is an XML-based markup language designed to describe two-dimensional vector and mixed vector/raster graphics in XML. SVG provides a rich set of features including shapes, paths, text, markers, clones, alpha blending, transforms, gradients, patterns, and grouping. SVG images and their behaviors are defined in XML text files, meaning they can be searched, indexed, scripted, and compressed. Moreover, SVG is an open standard developed by the World Wide Web Consortium (W3C), ensuring compatibility and accessibility across different platforms and devices.

Despite being vector graphics, which inherently scale without losing quality regardless of the display size, SVGs are unique because they can be created and edited with any text editor and with drawing software. They can also be manipulated through CSS and JavaScript, offering a wide range of interactive and animation capabilities, making them highly versatile for web design and interactive applications.

Basics of SVG

SVG tags can be used directly within HTML documents, allowing for the embedding of graphics directly within a webpage without the need for an external file (though external references are also supported). This integration with HTML is seamless, as both SVG and HTML are parts of the larger family of XML languages. This means that SVG elements can be styled with CSS and manipulated with JavaScript just like HTML elements.

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />

In the example above, an SVG element is defined with a specified width and height, containing a single circle element. Attributes of the circle, such as its center coordinates (cx and cy), radius (r), stroke color, stroke width, and fill color, are also specified.

Key SVG Elements

SVG offers a variety of elements to create graphical objects:

  • Shapes: Basic shapes include rect (rectangle), circle, ellipse, line, polyline, and polygon.
  • Path: The path element provides a way to define complex shapes using a series of move-to and line-to commands.
  • Text: The text element allows for the inclusion of text within an SVG document.
  • Grouping: The g element is used to group SVG shapes together.
  • Gradients and Patterns: SVG supports linear and radial gradients and patterns for sophisticated fill techniques.

These elements can be combined and styled to create intricate graphics and visualizations directly within the HTML structure.

Styling and Attributes

SVG provides multiple ways to style elements:

  • Inline Styling: Directly on the element via the style attribute.
  • Internal CSS: Within a <style> element inside the SVG.
  • External CSS: Through an external stylesheet, just like HTML.

However, not all CSS properties are applicable to SVG elements. Properties specifically for SVG, such as fill and stroke, control visual aspects unique to vector graphics. The possibility to manipulate these attributes through CSS and JavaScript opens up dynamic and interactive possibilities for SVG graphics.

Animation and Interactivity

SVG supports animation and interactivity:

  • SMIL: Synchronized Multimedia Integration Language, allows for animation of SVG elements directly within the SVG markup.
  • CSS Animations: CSS can be used to animate SVG properties.
  • JavaScript: JS can create rich interactive and animated graphics by manipulating SVG elements in real-time.

By leveraging these capabilities, designers and developers can create highly interactive and visually dynamic web applications.

Best Practices

When working with SVG, there are several best practices to keep in mind:

  1. Optimization: SVG files can be manually edited to remove unnecessary metadata and reduce file size. Tools like SVGO can automate this process.
  2. Accessibility: Always use descriptive titles and descriptions within SVGs to ensure accessibility for screen readers and other assistive technologies.
  3. Compatibility: Though widely supported, SVG features vary across browsers. Testing and fallback strategies are important for complex applications.


SVG is a powerful tool in web design, providing scalability, flexibility, and interactivity for vector graphics. Understanding the fundamentals of SVG, from basic shapes to advanced animations, allows for the creation of rich, engaging web content. As web technologies continue to evolve, the role of SVG is likely to expand, offering even more possibilities for creative and dynamic web design.