Sunday, December 3, 2023
HomeVideo EditingLearn how to Hand Code SVG

Learn how to Hand Code SVG


One of many nice issues about scalable vector graphics (other than their being infinitely scalable with out high quality loss) is that after you recognize the basics you’ll be able to hand-code easy shapes fairly simply, without having to open a graphics utility. 

With just some strains of code you’ll be able to have your individual customized icons, and also you’ll know precisely how every one is put collectively. While you create your individual SVGs you guarantee they’re written in probably the most environment friendly means attainable, and you’ve got the utmost stage of management when utilizing them in your websites.

On this tutorial we’re going to cowl all the basics of coding SVGs by hand, however I’m not going to bore you with a dry lecture that simply trots out the related shapes and attributes. As a substitute you’ll discover ways to hand code SVG by way of follow, creating the six icons you see firstly of this tutorial (try the demo on-line). Within the course of, you’ll use the entire fundamental parts required for SVG hand coding.

Talking of these fundamental parts, let’s have a fast introductory rundown on what every of them are.

Fundamental SVG Components

You may get into a number of complexity with SVG, however that’s not essential for the icons we’ll be making. The next checklist covers the constructing blocks we’ll want.

  • <svg> Wraps and defines the whole graphic. <svg> is to a scalable vector graphic what the <html> factor is to an online web page.
  • <line> Makes single straight strains.
  • <polyline> Makes multi-segment strains.
  • <rect> Makes rectangles and squares.
  • <ellipse> Makes circles and ovals.
  • <polygon> Makes straight sided shapes, with three sides or extra.
  • <path> Makes any form you want by defining factors and the strains between them.
  • <defs> Defines reusable belongings. Nothing positioned inside this <defs> part is seen initially. <defs> is to a scalable vector graphic what the <head> factor is to an online web page.
  • <g> Wraps a number of shapes into a gaggle. Place teams within the <defs> part to allow them to be reused.
  • <image> Like a gaggle, however with some additional options. Sometimes positioned within the <defs> part.
  • <use> Takes belongings outlined within the <defs> part and makes them seen within the SVG.

As we undergo and create our icons, we’ll be working by way of this checklist of parts within the order seen above.

Starter Recordsdata

Earlier than we get began, seize your self a duplicate of the starter information from the GitHub repo. You possibly can both obtain a .zip file, or clone the repo to your individual system.

We’re going to start with a doc that has some fundamental HTML and CSS already in place. This can give some styling to the SVGs we’ll be making, and also will set you up with a bit grid on the web page. We’ll be creating our icons excessive of this grid, and hopefully it should aid you visualize the coordinates you’re working with when laying down your SVGs. 

While you open up “handcodedsvg.html” from the supply “Starter Recordsdata” folder you need to see the next:

starterhtmlstarterhtmlstarterhtml

Fast Primer on x and y Values

When working in 2D area on a web site, the horizontal axis is represented by x and the vertical axis is represented by y. Positions alongside every of those axes are represented by numbers. If we need to transfer one thing to the fitting, we’ll want to make use of growing x values, and to maneuver to the left we’ll use lowering x values. Likewise, to maneuver one thing down we’ll use growing y values, and to maneuver one thing up we’ll use lowering y values.

A typical shorthand for expressing the x and y values of a single level is (x, y). For instance, a degree at 10 on the x axis and 50 on the y axis may be written as (10, 50). I’ll use this shorthand on occasion on this tutorial.

Discover the 2 darkest strains on our grid? We’re going to put our SVG so its prime left nook aligns with the place they intersect. As such, that intersection level will symbolize the place x = 0 and y = 0 , or (0,0), in our SVG.

The Background Grid

Every of the lightest grid strains represents 10px, and the medium thickness strains symbolize 100px. So if we wished to maneuver an object down from one medium thickness line to the subsequent, we’d improve its location on the y axis by one 100px.

If that also sounds a bit unclear, don’t fear this can all make sense as we get into the practicalities of making our SVG icons.

Default SVG Styling

Be aware that within the starter HTML file there’s some included CSS with default styling for our soon-to-be-created SVG icons:

