Welcome to the first tutorial in a series of tutorials all about SVG’s! In this first tutorial we will cover these basics:
All you need is a notepad (for example the windows default one or Notepad++) and a browser (such as Chrome/Firefox). You are simply required to add text to a file with a “.svg” extension and the drag and drop this file into the browser, like so:
This course is targeted primarily at web/UI developers who want to create simple and interactive SVG elements for their site. In this tutorial we will be learning about basic shapes, colours and positions. In the next tutorial series we will learn how to animate these shapes to create simple effects. In future tutorials we will learn how to integrate these SVG’s into our website. An example can be seen in the bottom right corner of this page. Watch how the SVG responds to page scrolls.
These lessons are all available on my Instagram too. If you have any questions try contacting me on there or email me
Learn how to create a simple SVG circle:
<svg width="200" height="200" version="1.1" xmlns="http://www.w3.org/2000/svg"></svg>
<circle cx="100" cy="100" r="80"/>
Save the file. Drag and drop the svg file into a browser:
See a black circle. Marvel at your masterpiece.
SVG Element - Version
We need to add the SVG version so the browser knows how to read the SVG element. Version 1.1 (Second Edition) was released in 2011 and is the latest version. The next version will be 2.0.
SVG Element - XMLNS
The XML NameSpace declares the XML dialect to use. In this case it’s SVG:
http://www.w3.org/2000/svg
Circle Element
Surprisingly a circle element is used to draw a circle. The cx and cy attributes define the x and y position (which defaults to 0, 0). The r attribute defines the circles radius.
<svg width="200" height="200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="80"/>
</svg>
Learn how to add colour to a simple SVG circle:
<circle cx="100" cy="100" r="80" fill="blue" stroke="red" stroke-width="10"/>
Colours
Colours apply to both fill and stroke. They can be described in several ways:
<svg width="200" height="200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="80" fill="blue" stroke="red" stroke-width="10"/>
</svg>
Learn about the other available shapes:
<rect x="50" y="50" width="300" height="300"/>
<circle cx="200" cy="200" r="150"/>
<ellipse cx="200" cy="200" rx="150" ry="75"/>
<polygon points="260,131 140,131 200,275"/>
Predefined Elements
SVG’s have many predefined shape elements, each with their own {attributes}:
<circle {cx}{cy} {r}>
cx: x position of the center of the circle
cy: y position of the center of the circle
r: radius
<rect {x} {y} {width} {height}>
x: x position from the upper left corner.
y: y position from the upper left corner.
<ellipse {cx} {cy} {rx} {ry}>
cx: x position of the center of the ellipse
cy: y position of the center of the ellipse
rx: horizontal radius
ry: vertical radius
<polygon {points}>
points: x,y position from the upper left corner.
<svg width="400" height="400" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect x="50" y="50" width="300" height="300" fill="#264653"/>
<circle cx="200" cy="200" r="150" fill="#2a9d8f"/>
<ellipse cx="200" cy="200" rx="150" ry="75" fill="#e9c46a"/>
<polygon points="260,131 140,131 200,275" fill="#e76f51"/>
</svg>
Learn how to position elements within a SVG:
<rect x="50" y="50" width="100" height="100"/>
<rect x="50" y="160" width="100" height="100"/>
<rect x="160" y="50" width="100" height="100"/>
<rect x="160" y="160" width="100" height="100"/>
Positions
Positions are measured in pixels from the top left corner of the SVG. This is known as the point of origin (x=0, y=0).
A positive x value moves the position to the right. A positive y value moves the position down.
It’s not always this simple but for now this understanding is good enough.
<svg width="310" height="310" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect x="50" y="50" width="100" height="100" fill="#F44E24"/>
<rect x="50" y="160" width="100" height="100" fill="#01A4EF"/>
<rect x="160" y="50" width="100" height="100".fill="#81B900"/>
<rect x="160" y="160" width="100" height="100" fill="#FFB902"/>
</svg>
Learn how to transform elements using groups:
<g transform="translate(50,50)">
<rect .../>
</g>
Groups
Group elements are used to assign attrributes to multiple elements at once:
Translate
Move an element based on x/y coordinates:
transform="translate(50,50)"
Rotate
Rotates an element in degrees:
transform="rotate(45)"
Skew
2D Skew an element in degrees in either the x or y direction:
transform="skewX(15)"
transform="skewY(15)"
Scale
Scale an element in the x and/or y direction:
transform="scale(0.5)"
transform="scale(0.5, 1.5)"
<svg width="310" height="310" version="1.1" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(50,50)">
<rect x="50" y="50" width="100" height="100" fill="#F44E24"/>
<rect x="50" y="160" width="100" height="100" fill="#01A4EF"/>
<rect x="160" y="50" width="100" height="100" fill="#81B900"/>
<rect x="160" y="160" width="100" height="100" fill="#FFB902"/>
</g>
</svg>
Learn how to apply multiple transforms to elements:
<g transform="translate(-15.5, 15.5) scale(1.1) skewY(-10)">
<rect .../>
</g>
Multiple Transform
It’s possible to apply multiple transformations at once to create more complex transforms.
For example to scale an object and keep its original center coordinates you will need to translate it in the x/y direction by the scale factor.
<svg width="310" height="310">
<g transform="translate(-15.5, 15.5) scale(1.1) skewY(-10)">
<rect x="50" y="50" width="100" height="100" fill="#F44E24"/>
<rect x="50" y="160" width="100" height="100" fill="#01A4EF"/>
<rect x="160" y="50" width="100" height="100" fill="#81B900"/>
<rect x="160" y="160" width="100" height="100" fill="#FFB902"/>
</g>
</svg>
Learn how to apply transforms within style tags:
<style>
svg {
transform: perspective(600px) rotateY(-40deg) scale(1.4,1);
-webkit-transform: perspective(600px) rotateY(-40deg) scale(1.4,1);
}
</style>
Style Tag Transform
Style tags allow us to embed a style sheet within our SVG. In this case we have applied a transform to the whole SVG. We have applied the transform this way as it’s not possible to apply a 3D transform natively.
Note: The perspective transform won’t work in IE.
<svg width="310" height="310" version="1.1" xmlns="http://www.w3.org/2000/svg">
<style>
svg {
transform: perspective(600px) rotateY(-40deg) scale(1.4, 1);
}
</style>
<g>
<rect .../>
</g>
</svg>
Good work! you have reached the end of the SVG Basics tutorial. You now know basic things about SVG’s. Feel free to take a moment to bask in this new found knowledge.
Moment over. Now move on to learning about SVG animations!
I hope this post was helpful. Please get in touch with any feedback you have: