SVG Basic Syntax
An SVG document consists of one or more SVG elements, which define the content and attributes of the graphics.
-
widthandheightThe attribute defines the width and height of the SVG canvas. -
xmlnsThe attribute specifies the XML namespace of the SVG document.
Basic Syntax of SVG:
-
An SVG document begins with the
tag and ends with thetag. -
SVG elements use tags to describe different graphics, for example
represents a circle,represents a rectangle, etc. -
SVG elements can contain attributes used to specify various characteristics of the graphics, such as position, size, color, etc.
Drawing Basic Shapes:
SVG provides a series of graphic elements to draw shapes of various forms, such as rectangles, circles, straight lines, polygons, etc.
: Draw a rectangle: Draw a circle: Draw an ellipse: Draw a straight line: Draw a polyline: Draw a polygon: Draw a path
**Rectangles:** Use the
**Circles:** Use the
**Ellipses:** Use the
**Lines:** Use the
**Polygons:** Use the
**Polylines:** Use the
**Paths:** Use the
Gradients and Fills:
-
Use
orDefine a gradient. -
Use
fillandstrokeThe attribute specifies the fill and stroke styles.
Text and Fonts:
-
Use
The element inserts text. -
Use
font-family,font-sizeand other attributes control the text style.
Animation and Interaction:
-
Use CSS or JavaScript to create animation effects.
-
Add event handlers to implement interactive features such as mouse clicks, hovers, etc.
SVG Element Attributes:
SVG elements can have various attributes used to specify characteristics of the graphics such as position, size, color, etc.
cy="100"
r="50"
fill="red"
stroke="black"
stroke-width="2"
/>
-
cxandcyThe attribute defines the x and y coordinates of the circle's center. -
rThe attribute defines the radius of the circle. -
fillThe attribute defines the fill color. -
strokeThe attribute defines the stroke color. -
stroke-widthThe attribute defines the stroke width.
Nesting and Grouping:
SVG elements can be nested and grouped to better organize and manage graphic elements.
-
The element is used to create a group. -
idThe attribute is used to assign a unique identifier to the group.
Simple SVG Example
SVG files are recommended to use .svg (all lowercase) as the extension for such files.
1. The following is a simple SVG file example that creates an SVG image containing a circle:
test.svg file
This code describes an SVG canvas with a width of 200 units and a height of 200 units. A circle is drawn on the canvas with center coordinates at (100, 100), a radius of 80 units, and a blue fill color.
Preview:

2. Another simple SVG graphic example:
test.svg file
: This is the end tag of the SVG document, marking the end of the SVG document.