1
svg {
2
  stroke: #000;
3
  stroke-width: 5;
4
  stroke-linecap: spherical;
5
  stroke-linejoin: spherical;
6
  fill: none;
7
}

This can set our icons to haven’t any fills, and black 5px large strokes with rounded caps and joins.

1. Setup the SVG

Step one in creating any SVG is to put down an <svg></svg> factor. Something you need your SVG to show must be between these tags. There are just a few attributes you should use on this factor, however we’ll preserve issues easy and simply use width and peak.

Add the next code within the <physique> part of your HTML doc:

1
<svg width="750" peak="500">
2

3
</svg>

Be aware: the CSS in our starter file goes to offset this SVG down and to the fitting by 100px so its prime left nook might be positioned on the intersection level of the 2 darkest strains on our background grid. And the values within the CodePen demos all through this tutorial might differ barely too–however be at liberty to mess around with them.

2. Create “Left Align” Icon

Let’s begin by utilizing the <line> factor to create this left align icon:

leftaligniconleftaligniconleftalignicon

The road factor has 4 attributes you’ll want to make use of:

  • x1 horizontal start line of the road
  • y1 vertical start line of the road
  • x2 horizontal ending level of the road
  • y2 vertical ending level of the road

To summarize the above, you employ the x1 and y1 attributes to set the place the road begins, and the x2 and y2 attributes to set the place the road ends.

Let’s create the primary line of our icon, the one on the prime. We’re going to make the road 45px lengthy, nevertheless the 5px stroke we’re utilizing goes so as to add some additional pixels across the exterior of our line. As such we’ll must offset our line down and to the fitting by 3px to make sure not one of the additional pixels created by the stroke are clipped off.

For that cause, we’re going to begin our line at a place of 3 on the x axis and 3 on the y axis. We will set this line start line of (3,3) by utilizing the attributes x1="3" y1="3".

We would like the road to be 45px lengthy, so we’re going so as to add 45 to our beginning x place of 3, giving us 48 as the worth we need to set for x2. We would like the road to complete on the similar place on the horizontal axis, so we’ll set y2 to equal 3, i.e. the identical worth we gave to y1. We’ll add this (48,3) line ending level through the attributes x2="48" y2="3".

The entire code for the primary line ought to seem like this:

1
<line x1="3" y1="3" x2="48" y2="3"></line>

Examine your browser preview and you need to see a 45px lengthy black line with good rounded caps.

Now we will go forward and add the subsequent three strains to our icon. We need to find yourself with 4 strains in complete. The primary and third ought to be 45px lengthy, and the second and fourth ought to be 62px lengthy. We additionally desire a vertical hole between every of 16px.

We will obtain this with the next code:

1
<line x1="3" y1="3" x2="48" y2="3"></line>
2
<line x1="3" y1="19" x2="65" y2="19"></line>
3
<line x1="3" y1="35" x2="48" y2="35"></line>
4
<line x1="3" y1="51" x2="65" y2="51"></line>

Be aware: the y values of every line incrementally improve by 16px with a view to create the required vertical hole.

Take one other take a look at your browser preview and you need to see all 4 strains. You may as well edit the SVG straight on this pen:

Remark Out Your Icons As We Go

With that code in place,
your first icon is already made. We’re prepared to maneuver onto creating
the subsequent icon, and we’re going to need to make it in the identical place
on the grid, however proper now the left align icon is in the way in which. As such, for now simply remark out its code to
clear the area. We’re going to come back again and uncomment it
later after we flip our icons into reusable belongings.

You’ll must do the
similar factor for every icon as we go, commenting it out after you end
creating it. For that cause it’s most likely additionally a good suggestion so as to add a bit notice
above the code for every icon so you recognize which is which once you come
again to them later.

3. Create a “Proper Caret” Icon

For this icon, let’s use the subsequent evolution of the <line> factor: the <polyline>. We’ll be utilizing it to create a proper pointing caret.

rightcaretrightcaretrightcaret

The <polyline> factor solely has one attribute: factors. In right here you employ pairs of numbers to set a sequence of factors. Traces will routinely be drawn between them. The quantity pairs are merely written one quantity after the opposite contained in the factors attribute. Comma separation just isn’t required, although it could possibly optionally be included. For readability you may additionally prefer to put every pair of values by itself line in your code.

