There are three elements which can be used to construct straight or curved paths. They use a similar set of style attributes which are discussed at the end of the page.

<line>

A line between two points (x1,y1) and (x2,y2) may be drawn with the <line> element.

<line x1="x1" y1="y1" x2="x2" y2="y2" [style attributes] />

Examples

The green line starts at the point (25,25) and ends at the point (475,25). The stroke (colour) is encoded as #00ff00 which is lime green. The stroke-width is 7. All other attributes revert to their default values.

<line x1=”25″ y1=”25″ x2=”475″ y2=”25″ stroke=”#00ff00″ stroke-width=”7″/>

The red line starts at the point (25,25) and ends at the point (475,25). The stroke is encoded as #ff0000 which is pure red. The stroke-width is 10 and the stroke-linecap is round. All other attributes revert to their default values.

<line x1=”25″ y1=”25″ x2=”475″ y2=”25″ stroke=”#ff0000″ stroke-linecap=”round” stroke-width=”10″/>

The three descending slanted blue lines demonstrate the use of the stroke-opacity attribute. The first line has an opacity set to 0.1 which is nearly transparent, the second line has an opacity of 50% and the third line has an opacity set to 1.0 which is fully opaque. All three have the stroke set to pure blue which is encoded as #0000ff and a stroke-width of 15. All other attributes revert to their default values.

<line x1=”25″ y1=”25″ x2=”150″ y2=”75″ stroke=”#0000ff” stroke-opacity=”0.1″ stroke-width=”15″/> <line x1=”175″ y1=”25″ x2=”300″ y2=”75″ stroke=”#0000ff” stroke-opacity=”0.5″ stroke-width=”15″/> <line x1=”325″ y1=”25″ x2=”450″ y2=”75″ stroke=”#0000ff” stroke-opacity=”1.0″ stroke-width=”15″/>

The slanted magenta line starts at the point (25,75) and rises to the point (475,25), demonstrating the dash-array attribute. In this instance, the pattern is a long dash (length 10), a short space (length 2), a medium dash (length 5) and a very long space (length 15). The pattern then repeats. The stroke is set to magenta which is encoded as #ff00ff and the stroke-width is 5. All other attributes revert to their default values.

<line x1=”25″ y1=”75″ x2=”475″ y2=”25″ stroke=”#ff00ff” stroke-dasharray=”10 2 5 15″ stroke-width=”5″/>

<polyline>

If you wish to draw multiple lines that are connected together, one after another, then you can either use several <line> elements or a <polyline> element. Therefore, if line #1 has endpoints (x1,y1) and (x2,y2), line #2 has endpoints (x2,y2) and (x3,y3), … and line #(n−1) has endpoints (xn−1,yn−1) and (xn,yn), then they may be written using a <polyline> element.

<polyline points="x1,y1 x2,y2 x3,y3 ... xn,yn" [style attributes] />

Examples

The red zigzag line has a stroke of #ff0000 and a stroke-width of 10. It begins at the point (25,25), moves to the point (100,75), then (175,25), then (250,75), then (325,25), then (400,75), before ending at (475,25).

<polyline points=”25,25 100,75 175,25 250,75 325,25 400,75 475,25″ fill=”none” stroke=”#ff0000″ stroke-width=”10″/>

We have not seen the fill attribute before. What happens if we leave it out?

<polyline points=”25,25 100,75 175,25 250,75 325,25 400,75 475,25″ stroke=”#ff0000″ stroke-width=”10″/>

An invisible line seems to connect up the first and last points, and the spaces between this invisible line and the explicit line are filled in with the default colour. If you simply want a line, be sure to set fill to none or you will find your image blotted by baffling areas of black!

The line drawn by <polyline> can take all of the various line attributes. The black zigzag line has rounded line caps, bevelled line joins and an opacity set to 40%.

<polyline points=”25,25 100,75 175,25 250,75 325,25 400,75 475,25″ fill=”none” stroke=”#000000″ stroke-linecap=”round” stroke-linejoin=”bevel” stroke-opacity=”0.4″ stroke-width=”10″/>

