Skip to Content
SVG Text Layout
book

SVG Text Layout

by Amelia Bellamy-Royds, Kurt Cagle
October 2015
Intermediate to advanced
232 pages
5h 1m
English
O'Reilly Media, Inc.
Content preview from SVG Text Layout

Chapter 2. SVG Text Basics

For the simplest use cases, SVG text is straightforward. A short label can be added to a diagram with a single markup element and a pair of attributes.

SVG text, like SVG shapes, are positioned within a two-dimensional coordinate system. The coordinate system can be controlled by viewBox attributes and transform properties, but by default it starts in the top-left corner of the graphic. All points in the image are defined by their position relative to that origin point, along the horizontal x-axis and vertical y-axis.

The rest of the book will assume you are familiar with the SVG coordinate system. If any of that sounded confusing, you might have some background reading to do.

Letters on a Page

SVG text is, conveniently enough, drawn using the <text> element. Attributes on the <text> element define the position at which to start writing. The child text content of the element provides the words and letters to be written. It looks something like this:

<text x="10px" y="80%">SVG Text</text>

Put that code in a 400×80 SVG file, on top of a background rectangle, as in Example 2-1, and the result looks like Figure 2-1.

Example 2-1. Defining text within an SVG
<svg xmlns="http://www.w3.org/2000/svg"
     xml:lang="en"
     width="4in" height="0.8in" viewBox="0 0 400 80" >
    <title>Basic SVG Text</title>
    <rect width="100%" height="100%" fill="lightYellow" />
    <text x="10px" y="80%">SVG Text</text>
</svg>
Figure 2-1. Unstyled text in an SVG

Which…isn’t particularly exciting. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

SVG Colors, Patterns & Gradients

SVG Colors, Patterns & Gradients

Amelia Bellamy-Royds, Kurt Cagle
Mastering SVG

Mastering SVG

Rob Larsen
Using SVG with CSS3 and HTML5

Using SVG with CSS3 and HTML5

Amelia Bellamy-Royds, Kurt Cagle, Dudley Storey

Publisher Resources

ISBN: 9781491933817Errata Page