We’re going to begin our proper caret’s polyline on the similar spot we began our final icon, that being (3,3) to make sure our stroke and caps don’t get clipped. We would like our second level to maneuver over to the fitting, and down by 25px, so we’ll set it to (30,28). Our third level ought to be vertically aligned with the primary level, and transfer down by one other 25px, so will probably be set to (3,53).

We will add these factors into our polyline’s factors attribute like so:

1
<polyline factors="
2
  3 3
3
  30 28
4
  3 53
5
"></polyline>

If you need extra concise code, you may additionally write the above as:

1
<polyline factors="3 3, 30 28, 3 53"></polyline>

or

1
<polyline factors="3 3 30 28 3 53"></polyline>

Check out your browser preview and you need to see your proper caret icon displaying: one other icon achieved, identical to that! 

As soon as once more, remark out this icon and provides it a bit notice so you recognize which one it’s earlier than shifting onto the subsequent icon.

4. Create a “Browser” Icon

Now we now have strains down pat, let’s create some shapes, beginning with a rectangle (<rect>). We’re going to make use of it together with a few <line>s to create a browser icon.

browsericonbrowsericonbrowsericon

Rectangles and squares could be created with the <rect> factor, which has 4 attributes you’ll want to offer:

  • x the highest left nook place on the x axis
  • y the highest left nook place on the y axis
  • width width of the form
  • peak peak of the form

Be aware: you can too use the attributes rx and ry to create rounded corners if you happen to’d like.

We’re going to create a rectangle with its prime left nook offset by 3px in each instructions, once more to keep away from clipping the stroke, so we’ll use the attributes x="3" y="3". We would like it to be 80px large by 60px excessive, so we’ll additionally use the attributes width="80" peak="60".

As such our full rectangle code ought to be:

1
<rect x="3" y="3" width="80" peak="60"></rect>

Save your code and try your browser preview. It’s best to see a neat little rectangle sitting there.

Now all we have to do is add a horizontal line throughout the highest, and a vertical line close to the highest left, as you see depicted within the picture firstly of this part. We’ll use the identical line creation course of as we did earlier than, and our full browser icon code ought to seem like this:

1
<rect x="3" y="3" width="80" peak="60"></rect>
2
<line x1="3" y1="19" x2="83" y2="19"></line>
3
<line x1="20" y1="3" x2="20" y2="17"></line>

Take a second to take a look at the coordinates supplied within the two line attributes, and perhaps change their values round a bit bit so you’ll be able to see how they’re working on this icon.

5. Create an “Alert” Icon

Now that we now have rectangles creation beneath management, let’s check out utilizing <ellipse>s. We’re going to make use of two of them, together with a <line>, to create this alert icon:

alertalertalert

Like rectangles, the <ellipse> factor additionally requires 4 attributes, nevertheless they’re a bit totally different to these of rectangles. As a substitute of utilizing width and peak we set a horizontal and vertical radius. And as an alternative of positioning the highest left nook of the form, we place its heart:

  • cx the middle place on the x axis. Suppose “cx for heart x”.
  • cy the middle place on the y axis. Suppose “cy for heart y”.
  • rx the dimensions of the radius on the x axis, i.e. the form’s peak divided in half. Suppose “rx for radius x”.
  • ry the dimensions of the radius on the y axis, i.e. the form’s width divided in half. Suppose “ry for radius y”.

We would like a superbly spherical circle that’s 80px large by 80px excessive, which implies we’d like its radius to be 40px on each axes. We’ll set this with the attributes rx="40" ry="40".

We additionally need the circle to take a seat flush with the darkest strains on our graph. Provided that our circle might be 80px large and excessive, that will place its heart level at 40px. We additionally want to permit for our 3px offset to keep away from clipping nevertheless, so which means our circle’s heart level ought to be a 43px on each axis. We’ll set this with the attributes cx="43" cy="43".

Placing all that collectively, we get this code:

1
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>

Examine your browser preview and you need to now see a circle in your web page.

