SVG

Graphical
Elements

Learn which elements can be drawn
by SVG special tags

Tutorials#


Elements

The two main drawing elements in SVG are path and text. There is a set of basic shape drawing elements like 'rect' that are essentially shorthand forms for the path element.

In this tutorial the following basic graphical elements are detailed (there are more like tref, marker, ...):

Notes:


Example:

<svg width="100" height="50" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <rect x="10" y="10" width="50" height="30"/>
</svg>
<svg width="100" height="50" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <rect x="10" y="10" rx="10" ry="10" width="50" height="30"/>
</svg>

Rectangle

Rectangle main attributes are:




Example:

<svg width="100" height="100" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <circle cx="50" cy="50" r="20"/>
</svg>

Circle

Circle main attributes are:







Example:

<svg width="100" height="100" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <ellipse cx="50" cy="50" rx="30" ry="20" stroke="black" fill="none" />
</svg>

Ellipse

Ellipse is similar to circle with two radius rx and ry.









Example:

<svg width="100" height="100" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <line x1="10" y1="90" x2="50" y2="20"
            stroke="black" stroke-width="7" stroke-linecap="round" />
</svg>

Line

Line has x1, y1 nad x2, y2 coordinates, which specify the start and end point of the line.









Example:

<svg width="100" height="100" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <polyline
            points="10 90, 50 20, 30 40, 60 80" stroke="black" fill="none" />
</svg>

Polyline

Is a group of connected lines. It contains a list of points, each number separated by a white space. Each point must contain two numbers, an x coordinate and a y coordinate. So the list (0,0), (1,1) and (2,2) could be written: "0 0, 1 1, 2 2".







Example:

<svg width="100" height="100" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <polygon
    points="10 90, 50 20, 30 40, 60 80" stroke="black" fill="none" />
</svg>

Polygon

Like polyline, where the last point is automatically connected to the first one.









#

Example:

<svg width="100" height="100" style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <path
        d="M50 0 L75 20 L25 100 Z" stroke="black" fill="green" />
</svg>
<svg width="100" height="100"style="border: 1px solid rgba(0, 0, 0, 0.2)">
    <path
        d="M100 200  Q250 100 400 200 T600 200"
        fill="none" stroke="#000" stroke-width="20" />
</svg>

Path

Path is a general shape. SVG paths come with a set of commands that enable you to draw pretty much any shape you want. However, simple shape elements (<line>, <circle>, < rect>, <ellipse>, <polygon> and <polyline>) are there for many reasons, and they are more readable, maintainable, animatable, editable by hand than their <path> alternatives.

The following commands are available for path data:

There are always two versions of the same drawing command :

There are three groups of commands that draw curved paths: Cubic Bézier (C, c, S, s), Quadratic Bézier (Q, q, T, t), and Elliptical arc (A, a). For more see A Closer Look at SVG Path Data.

See also Path Specification and The SVG 'path' Syntax: An Illustrated Guide for more details.


Text

#

The second most important drawing element after the path is text. It has a large number of styling properties.

The three main types of text that can be generated are:

The position of the text is determined by the x and y attributes of the text element. The x-attribute determines where to locate the left edge of the text (the start of the text). The y-attribute determines where to locate the bottom of the text (not the top). Thus, there is a difference between the y-position of a text and the y-position of lines, rectangles, or other shapes.

Example:

<svg width="570" height="100">
    <text x="0" y="15"
    stroke="#d30b0b" fill="none">
        I AM SVG TEXT
    </text>
</svg> 
I AM SVG TEXT

Example: Multiline Text uses tspan attribute.

<svg width="570" height="100">
    <text x="0" y="12" >First Line</text>
    <text x="0" y="30" fill="rgb(121,0,121)">Second Line
       <tspan x="0" y="50" font-weight="bold">Third Line</tspan>
       <tspan x="0" y="90" font-size="3em">Last Line</tspan>
    </text>
</svg>
First Line Second Line Third Line Last Line

Example: Text on a path.

<svg>
    <defs>
        <path id="textPath" d="M15 50 C10 0 90 0 90 50" />
    </defs>
    <text fill="#d30b0b" font-size="20" font-family="fantasy">
        <textPath xlink:href="#textPath">Text on a Path</textPath>
    </text>
</svg>
Text on a Path
<svg>
    <defs>
        <path id="s3" d="M 50,50 A 20,20 0 0 1 90,50 A 40,40 0 0 1 10,50 A 60,60 0 0 1 130,50" />
    </defs>
    <text font-size="20" font-family="fantasy" fill="#d30b0b">
        <textPath xlink:href="#s3">
            Text on a spiral path looks very very crazy!!!
        </textPath>
    </text>
</svg>
Text on a spiral path looks very very crazy!!!

Image Tag

The SVG <image> element is used to embed bitmap images inside your SVG image. This way you can draw on top of, or next to, the bitmap image.

Example:

<svg>
    <g style="mix-blend-mode: multiply;">
        <image x="0" y="0" width="120" height="80"  xlink:href="images/dog-icon.png" />
        <text x="0" y="95" stroke="#d30b0b" stroke-width="3" font-size="46" fill="none">My Dog</text>
    </g>
</svg>
My Dog

Linking

Linking in SVG is much the same as in HTML. SVG has an a element that indicates the hyperlink and defines where the link goes.

Example:

<svg>
    <a xlink:href="https://www.w3.org/TR/SVG/linking.html#AElement" target="_blank">
        <rect width="200" height="40" fill="#fccece" stroke="#ed7575" />
        <text x="70" y="25" stroke="#d30b0b" >Press Me</text>
    </a>
</svg>
Press Me