Scalable Vector Graphics is a XML namespace markup language used to describe 2d vector graphics. They are infinity scalable without loosing image quality.
In order to make a svg you need to wrap your element in
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- SVG content goes here -->
</svg>
XML allows you to define your own custom elements and attributes, but in order to apply rules to these elements and attributes you need a XML namespace. The xmlns attribute is used to define this XML namespace and if left off there may be unexpected behaviors.
The xmlns:xlink="http://www.w3.org/1999/xlink"
attribute allow you to use links and use in your svg.
The width and height properties define the width and height of the viewPort. The viewPort allows you to see a portion of an svg. Like looking through a window.
The viewBox is like the viewPort, but it allows for panning and zooming. Like looking through binoculars.
When defining the viewBox attribute it uses this format: viewBox="min-x min-y width height"
stroke-dasharray is an array of values which defines the lengths of alternating dashes and gaps. It follows the pattern of line length then gap length.
stroke-dasharray="10 5"
line length of 10 followed by gap length of 5. This repeats for the whole SVG element.stroke-dasharray="2 6 8"
line 2 -> gap 6 -> line 8 -> gap 2 -> line 6 -> gap 8 -> repeat.stroke-dashoffset rotates the lines and gaps. If it’s positive it rotates it clockwise, if negative counter clockwise.
The order of transforms matters
transform="translate(10, 10) rotate(45)
transform="rotate(45) translate(10, 10)
<line x1="" y1="" x2="" y2=""/>
<rect x="" y="" width="" height="" />
Used to create a closed shape by connecting the first and last points.
<polygon points="x1,y1 x2,y2 x3,y3" />
<polygon points="10,10 10,50 50,50"/>
draws a triangleUsed to create an open shape. There is no connecting between the last and first points.
<polyline points="x1,y1 x2,y2 x3,y3 />
<circle cx="" cy="" r="" />
<ellipse cx="" cy="" rx="" ry="" />
Used to draw lines, arcs, quadratics, and cubic bezier curves.
Mx,y
moves the penLx,y
draws a line to x and y
Hdistance
draws a line on the x axis that distanceVdistance
draws a line on the y axis that distanceArx,ry xRotation largeArcFlag,sweepFlag x,y
draws an elliptical arc
Qcx,cy x,y
draws the quadratic equations
Ccx1,cy1 cx2,cy2 x,y
draws the cubic bezier curve which has 2 more control points than the quadratic equation.
Z
draws a line from the starting point to the end point of the path. If the start and end point are the same it closes the path.M100,100 L150,150
draws line from 100,100 to 150,150M100,100 l150,150
draws line from 100,100 to 250,250<path d="M100,100 L150,150 L150,200
A50,50 0 1,0 100,150
Q20,250 200,300
C200,400 375,350 400,300
Z
"/>
<text x="" y="">
Text
<tspan fill="red">red</tspan>
</text>
The origin on the text is the bottom left.
You can have other attributes:
font-size=""
font-family=""
Tspan is used to style text independently from the other text.
Sets a path where text can be rendered. This allows text to be curved.
<text>
<textPath href="#path">Curved Text</textPath>
</text>
The group elements allows you to group different svg elements together in order to style them together.
<g>
<!-- Grouped SVG elements -->
</g>
Allows you to have a hyperlink inside your svg. You need the xmlns:xlink attribute set in your svg in order for this to work.
<a xlink:href="https://example.com">
<text x="50" y="50" fill="white" text-anchor="middle" alignment-baseline="middle">Click me</text>
</a>
References another SVG element defined with the id attribute. You need the xmlns:xlink attribute set in your svg in order for this to work.
<use xlink:href="#existingElement" />
Defines reusable SVG elements. In order to give the definition a nation you have to use the id attribute.
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
Displays raster images
<image x="" y="" width="" height="" href="" />
Creates a path which can hide or show portions of other elements.
<clipPath id="clip">
<rect width="50" height="50" />
</clipPath>
Defines an image mask which can hide or show portions of other elements.
<mask id="mask">
<!-- Define mask elements here -->
</mask>
What is the difference between clipPath and mask?
<linearGradient x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<radialGradient cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
What is fx and fy?
A pattern element which can be redrawn at repeated x and y coordinates intervals(tiled).
<defs>
<pattern id="star" viewBox="0,0,10,10" width="10%" height="10%">
<polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2" />
</pattern>
</defs>
<circle cx="50" cy="50" r="50" fill="url(#star)" />
Apply a filter, defined by grouping filter primitives, to svg elements.
<filter id="blurMe">
<feGaussianBlur stdDeviation="5" />
</filter>
<circle cx="170" cy="60" r="50" fill="green" filter="url(#blurMe)" />
Filter primitive | Description |
---|---|
feBlend | |
feColorMatrix | |
feComponentTransfer | |
feComposite | |
feConvolveMatrix | |
feDiffuseLighting | |
feDisplacementMap | |
feDropShadow | |
feFlood | |
feGaussianBlur | |
feImage | |
feMerge | |
feMorphology | |
feOffset | |
feSpecularLighting | |
feTile | |
feTurbulence |
<metadata>
<!-- Include metadata information here -->
</metadata>
<desc>This is an example SVG document.</desc>
<title>SVG Document Title</title>
Questions