We’re going so as to add a second circle now, to create the dot on the backside of the exclamation mark. We’ll create this in simply the identical means, the one distinction being we’re going to make use of an inline type to set the fill to black:

1
<ellipse type="fill:black;" cx="43" cy="65" rx="5" ry="5"></ellipse>

Lastly, we simply want so as to add a line to create the opposite a part of the exclamation mark. As soon as once more we’re utilizing the identical strategies as with the opposite strains we’ve used to this point, with the one distinction being we’ll use an inline type to thicken this stroke width up from 5px to 8px.

The finished code for our alert icon is as follows:

6. Create a “Play” Icon

Now we now have the grasp of the comparatively fastened shapes of rectangles and ellipses, we’re able to roll our personal shapes utilizing the <polygon> factor. We will create any multi-sided form we wish with this, from octagons to stars. Nonetheless we’ll preserve issues straight ahead for now and create a triangle. We’ll mix it with an <ellipse> to create a play icon:

playiconplayiconplayicon

The <polygon> factor is sort of similar to the <polyline> factor. It too has only one attribute, factors, through which you employ pairs of values to set every level that makes up the form. The distinction is that whereas a polyline will stay open, a polygon will routinely shut itself.

Let’s begin by getting the circle down that our polygon will sit within. We’ll use the very same ellipse we did in our alert icon:

1
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>

Now let’s create our polygon. We’re going to put three factors, and contours will routinely be generated between these factors to create a triangle. The factors might be (35,23), (60,43) and (35,63). As such our polygon’s code might be:

1
<polygon factors="35 23, 60 43, 35 63" />

And our full play icon’s code is:

7. Create a “Obtain” Icon

Now we’ll transfer onto probably the most doubtlessly complicated, however concurrently most versatile means of manufacturing SVG shapes, and that’s the <path> factor. Making a path is a bit like making a polygon, the place you lay out your form a bit at a time. Nonetheless with a path you straight create every level and line your self with out automation, and also you even have the choice to create curves between factors as an alternative of straight strains.

A path can be utilized to create absolutely anything, however past a sure stage of complexity you might be nonetheless higher off utilizing a vector design utility relatively than hand coding. For that cause, we’re going to deal with a small subset of path performance, and use it to create this obtain icon:

downloadicondownloadicondownloadicon

Technically, you may create the above form with a polygon, however this arrow will give us a great way to get throughout how the trail factor works.

We’ll be utilizing solely one of many attributes of <path>, and that’s d. The d stands for “knowledge”, and it’s in right here you’ll outline all of the factors and contours of your path. Inside this attribute, instructions to set the factors of a path and create strains between them are supplied through single letters resembling M or L, adopted by a set of x and / or y coordinates.

There are a number of of those instructions, however to provide you an intro to working with <path> we’ll stick to a couple that may be realistically used when hand coding. They’re as follows:

  • M Represents moveto. It begins a brand new path at a given place, outlined with x and y values. Think about that is like hovering your mouse over a degree in your canvas, prepared to attract. The capital M signifies shifting to an absolute set of coordinates. (Decrease case m would point out relative coordinates).
  • L Represents lineto. Draw a line from the present place to a brand new place. The capital L signifies shifting to an absolute set of coordinates. (Decrease case l would point out relative coordinates).
  • Z Represents closepath. It converts the trail right into a closed form by drawing a straight line
    between the present level to the primary level created within the path.

It’s best to undoubtedly view these three instructions, (and the icon we’ll create with them), as an introductory primer to the <path> factor. To actually get probably the most out of it you’ll need to familiarize your self with all of the instructions at your disposal.

Code Your Obtain Icon

To code your obtain icon path I like to recommend first including within the empty path factor:

1
<path d="
2

3
"></path>

