Skip to content
Tutorials Center
My learning

Topics

Lesson 81 of 122 ยท Graphics

SVG Paths, Text and Gradients

Draw any shape with SVG path commands, add real text, fill shapes with linear and radial gradients, reuse parts with g and use, and make SVG accessible.

The basic shapes from SVG Basics get you surprisingly far, but real-world graphics have curves: the swoosh of a logo, the outline of a speech bubble, the line of a chart. For those, SVG has one extremely flexible element, <path>, which can draw any shape at all.

In this lesson you'll learn to read and write path data, add text to drawings, fill shapes with gradients, organize and reuse parts with <g> and <use>, and make your inline SVG accessible to everyone.

The path element

A <path> has one essential attribute: d, short for data. It contains a series of commands, each a letter followed by numbers. Think of it as instructions for a pen:

HTML
<path d="M 10 80 L 60 20 L 110 80 Z" />

Read it like this: Move the pen to (10, 80) without drawing. Draw a Line to (60, 20). Draw a line to (110, 80). Close the shape with Z, drawing back to the start. The result is a triangle.

Here are the commands you'll use most:

Command Parameters Meaning
M x y Move to a point (start a new sub-path)
L x y Line to a point
H x Horizontal line to an x position
V y Vertical line to a y position
Q x1 y1 x y Quadratic Bรฉzier curve with one control point
C x1 y1 x2 y2 x y Cubic Bรฉzier curve with two control points
A rx ry rotation large-arc sweep x y Elliptical arc
Z โ€” Close the path back to its start

Uppercase commands use absolute coordinates. Lowercase versions (m, l, h, v, q, c, a, z) are relative: their numbers are offsets from the current pen position. l 20 0 means "draw 20 units to the right of where I am". Relative commands are handy for shapes you want to move around, because only the first M needs to change.

Commas and spaces are interchangeable, and you can repeat parameters without repeating the letter, so L 10 10 20 20 draws two lines.

Curves

A Bรฉzier curve bends toward its control points without passing through them. With Q, one control point pulls the curve; with C, two control points shape the start and the end separately. The shortcuts T and S continue a curve smoothly by mirroring the previous control point.

The arc command A draws part of an ellipse. Its two flags choose between the four possible arcs that connect the same two points: large-arc picks the longer (1) or shorter (0) way around, and sweep picks the clockwise (1) or counterclockwise (0) direction.

Lines, curves and arcs

Try moving the red control point: change both 230 10 values in the Q command and the circle, and watch the curve follow. Nobody writes complex paths entirely by hand; design tools such as Inkscape or Figma export them for you. But knowing the commands lets you read, tweak and animate those exported paths.

Text in SVG

The <text> element places real, selectable text in a drawing. Its x and y attributes set where the text's baseline starts, the line letters sit on, not its top-left corner.

  • text-anchor aligns text horizontally around x: start, middle or end.
  • dominant-baseline="middle" (or central) centers text vertically around y, which is handy for labels inside shapes.
  • Font properties work as in CSS: font-family, font-size, font-weight. The text color is set with fill, not color.
  • <tspan> styles or repositions part of a text, much like <span> in HTML. Its dx and dy attributes shift it relative to the previous text.

SVG text does not wrap automatically. For multiple lines, use several <text> elements or <tspan> elements with dy offsets.

Labels and a simple bar chart

Because it's real text, search engines can index it, users can select and copy it, and find-in-page can find it. That's a big advantage over text baked into a bitmap image.

Text on a path

