Home

SVGs

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.

Width, Height, and viewBox attributes

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"

Attributes for all SVG elements

stroke-dasharray and stroke-dashoffset

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-dashoffset rotates the lines and gaps. If it’s positive it rotates it clockwise, if negative counter clockwise.

transform

The order of transforms matters

Line

<line x1="" y1="" x2="" y2=""/>

Rectangle

<rect x="" y="" width="" height="" />

Polygon

Used to create a closed shape by connecting the first and last points.

<polygon points="x1,y1 x2,y2 x3,y3" />

Polyline

Used to create an open shape. There is no connecting between the last and first points.

<polyline points="x1,y1 x2,y2 x3,y3 />

Circle

<circle cx="" cy="" r="" />

Ellipse

<ellipse cx="" cy="" rx="" ry="" />

Path

Used to draw lines, arcs, quadratics, and cubic bezier curves.

<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

<text x="" y="">
	Text
	<tspan fill="red">red</tspan>
</text>

The origin on the text is the bottom left.

You can have other attributes:

Tspan is used to style text independently from the other text.

Text path

Sets a path where text can be rendered. This allows text to be curved.

<text>
  <textPath href="#path">Curved Text</textPath>
</text>

Group

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>

Use

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" />

defs

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)" />

Image

Displays raster images

<image x="" y="" width="" height="" href="" />

ClipPath

Creates a path which can hide or show portions of other elements.

<clipPath id="clip">
  <rect width="50" height="50" />
</clipPath>

Mask

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?

Linear Gradient

<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>

Radial Gradient

<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?

Pattern

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)" />

Filter

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  

Information Elements

Metadata

<metadata>
  <!-- Include metadata information here -->
</metadata>

Description

<desc>This is an example SVG document.</desc>

Title

<title>SVG Document Title</title>

Questions