From right here, add in every command one after the other, saving and viewing the progress of the form so that you see how it’s created. I additionally suggest placing every command by itself line for readability.

  1. First, we need to transfer to (18,3), the purpose at which we wish our arrow to start. To do that we’ll add the command M 18 3 to our path’s d attribute.
  2. Subsequent we need to use the L command to create a line that attracts from our path’s start line alongside the x axis for 28px. To do this let’s add our second command: L 46 3. Examine your preview and you need to see a small horizontal line.
  3. Now let’s draw a line straight down for 37px by including L 46 40.
  4. Then straight to the fitting by 15px with L 61 40
  5. Subsequent up we now have to start creating the arrow level. We have to draw a line diagonally down and to the left. We’ll do that with L 32 68.
  6. After which we’ll have a line go diagonally again up and to the left with L 3 40.
  7. Now we’ll end our arrow head by drawing a bit methods to the fitting once more with L 18 40.
  8. To shut our form we don’t must specify a degree to attract a line to. All we have to do is add the Z command, which can routinely shut our form for us.

Your ultimate arrow path code ought to seem like this:

1
<path d="
2
  M 18 3
3
  L 46 3
4
  L 46 40
5
  L 61 40
6
  L 32 68
7
  L 3 40
8
  L 18 40
9
  Z
10
"></path>

For more information on working with <path> try the references on the backside of the web page.

8. Add <defs> Aspect

We’re all achieved coding up our six icons, so now we will get them prepared for placement and reuse in our SVG.

To do that we’re going to wrap the code for all six of our, (presently commented out), icons with the tags <defs></defs>:

1
<defs>
2
<!-- All of the icons you created to this point go in right here -->
3
</defs>

This tells the system that every one the icons we’ve made are to be hidden by default, till we explicitly use them.

Now you can uncomment every of your icons they usually received’t be seen on the web page.

9. Create Teams With <g>

There are two methods we will make our icons prepared to be used: by changing them to teams, or into symbols. We’ll flip the primary half of the icons into teams, and the second half into symbols so we will illustrate the distinction.

To transform considered one of our icons into a gaggle all we now have to do is wrap it with <g></g> tags. To make that group usable we additionally want to provide it a singular ID.

For instance:

1
<g id="leftalign">
2
<!-- Left align icon made with strains -->
3
<line x1="3" y1="3" x2="48" y2="3"></line>
4
<line x1="3" y1="19" x2="65" y2="19"></line>
5
<line x1="3" y1="35" x2="48" y2="35"></line>
6
<line x1="3" y1="51" x2="65" y2="51"></line>
7
</g>

Wrap every of your first three icons with <g></g> tags and add distinctive IDs, like so:

1
<g id="leftalign">
2
<!-- Left align icon made with strains -->
3
<line x1="3" y1="3" x2="48" y2="3"></line>
4
<line x1="3" y1="19" x2="65" y2="19"></line>
5
<line x1="3" y1="35" x2="48" y2="35"></line>
6
<line x1="3" y1="51" x2="65" y2="51"></line>
7
</g>
8

9
<g id="rightcaret">
10
<!-- Proper caret icon made with a polyline -->
11
<polyline factors="
12
  3 3
13
  30 28
14
  3 53
15
"></polyline>
16
</g>
17

18
<g id="browser">
19
<!-- Browser icon made with rectangle and contours -->
20
<rect x="3" y="3" width="80" peak="60"></rect>
21
<line x1="3" y1="19" x2="83" y2="19"></line>
22
<line x1="20" y1="3" x2="20" y2="17"></line>
23
</g>

10. Place Teams With <use>

We now have three icons outlined as teams in our <defs> factor, so we’re prepared to make use of them in our SVG. To realize this, all we have to do is add a <use> factor, (being certain so as to add it after and outdoors the <defs> factor), and set an href attribute to focus on the specified icon’s ID.

For instance, to make use of the left align icon add this code:

1
<use href="#leftalign"></use>

To place the icon in a selected location add x and y attributes:

1
<use href="#leftalign" x="100" y="100"></use>

The code so as to add all three icons and area them aside would look one thing like this:

1
<use href="#leftalign" x="100" y="100"></use>
2

3
<use href="#rightcaret" x="300" y="100"></use>
4

5
<use href="#browser" x="500" y="100"></use>

Examine your browser and you need to see all three of your first icons:

usergroupsusergroupsusergroups

11. Create Symbols With <image>