<textPath> makes text follow any path, which is how curved labels and badges are made. Put it inside a <text> element and point href at a <path> (the path can live in <defs> so it isn't drawn).

  • startOffset moves the starting point along the path. It takes a length or a percentage of the path's length, so startOffset="50%" with text-anchor="middle" centers the text on the curve.
  • side="right" (where supported) puts the text on the other side of the path, and method and spacing tune how glyphs are placed on curves.
  • Very sharp turns squash or spread letters, so keep curves gentle.

Markers: arrowheads and dots on lines

A <marker> is a small drawing that SVG repeats at the vertices of a line, path or polyline. Arrowheads are the classic use. You define the marker once in <defs> and attach it with marker-start, marker-mid or marker-end (or the marker shorthand).

The attributes that matter:

  • markerWidth and markerHeight set the marker's box size (default 3). They are measured in multiples of the line's stroke width unless markerUnits="userSpaceOnUse".
  • viewBox gives the marker its own coordinate system, like on <svg>.
  • refX and refY pick the point of the marker that sits exactly on the vertex, so an arrow's tip can touch the end of the line.
  • orient="auto" rotates the marker to follow the line direction (auto-start-reverse flips the start marker).
Curved text and arrowheads

The polyline gets an arrowhead at both ends (auto-start-reverse turns the first one around) and a pink dot on every vertex in between.

Grouping with g

The example above already used <g>, the group element. A group has no appearance of its own, but:

  • Painting and font attributes set on it are inherited by its children, so you don't repeat fill four times.
  • A transform on the group moves, rotates or scales everything inside together.
HTML
<g transform="translate(100 50) rotate(45)">
  <rect x="-20" y="-20" width="40" height="40" fill="teal" />
</g>

Transforms are applied from right to left in effect: here the square is rotated around the group's origin and then moved to (100, 50). The available functions are translate(x y), rotate(angle) (or rotate(angle cx cy) to rotate around a point), scale(s) and skewX/skewY.

Gradients

Instead of a flat color, fill and stroke can reference a gradient. You define gradients inside <defs>, a container for things that aren't drawn directly, give them an id, and refer to them with url(#id).

A linear gradient blends colors along a line. Each <stop> sets a color at an offset from 0 (start) to 1 (end), or 0% to 100%. By default the gradient runs from the left edge of the shape to its right edge; change the direction with x1, y1, x2 and y2 (also from 0 to 1 by default, relative to the shape's bounding box).

A radial gradient blends outward from a center. cx, cy and r set the outer circle; fx and fy move the focal point where the first color starts, which can create a lit, 3D look.

Linear and radial gradients

Gradient IDs are global to the page. An id inside an inline SVG lives in the same namespace as every other id in the HTML document. If two inline SVGs both define id="shine", they'll clash. Use distinctive IDs, especially in reusable snippets. See IDs.

Reusing shapes with use and symbol

<use> draws a copy of another element, referenced by its id. The x and y attributes offset the copy:

HTML
<svg viewBox="0 0 200 60" width="200" height="60">
  <defs>
    <polygon id="star" points="20,2 25,15 39,15 28,24 32,38 20,30 8,38 12,24 1,15 15,15" />
  </defs>
  <use href="#star" fill="gold" />
  <use href="#star" x="50" fill="gold" />
  <use href="#star" x="100" fill="#e2e8f0" />
</svg>

Because the original star has no fill, each copy can set its own. Older code often writes xlink:href; plain href is the modern form and works in all current browsers.

The <symbol> element is a template meant only for <use>. Unlike shapes in <defs>, a symbol can have its own viewBox, so each <use> can set a width and height and the symbol scales to fit. That's the basis of SVG icon sprites: one hidden SVG full of symbols at the top of the page, and short <use href="#icon-search"> references wherever an icon is needed.

Accessible SVG

Screen readers need a text alternative for meaningful graphics, just like alt on an image. For inline SVG, the most reliable pattern is:

HTML
<svg role="img" aria-labelledby="chart-title chart-desc" viewBox="0 0 320 210">
  <title id="chart-title">Books read per season</title>
  <desc id="chart-desc">Winter is highest with 6 books; autumn is lowest with 2.</desc>
  โ€ฆ
</svg>
  • role="img" tells assistive technology to treat the whole SVG as one image, instead of announcing each shape.
  • <title> (the first child of the <svg>) gives a short name; <desc> gives a longer description. Many browsers also show the title as a tooltip.
  • aria-labelledby points at them by id, which makes the connection work consistently across browsers and screen readers.

For decorative SVG, such as an icon next to a text label that already says "Search", hide it instead: <svg aria-hidden="true">. You'll find more on these attributes in ARIA Roles, States and Properties.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the path data M 10 10 L 50 10 draw?

    M moves the pen to (10, 10) and L draws a straight line to (50, 10).
  2. 2. What is the difference between l 20 0 and L 20 0?

    Lowercase path commands use offsets from the current position; uppercase commands use absolute coordinates.
  3. 3. An SVG <text> has y="50". What is at y = 50?

    By default y sets the baseline. Use dominant-baseline to align the text differently.
  4. 4. How do you apply a gradient with id="glow" to a circle?

    Paint servers like gradients are referenced with the url(#id) syntax in fill or stroke.
  5. 5. Which markup makes a meaningful inline SVG chart accessible to screen readers?

    <svg> has no alt attribute. role="img" with a labelled <title> names the graphic; aria-hidden is only for decorative SVG.

Summary

  • <path d="โ€ฆ"> draws any shape with commands: M, L, H, V, Q, C, A, Z and their relative lowercase versions.
  • <text> adds real text positioned at its baseline; align it with text-anchor and dominant-baseline, and style parts with <tspan>.
  • <g> groups shapes so they share attributes and transforms.
  • Define linearGradient and radialGradient in <defs> and apply them with fill="url(#id)"; IDs must be unique across the whole page.
  • <use href="#id"> reuses shapes, and <symbol> with its own viewBox powers icon sprites.
  • Meaningful SVG gets role="img" and a <title> linked with aria-labelledby; decorative SVG gets aria-hidden="true".

Next lesson: Canvas Basics.