The final example uses the fill attribute to create a yellow (#ffff00) triangle with a dotted cyan (#00ffff) line on two sides.

<polyline points=”25,25 475,25 475,75″ fill=”#ffff00″ stroke=”#00ffff” stroke-dasharray=”5 5″ stroke-width=”5″/>

<path>

A <path> element allows you to draw a series of lines and/or arcs which may be joined together or disjoint. It can be used to render quite complicated images. It is useful to think of a pen drawing (hence the d= command) the image as one or more lines/curves/arcs which may or may not be connected.

<path d="M x1,y1 [movement attributes]" [style attributes] />

The movement attributes are what make the <path> element so complicated. The first command is always M x1,y1 which moves the pen to its starting position at the point (x1,y1). Some of the more commonly used drawing commands are listed below.

CommandNameValuesDescription
Mmovetox,yMove the pen to the point (x,y) without drawing.
mmovetox,yMove the pen to the point x units horizontally and y units vertically from the current position without drawing.
Llinetox,yDraw a line from the current position to the point (x,y).
llinetox,yDraw a line to the point x units horizontally and y units vertically from the current position.
Hhorizontal linetoxDraw a horizontal line from the current position to the point (x,yc) where yc is the y-coordinate of the current position.
hhorizontal linetoxDraw a horizontal line to a point x units horizontally from the current position.
Vvertical linetoyDraw a vertical line from the current position to the point (xc, y) where xc is the x-coordinate of the current position.
vvertical linetoyDraw a vertical line to a point y units vertically from the current position.
Aelliptical arcrx,ry r large-arc-flag, sweep-flag x,yDraw an elliptical arc from the current position to the point (x,y). The ellipse has an x-radius of rx and a y-radius of ry. It is rotated clockwise r degrees. The large-arc-flag and sweep-flag take the values 0 or 1 and control which section of the ellipse is used.
aelliptical arcrx,ry r large-arc-flag, sweep-flag x,ySame as A, interpreting the coordinates as being relative to the current position.
Ccubic Bezier curvetox1,y1 x2,y2 x,yDraw a cubic Bezier curve from the current position to the point (x,y) using the starting control point (x1,y1) and the ending control point (x2,y2) to define the shape of the curve.
ccubic Bezier curvetox1,y1 x2,y2 x,ySame as C, interpreting the coordinates as being relative to the current position.
Qquadratic Bezier curvetox1,y1 x,yDraw a quadratic Bezier curve from the current position to the point (x,y) using the control point (x1,y1) to define the shape of the curve.
qquadratic Bezier curvetox1,y1 x,ySame as Q, interpreting the coordinates as being relative to the current position.
ZclosepathDraw a line from the current point to the first point.
zclosepathSame as Z.

Examples

The blue line is drawn using <line> whilst the identical red line is drawn using <path>.

<line x1=”25″ y1=”25″ x2=”475″ y2=”25″ stroke=”#0000ff” stroke-width=”7″/> <path d=”M 25,25 L 475,25″ stroke=”#ff0000″ stroke-width=”7″/>

The blue zigzag line is drawn using <polyline> whilst the identical red zigzag line is drawn using <path>.

<polyline points=”25,25 100,75 175,25 250,75 325,25 400,75 475,25″ fill=”none” stroke=”#0000ff” stroke-width=”10″/> <path d=”M 25,25 L 100,75 L 175,25 L 250,75 L 325,25 L 400,75 L 475,25″ fill=”none” stroke=”#ff0000″ stroke-width=”10″/>

The closepath command is a convenient shortcut if your path starts and ends from the same point. The blue triangle uses lines between three points whilst the red triangle uses lines between two points, followed by the Z command. Note the difference in the join between the lines in the lower left corner of each triangle. Always use the Z command to close a path.

<path d=”M 25,175 L 225,175 L 225,25 L 25,175″ fill=”#0000ff” stroke=”#000000″ stroke-width=”5″/> <path d=”M 25,175 L 225,175 L 225,25 Z” fill=”#ff0000″ stroke=”#000000″ stroke-width=”5″/>

A Bezier curve is a smooth curve defined by a set of control points which determine how the curve is shaped. The first and last control points are also the endpoints of the curve but the intermediate control points usually lie outside the path. We will not discuss Bezier curves further.

The elliptical arc command is the most complex of the curves. An elliptical arc is part of an ellipse which is centred at the point (x,y) with an x-radius of length rx and a y-radius of length ry which is rotated r degrees clockwise about the centre of the ellipse. The large-arc-flag and the sweep-flag determine which of the four possible arcs are chosen and are the source of endless confusion. They take the values 0 or 1. I usually have to play with all four permutations before I hit on the right combination to get the arc that I want.

Consider the four possible elliptical arcs between two points, the starting point in blue and the ending point in red.

large-arc-flag = 0 sweep-flag = 0 large-arc-flag = 0 sweep-flag = 1 large-arc-flag = 1 sweep-flag = 0 large-arc-flag = 1 sweep-flag = 1

Examples

Here is a nonsensical example of a path consisting of line segments and elliptical arcs. We begin with a horizontal line segment, followed by an arc based on an ellipse with rx = 20 and ry = 50 with no rotation. Both flags are set to 0. This is followed by another line segment. Then there is another elliptical arc, again with rx = 20 and ry = 50 but with a rotation of 45° clockwise. The path finishes with another horizontal line segment.

<path d=”M 25,25 L 100,25 A 20,50 0 0,0 175,25 L 325,25 A 20,50 45 0,0 400,25 L 475,25″ fill=”none” stroke=”#000000″ stroke-width=”5″/>

More arcs which look like a crescent Moon, a quarter Moon and a gibbous Moon.

<path d=”M75,25 A 25,25 0 1,1 75,125 A 15,25 0 1,0 75,25″ fill=”#ff0000″ stroke=”#000000″ stroke-width=”3″/> <path d=”M225,25 A 25,25 0 1,1 225,125 Z” fill=”#ffff00″ stroke=”#000000″ stroke-width=”3″/> <path d=”M375,25 A 25,25 0 1,1 375,125 A 15,25 0 1,1 375,25″ fill=”#0000ff” stroke=”#000000″ stroke-width=”3″/>

Style attributes

This is not an exhaustive list but it does include some of the more commonly used style attributes.

AttributePurposeValueDefault
fillControl the colour of the area enclosed by lines/curves.none, colour name, or colour code.black
fill-opacityControl the opacity of the area enclosed by lines/curves.A decimal number between 0 and 1, with values toward 0 more transparent and values toward 1 more opaque.1
strokeControl the colour of the line/curve.none, colour name, or colour code.black
stroke-dasharrayControl the appearance of a dashed line/curve.One or more sets of two numbers, the first number in the set giving the length of the dash and the second number in the set giving the length of the space.
stroke-linecapControl the shape of the end of the line/curve.Several values are possible, including butt (the line is cut off exactly at the end of the line), round (the line extends slightly past the end and has a rounded appearance), and square (the line extends slightly past the end and has a squared appearance).butt
stroke-linejoinControl how the join between two lines/curves is rendered.Several values are possible, including bevel (flat join), miter (pointed join), and round (rounded join).miter
stroke-opacityControl the opacity of the line/curve.A decimal number between 0 and 1, with values toward 0 more transparent and values toward 1 more opaque.1
stroke-widthControl the thickness of the line/curve.A number or a number plus unit of measurement (such as px or em).1