As a substitute of utilizing teams to outline your icons you can too use symbols.
Symbols are virtually the identical as teams, nevertheless you achieve entry to
further settings to manage the viewbox and facet ratio.

This may be very helpful if you wish to do issues like centering the icons we’ve created to this point. We’ll flip the remaining three icons into symbols, then modify them in order that they’ll vertically fill a 100px excessive area, and be horizontally centered in that area.

We create our symbols in the identical means as our teams, solely we’ll be wrapping every of our final three icons’ code in <image></image> tags. We’ll additionally want so as to add a singular ID to every.

Nonetheless what we’re additionally going so as to add is a viewBox attribute. This can allow us to outline what the seen portion of every image ought to be. When the browser has entry to this data it could possibly then scale and align symbols appropriately.

We’ll want to provide our viewBox attribute 4 values. The primary two will outline the highest left level of our icon, and the third and fourth outline its width and peak respectively.

Beginning with our “alert” icon, its width and peak are each 86px so we are going to set its viewBox to 0 0 86 86 like so:

1
<image id="alert" viewBox="0 0 86 86">
2
<!-- Alert icon made with ellipses and a line -->
3
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>
4
<ellipse type="fill:black;" cx="43" cy="65" rx="5" ry="5"></ellipse>
5
<line type="stroke-width: 8;" x1="43" y1="19" x2="43" y2="48"></line>
6
</image>

The “play” icon can also be 86px in width and peak, and the “obtain” icon is 64px large by 71px excessive. The corresponding code for our symbols ought to subsequently be:

1
<image id="alert" viewBox="0 0 86 86">
2
<!-- Alert icon made with ellipses and a line -->
3
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>
4
<ellipse type="fill:black;" cx="43" cy="65" rx="5" ry="5"></ellipse>
5
<line type="stroke-width: 8;" x1="43" y1="19" x2="43" y2="48"></line>
6
</image>
7

8
<image id="play" viewBox="0 0 86 86">
9
<!-- Play icon made with ellipse and polygon -->
10
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>
11
<polygon factors="35 23, 60 43, 35 63" />
12
</g>
13

14
<image id="obtain" viewBox="0 0 64 71">
15
<!-- Obtain icon made with path -->
16
<path d="
17
  M 18 3
18
  L 46 3
19
  L 46 40
20
  L 61 40
21
  L 32 68
22
  L 3 40
23
  L 18 40
24
  Z
25
"></path>
26
</image>

12. Place Symbols With <use>

We will now use our image icons in the identical means as we did our teams. Nonetheless we’re additionally going to offer every with width and peak attributes set to 100:

1
<use href="#alert" x="100" y="200" width="100" peak="100"></use>
2

3
<use href="#play" x="300" y="200" width="100" peak="100"></use>
4

5
<use href="#obtain" x="500" y="200" width="100" peak="100"></use>

You’ll see that every considered one of your image based mostly icons neatly fills and aligns in its 100px by 100px area:

usesymbolsusesymbolsusesymbols

Attempt making use of width and peak attributes to the <use> parts of considered one of your group based mostly icons. You’ll discover that nothing adjustments. It is because the browser depends on viewBox values, (which a gaggle can not have), with a view to know how you can scale the icons.

Wrapping Up

Right here’s how your accomplished code ought to look:

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
  <meta charset="UTF-8">
5
  <meta identify="viewport" content material="width=device-width, initial-scale=1.0">
6
  <meta http-equiv="X-UA-Suitable" content material="ie=edge">
7
  <title>Hand Coded SVG</title>
8
  <type>
9
  html, physique {
10
    peak: 100%;
11
    width: 100%;
12
    background: #e9e9e9;
13
  }
14
  physique {
15
    margin: 0;
16
    text-align: heart;
17
  }
18
  .grid {
19
    width: 750px;
20
    peak: 500px;
21
    margin: 0 auto;
22
    padding-top: 100px;
23
    padding-left: 100px;
24
    background-image: url('grid.png');
25
    place: relative;
26
  }
27
  .grid::earlier than {
28
    content material: "";
29
    border-left: 1px stable #7c7cea;
30
    place: absolute;
31
    prime: 0;
32
    left: 100px;
33
    width: 750px;
34
    peak: 600px;
35
  }
36
  .grid::after {
37
    content material: "";
38
    border-top: 1px stable #7c7cea;
39
    place: absolute;
40
    prime: 100px;
41
    left: 0;
42
    width: 850px;
43
    peak: 500px;
44
  }
45
  svg {
46
    stroke: rgb(0, 0, 0);
47
    stroke-width: 5;
48
    stroke-linecap: spherical;
49
    stroke-linejoin: spherical;
50
    fill: none;
51
  }
52
  </type>
53
</head>
54
<physique>
55

56
<div class="grid">
57

58
<svg width="750" peak="500">
59

60
<defs>
61

62
<g id="leftalign">
63
<!-- Left align icon made with strains -->
64
<line x1="3" y1="3" x2="48" y2="3"></line>
65
<line x1="3" y1="19" x2="65" y2="19"></line>
66
<line x1="3" y1="35" x2="48" y2="35"></line>
67
<line x1="3" y1="51" x2="65" y2="51"></line>
68
</g>
69

70
<g id="rightcaret">
71
<!-- Proper caret icon made with a polyline -->
72
<polyline factors="
73
  3 3
74
  30 28
75
  3 53
76
"></polyline>
77
</g>
78

79
<g id="browser">
80
<!-- Browser icon made with rectangle and contours -->
81
<rect x="3" y="3" width="80" peak="60"></rect>
82
<line x1="3" y1="19" x2="83" y2="19"></line>
83
<line x1="20" y1="3" x2="20" y2="17"></line>
84
</g>
85

86
<image id="alert" viewBox="0 0 86 86">
87
<!-- Alert icon made with ellipses and a line -->
88
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>
89
<ellipse type="fill:black;" cx="43" cy="65" rx="5" ry="5"></ellipse>
90
<line type="stroke-width: 8;" x1="43" y1="19" x2="43" y2="48"></line>
91
</image>
92

93
<image id="play" viewBox="0 0 86 86">
94
<!-- Play icon made with ellipse and polygon -->
95
<ellipse cx="43" cy="43" rx="40" ry="40"></ellipse>
96
<polygon factors="35 23, 60 43, 35 63" />
97
</g>
98

99
<image id="obtain" viewBox="0 0 64 71">
100
<!-- Obtain icon made with path -->
101
<path d="
102
  M 18 3
103
  L 46 3
104
  L 46 40
105
  L 61 40
106
  L 32 68
107
  L 3 40
108
  L 18 40
109
  Z
110
"></path>
111
</image>
112

113
</defs>
114

115
<use href="#leftalign" x="100" y="100"></use>
116

117
<use href="#rightcaret" x="300" y="100"></use>
118

119
<use href="#browser" x="500" y="100"></use>
120

121
<use href="#alert" x="100" y="200" width="100" peak="100"></use>
122

123
<use href="#play" x="300" y="200" width="100" peak="100"></use>
124

125
<use href="#obtain" x="500" y="200" width="100" peak="100"></use>
126

127
</svg>
128

129
</div>
130

131
</physique>
132
</html>

That covers the necessities of hand coding SVG! Let’s recap and summarize what we discovered:

  • Setup your <svg> factor to wrap your entire graphic.
  • Use <line> and <polyline> to create strains.
  • Use <rect>, <ellipse> and <polygon> to create closed shapes.
  • Use <path> to create something you need.
  • Group shapes with the <g> factor.
  • For group like habits with additional options, use <image>
  • Use the <defs> factor to outline any reusable symbols and teams.
  • Place your outlined reusable symbols and teams with the <use> factor.

We discovered some stable foundations on this tutorial, however there’s a lot extra you are able to do with SVG so don’t cease right here, preserve digging and discovering extra of the superior issues that may be achieved!

Within the meantime, hopefully you now not really feel solely depending on vector design apps on your SVG creation, and also you’re assured to provide a few of your individual graphics by hand. For extra complicated graphics, design apps are nonetheless the way in which to go, however there’s an entire lot you are able to do with the constructing blocks you may have at your disposal, profiting from the pace and management hand coding brings.

Associated Hyperlinks:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments