PRETTY MUCH DONE!

SMIL animations are done, with Shapes coming later and periodic overall polishing.

SCRAPPED: 3D animations (super cool, but too inconsistent across browsers / devices to be practical :cry:)

check out SVG templates here!
← back to /caco

NOTICE!

I've edited this rentry primarily on Firefox mobile, and a few elements of the svg images may look different than described because of how your device/browser renders them. A good principle is to set all necessary values for your text manually so that your device or browser don't have to guess and fill in the data with what you don't want.

↑ Examples include: Text-shadows (blurrier on Chromium), Default font-family differences (between devices), Extreme text skewing (sometimes jittery on Chrome, doesn't render extreme animations smoothly on Firefox), 3D CSS animation (overall super finnicky support between browsers)

...and literally nothing working for Safari (there's way too many examples). I've added a section 𝗡𝗲𝗿𝗲 in case you need proper support for some features in Safari browsers as well, and it will be referenced properly in related text.

Second notice!
While the url for this tutorial is /svgtutorial, pretty much every aspect covered here (except for shapes and animation groups) is actually CSS related. The "SVG" refers to embedding the CSS-animated text using an SVG data URI for Rentries.

Articles:
.> Basics
..> Setting up text
..> Colors
.> Fonts
..> Custom fonts?
.> Styling
..> Outlines (strokes)
..> Gradients
..> Shadows
.> Classes
.> Animations
..> Animation groups
.> Matrices (Matrixes)
.> SMIL animations
.> Shapes [TBD]
.> Safari workarounds


code has been scaled down to comfortably fit on mobile browsers. if you need to size it back up on PC, click the high contrast button

Introduction

You may have seen that some rentries have scrolling, high quality text - called "marquee text". Cool, right? These are usually achieved by embedding text and css animations for it within an svg image. As a typical person doesn't know how CSS/SVG code works, they use a site like this to achieve such effect:
https://trailblase.com/svg-generator/

But what if I told you that you could do much more than that...? :eyes:

This is a tutorial on how to create and use animated text within svg images. You can learn some pretty cool options knowing how the text code itself works - as well as extra settings that may not be included in a simple generator! I think it's important you understand the text-rendering basics first before getting into the animations themselves.

Basics

Setting up text

I'll start with this template:

βŽ—
βœ“
1
2
3
4
5
6
7
8
9
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 250 60' width='250' height='60'>
    <style>
      text { 
          font-family: monospace;
          font-size: 60px; }
      </style>
    <text x='0' y='45'>SAMPLE!</text>
  </svg>)

Let's learn some terminology first! The whole text { ... } block within CSS is called a "ruleset". A declaration is something like font-size: 60px within the text rule, containing both a property and a value.

You cannot use line breaks within the svg fields, otherwise it'd break the formatting and not render the text at all. Some svg texts embed their style data within /*<![CDATA[*/ ... /*]]>*/. This CDATA container is mostly redundant today, but it helped the parser (the thing that reads and interprets code) back then to render the css/style properties correctly without breaking, and unless your declarations within <style></style> are using <angled brackets> or the ampersand, you'll almost never need it. If by any chance you want to write an ampersand in your css code, you can write it like so: %26amp; (a normal & breaks it).

The viewBox and width/height values are necessary for proper rendering across all browsers, even if it takes a bit more effort to set up. This is because browsers set their own values automatically if they're not filled in manually. (A lot of marquee text actually don't have these properties set, which results in them appearing tiny down on Firefox)

And these are the values you need to know for adjustments:

viewBox = 'min-x min-y width height' | viewBox creates the "coordinate space"/"camera" for your text and other stuff to render
width = scales the display width size
height = scales the display height size

It's recommended to keep the viewBox width and height and the separate width/height values on the same numbers/ratio (i.e viewBox has w/h set to 200 50 whereas the separate w/h fields are 400 100. this creates a ratio of 1:2 which keeps things a bit more consistent during editing).

Template result:

y within <text> is set to 45 so that the text gets pushed downward below its baseline, so that it appears within the image bounds (by "image bounds" I mean both within viewBox and the svg width/height values)

I think you'll understand viewBox and width/height better if you see how the text renders with some changed values.

This is with viewBox width set to 150:

And 25:

Setting viewBox width back to 250, this is what you get if you change the viewBox height to 30:

^ Notice the bit of empty space that appears above the text whilst the text goes a bit downwards - because the viewBox "camera" is being pushed upward.

This is with viewBox height set to 15:

And 1:

Yup!


Given our example above, changing the text without anything else results in this:

It goes out of bounds and out of where our viewBox is defined. So what if we change the viewBox width to fit?

^ This is with viewBox width 530. But notice that the svg image itself is still the same size as our previous "SAMPLE!" text, with too much height above and below the text - as well as the fact that the text scaled down despite the font-size not changing at all.

We need to adjust the separate svg "width" value to match viewBox as well. This is what we get when we do that:

The separate width value within <svg> changes the display size of the image, whereas the viewBox width value changes the internal "coordinate space" I mentioned earlier.

Small thing: notice how the text above is ever so slightly clipping on the top at the "i". This is fixed by adjusting the svg height value. It's okay for the svg width/height and viewBox width/height to not perfectly match!


^ The height 65 text will be used throughout below.

Colorsβ €

Colors in text can be set using fill: within the text{} ruleset. Like this:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 530 60' width='530' height='65'>
    <style>
      text { 
          fill: red;
          font-family: monospace;
          font-size: 60px; }
      </style>
    <text x='0' y='45'>Simply awesome!</text>
  </svg>)

Which renders as:

All the available name colors are in:
colors!
(yes, you can nest svgs inside links if that wasn't obvious before lol)

If you want to use a specific hex value (#00aabb), or hex with transparency (#00aabbcc), you must replace the hashtag symbol with %23 (like URLs do) so as to not break the formatting. Below is an example with "fill:" set to %2300aabb:

And with "cc" transparency:

Any rgb(1, 2, 3) or rgba(1, 2, 3, 4) values can be written as: rgb%281, 2, 3%29, and rgba%281, 2, 3, 4%29. Parentheses "(" and ")" are written as %28 and %29 respectively. Percentage values also work, but if formatting somehow breaks (which it shouldn't), try writing percents as %25.

When working with rgba specifically, you must have all values be either 0-255 or percentages. Mixing them together, like rgba%280, 50%, 100%, 50%%29 doesn't make the fill color work and this is how the aforementioned RGBA values would render:

If you remove the commas on the RGBA values and add a slash / before the alpha value, then it works:

You can also control fill opacity separately by adding fill-opacity:, like this:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 530 60' width='530' height='65'>
    <style>
      text { 
          fill: %2300aabb;
          fill-opacity: 50%;
          font-family: monospace;
          font-size: 60px; }
      </style>
    <text x='0' y='45'>Simply awesome!</text>
  </svg>)

Which renders as:

Using a transparency value in fill whilst also having a fill-opacity set makes both values multiply. So for example if you had fill: rgba%280%, 100%, 100%, 50%%29; (where A is 50%) and fill-opacity: 50%, the two percentages multiply - resulting in 25% opacity:

Setting outlines and more is explained in the Styling section, below Fonts.

Fonts

Fonts (and by extension other elements) inside SVG images can only load what you already have / what your browser supports, which means it can't import any external fonts (like how Rentry's font metadata tags import fonts from Google)

Fonts are added in the text{} ruleset with font-family:, and the supported choices are:






If two or more fonts above look identical to you, that means your device/browser does not support it.

Some rentries may include additional fonts that exist in some browsers, such as:

But if you're not 100% sure that everyone's device will support rendering said font, you should include a fallback font. The example above uses:

font-family: Georgia, fantasy;

But you can include as many fonts as you practically can! Font names aren't case sensitive, but capitalizing makes them easier to read.

font-family: Georgia, 'Times New Roman', Arial, system-ui, sans-serif;

There are also a handful of other font options you can include for your text. font-weight can be set to either normal (400), bold (700) or a value between 100-900. If a font doesn't support having different weights, the browser will typically try "faking" it itself.

Below are rendered examples with normal, bold, and 550:
normal, bold, 550

Other options include:

  • font-style: - with normal, italic or oblique. Oblique can accept degree values up to 90deg or -90deg for custom slanting, but it doesn't render properly for me on Chrome desktop.

v Check image/alt text for context v
font-style: normal, italic, oblique, oblique -20deg

  • font-stretch / font-width - changes the horizontal width of text. By default uses percentages values but also has a few keywords built in. You should use font-stretch for compatibility; both names do the exact same thing.

font-width/stretch: normal, expanded, 30%, 170%
font-width/stretch: normal, expanded, 30%, 170%

  • font-variant: - changes how letters render. There's plenty of values for it.

normal, all-small-caps, petite-caps, unicase. Width slightly increased to accommodate unicase, and text slightly more spaced out.
font-variant: normal, all-small-caps, petite-caps, unicase

  • text-transform - changes how characters are capitalized. Values include uppercase, lowercase, capitalize, and full-width. Example below uses all four.

text-transform: uppercase, lowercase, capitalize, full-width

  • text-decoration-line - adds a line decoration to your text! Accepts underline, overline, line-through, and a deprecated option called blink. Many of these can be changed with the sub-properties below.

underline, overline, line-through, blink
^ text scaled down to see it properly

  • text-decoration-style - Changes how your line looks. Accepts solid, double, dotted, dashed, and wavy. Solid is default, and the others are shown below:

double, dotted, dashed, wavy

  • text-decoration-color and text-decoration-thickness set the color and thickness of your line. Color accepts any valid CSS color, and likewise goes for thickness with size values.
  • text-underline-offset and text-underline-position control the offset and position of the line.

deco-thickness with 2px and offsets at 10px, 20px, -15px with different colors

That covers underlines! You can also combine individual values into a single text-decoration: property. Below is an example with text-decoration: line-through blue dotted;

meow~

However, the text decoration inherits the color from the text's fill and stroke, so it doesn't really render as blue here...

And finally, letter-spacing and word-spacing change how letters and words are spaced out on your text.

letter 4px, letter -4px, word 10px, letter -10px word 25px
^ additional transparent red text to show word-spacing by itself more clearly

Yep!

SVG also has its own text properties: textLength and lengthAdjust. textLength declares a specific horizontal length for your text to render at - independent of any viewBoxes or other width values. lengthAdjust controls whether only the spaces between the letters (spacing) or the letters as well (spacingAndGlyphs) should be adjusted to fit textLength. You insert both inside <text>

tL 500 spacing, tL 500 spacingGlyphs, tL 100 spacingGlyphs

It's a pretty neat property to get a text style similar to well-known condensed fonts like Cossette Titre!

Custom fonts?β €

Does not work on Safari - you get it by now.

There is a way to embed custom fonts if you really want to - using something called a @font-face - but it requires more setup and takes up a considerable amount of space on your Rentry page.

↓ Open this in /edit to see what I mean.

The way font faces work is that they let you embed custom font files as Base64 URI data and later use them for your texts. Font files can be anywhere from ~70kB up to 500kB+ which is why you need to subset your fonts first before transforming them to a Base64 URI. ("Base64 URI" refers to the entire base64 string your font file is encoded in + the 'data:font/woff2;charset=utf-8;base64, container for it)

Subsetting is when you remove any characters from the font file that you will not use. If you do not, for example, subset a 100KB font file, it will turn into 133KB of base64 data - easily using up 66% of your Rentry page's total character limit (which is 200,000). If you're planning on using custom font faces for a Rentry, make sure you're 100% certain of which characters your text is gonna use before subsetting, as undefined ones will fall back to a default font.

I imagine font faces have a pretty niche use, since they only come in handy if a) your desired font doesn't exist on Google Fonts, or b) you want to use more than two fonts on your Rentry. It's a pretty neat trick regardless!

I used -
fontfaces!
- to subset and encode the example's font file. Just make sure you replace the resulting css parentheses with their corresponding %28 %29 characters so as not to break format. I used the woff2 format as well.

Stylingβ €

This one's fun!

Remember how I mentioned outlines will be explained in the styling section? I'll explain them first. If you've ever used Rentry's border metadata tags, some of these things will feel familiar to you!

Outlines (strokes)β €

You can add a basic outline to text by adding stroke: and stroke-width: to the text{} ruleset. Stroke is the color, and width is the thickness.

Like this:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 530 60' width='530' height='65'>
    <style>
      text { 
          fill: %23ffff00;
          font-family: monospace;
          stroke: red;
          stroke-width: 3px;
          font-size: 60px; }
      </style>
    <text x='0' y='45'>Simply awesome!</text>
  </svg>)

Which results in:

Notice how the stroke outline also goes a bit inwards on the text. This is because outlines are drawn from the text's edge, going half inside and half outside it.

You can change how the stroke renders with fill by adding paint-order: stroke fill;:

Paint order changes in what order parts of the text are drawn. stroke fill draws the stroke first, then the fill (the text) over it.

Like fill-opacity, you can also set a stroke-opacity in the text. Below is an example with stroke opacity set to 50%:

It's more visible if you make the text only display the outline without the fill. This is possible by setting fill: none;

And yes, you can get some pretty cool hollow letters with this! Below is an example with 1px stroke-width:

Setting the fill-opacity (or using the alpha color value) can give you transparent letters with opaque borders too. Below is an example of 25% fill-opacity with a 1px black outline:

^ This one uses fill stroke; for paint-order instead, as it looks more in-theme.

Suppose you were editing some very thick text and noticed something about the outline:

Do you see it?

The corners are pretty sharp! Like at the bottom of that "A" or at the end of the "S". So.. wat do? You can add stroke-linejoin: with the value round;. This smooths out sharp corners of your outline, which results in text like this:

The only other value is bevel, which is a middle road between miter (the default) and round. This is with bevel:

Yep!

You can make a text outline dotted by adding stroke-dasharray: 1 2; to it. The first number draws a 1 unit long dash, and the second number draws a 2 unit long gap - then the pattern repeats.

Zoomed in on just two letters with a different font (and round linejoin) to notice the pattern more clearly:

You can add more values to dasharray to create more varied patterns, the order is dash gap dash gap ... repeating. An example with 1 4 3 4:

You can get some cool results by toying around too!

Now onto...

Gradientsβ €

!!

Gradients take a bit more setup than your usual declaration. There are two types of gradients, <linearGradient> and <radialGradient>. Let's focus on linear first, with this template:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 60' width='200' height='60'>
       <defs>
        <linearGradient id='LG' x1='0' y1='0' x2='1' y2='0'>
           <stop offset='0' stop-color='red'/>
           <stop offset='1' stop-color='yellow'/>
        </linearGradient>
       </defs>
    <style>
      text { 
          fill: url%28%23LG%29;
          font-family: sans-serif;
          font-size: 70px; }
      </style>
    <text x='0' y='56'>GRAD!</text>
  </svg>)

Yes, these look like a lot more values, but it's not that complicated! Let me explain.

Before the <style></style> tags, two primary new ones appear: <defs> </defs> and <linearGradient> </linearGradient>. The linear gradient tags must go inside <defs></defs>, and inside <linearGradient>, you must define id='' in order to reference it as a color for your text later.

Your text references the color gradient with url(#ID), which - as shown in the template - is written as url%28%23ID%29. Since our template's linearGradient ID is "LG", fill: references LG.

x1 y1 x2 y2 are the points that set the direction of the gradient. They start from the top left corner of the text's bounding box, and +y goes downward. <stop>s are the data for what colors should be rendered on the gradient; they should go between the <linearGradient> tags and end appropriately with />. Think of offset='0' as 0% of the gradient, and offset='1' as 100% of the gradient. So a <stop> at offset=0.4 would change the color of the gradient on its 40% point, and 0.6 at 60%, and etc.

This is what the template rendered looks like:

As explained above, x1 y1 x2 y2 are the coordinate points for the gradient. If we change y2 to 1, the point at the top right corner goes down to the bottom right - creating a diagonal gradient.

Stop's offsets control how the gradient color renders. We can add a third <stop> in the middle of the existing two that changes the color to blue on the gradient's halfway point. (0.5; 50%)

We can tilt the diagonal gradient upward by changing the Y values of each point. y1 is set to 1, making it go down, and y2 is set to 0 bringing it back up.

And yes, a rainbow gradient is also very much possible!

^ I just tilted the points back to be a straight (no pun intended) gradient again.

You can add stop-opacity inside <stop> to set a stop point's opacity. Taking our rainbow example above, here is every 2nd gradient's opacity (orange, green, purple) set to 0.5:

And here it gradually fades out to the right:

A few other neat properties exist too! spreadMethod='' in <linearGradient> dictates what happens outside of a gradient's set range. Take our diagonal red-yellow example from earlier, but set x2 y2 both to 0.5. What do we get?:

As you can see, it fills in the end with yellow by default ('spreadMethod=pad'). But we can change that.

^ This is with 'spreadMethod=repeat' which repeats the gradient at the end. You can mirror it by also doing reflect:

And yes, you can do some wacky stuff too.

🧭 Safari notice!

Radial gradients on Safari - for some reason - are rendered differently compared to Chrome/Firefox. See Safari workarounds for more.

Now onto radial Gradients! Here's a template for it below:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 60' width='200' height='60'>
       <defs>
         <radialGradient id='RG' cx='0.5' cy='0.5' r='0.5'>
         <stop offset='0' stop-color='gold'/>
         <stop offset='1' stop-color='navy'/>
      </radialGradient>
       </defs>
    <style>
      text { 
          fill: url%28%23RG%29;
          font-family: sans-serif;
          font-size: 70px; }
      </style>
    <text x='0' y='56'>GRAD!</text>
  </svg>)

cx cy are the center coordinates and r is the radius. They are by default 0.5, and we get this result from the template:

This is what get get with cx moved to 0.3:

Radius controls... well, the radius! This is what we get with r='0.7':

Radius defines the size of the circle originating from the cx cy coordinates, and you can use fx fy (within <radialGradient>) to move the focal point - which is the point where the first gradient begins. Below is an example with fx set to 1:

You can see how the gradients spread out differently based on where the focal point is. Below is the focal point set to fx='0.5' fy='0.9'

Neat! And the aforementioned spreadMethod also applies to radial gradients. Here's repeat:

Dope results are available too!

There's a few more gradient properties I'd like to go over...

gradientUnits='' lets you change how the gradient's points are set. By default it is objectBoundingBox - which calculates numbers as fractions of each element's text box - but the other value is userSpaceOnUse. The latter allows you to set the numbers as if they were viewBox coordinates, with the points starting from the top left of your viewBox (0, 0).

If you have many gradients that use the same colors, you can set an ID for the first gradient and subsequently copy it over with other gradients using href='', like this:

βŽ—
βœ“
1
2
3
4
5
<linearGradient id='BG'>
  <stop offset='0' stop-color='pink'/><stop offset='1' stop-color='deepskyblue'/>
</linearGradient>
<linearGradient id='T1' href='%23BG' x1='0' y1='0' x2='1' y2='0'/>
<linearGradient id='T2' href='%23BG' x1='0' y1='0' x2='0' y2='1'/>

The above example defines a single gradient with colors and stops but no coordinates, and the two others provide their own coordinates while using the first gradient as a reference for colors and stops.

An example of using "T1" for the fill and "T2" for the stroke:

stroke: can reference IDs identically to fill: using url%28%23ID%29. The above example would probably be more practical to do with two separate linear gradients that have their own defined coordinates, as href referencing becomes more useful when you're working with many gradients. How many do you really need for a simple text?..

Shadowsβ €

There's a surprising amount of ways to render shadows on your text. The primary way is text-shadow within text{} data, like this:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 230 60' width='230' height='60'>
    <style>
      text { 
          fill: darkgray;
          text-shadow: 2px 2px 3px gray;
          font-family: sans-serif;
          font-size: 70px; }
      </style>
   <text x='0' y='56'>Shade!</text>
  </svg>)

The values are: text-shadow: x-offset y-offset blur color;. The template above renders as:

You can add more distance by increasing x/y offsets, and make it blurrier with the third value:

^ But notice how the shadow clips on the edges. You should adjust your viewBox and width/height ratios accordingly to make it fit!

A quick notice, though: In my testing, text-shadow is rendered way more blurred/distant on Chrome/S.Browser than on Firefox/Tor. This is what I meant by the top page notice regarding devices/browsers. I'd say it's still generally okay to use text-shadow if the values are minimal, and you should use a blur filter: - explained down below - if you just want blurry text without the fill. (Firefox doesn't render neither text- nor drop-shadows if the fill is none/transparent)

🧭 Safari notice!

None of the CSS filter:s work in Safari. See Safari workarounds.

If you want to keep your shadows consistent, you can use filter: drop-shadow() in the text data instead. The same values apply: x-offset, y-offset, blur and color. Below is an example with filter: drop-shadow%281px 1px 2px gray%29;:

And 2px 2px 4px: (with changed ratios to fit the shadow)

You can stack them by simply duplicating the filter!

^ This uses filter: drop-shadow%282px 2px 4px black%29 drop-shadow%28-2px -2px 4px white%29;

And this is the example above but with both blurs set to 0:

You can see it creates an emboss effect, similar to how ibisPaint does it. Hex colors are also supported, just don't forget the %23. For practical reasons, I recommend using drop-shadow if you also want to animate it for text later.

Another method to draw shadows is to use <feDropShadow> inside <filter></filter>. We've already experimented with adding linear/radial gradients inside <defs><defs/>, and this is essentially the same thing. Below is the template:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 235 65' width='235' height='65'>
     <defs>
       <filter id='shd' x='-0.2' y='-0.2' width='1.5' height='1.5'>
       <feDropShadow dx='2' dy='2' stdDeviation='2' flood-color='gray' flood-opacity='0.7'/>
        </filter>
      </defs>
    <style>
      text { 
          fill: gold;
          filter: url%28%23shd%29;
          font-family: sans-serif;
          font-size: 70px; }
      </style>
   <text x='0' y='56'>Shade!</text>
  </svg>)

Lots of things here! Let's break them down:

<feDropShadow> is inside <filter></filter>, which is inside <defs></defs>. Like gradients, you put an id='' identifier inside <filter> to later reference it as a filter: for your text.

x y width height function the same as the coordinates for viewBox, and they dictate where the filter is allowed to draw. -0.2 on both x and y means the region starts 20% to the left and above the text. Width and height change the size accordingly. However, using the filter changes how the text renders, and it must occupy the space for the shadow plus the text itself in order to render everything properly. Below is an example of the <filter> x being 0.1 (10% to the right of the top-left corner of the text) and y being 0.4 (40% down from the text):

You can see that it's not just the shadow being cut off, but also the text itself. The filter's x and y values function like viewBox's own coordinate space, but they start from the top-left text's corner.

🧭 Safari notice!

SVG filters make text render at a lower resolution on Safari. See Safari workarounds for a solution.

As mentioned in gradients earlier, you can add filterUnits='userSpaceOnUse' inside <filter> to make it work like real viewBox coordinates.

stdDeviation is the blur radius, and dx dy dictate the shadow offset. flood-color and flood-opacity are shadow attributes.

Now, with proper settings, this is how our template renders:

Moving the <text> offsets also moves the drop shadow with it. The same applies to the two previous drop shadow methods.

But you can move the drop shadow itself too, with dx! Moving our text back, this is dx set to 30:

You may be wondering how you can get just the shadow without the text. I probably should've mentioned this earlier, but you can do so by applying a blur filter: to the text itself!

^ The above example uses filter: blur%282px%29;.

If you've seen rentries that render blurry/foggy text (as if it were behind a window pane), it's either an image... or this!

Do other filters exist? Absolutely! Topping off the Styling section, we have:

  • filter: brightness(1.5). 1 is normal, 1.5 brightens, and 0 is black.


  • filter: contrast(1.5). 1 is normal, and lower numbers become more gray.

  • filter: saturate(1.5) 1 is normal, 0 is gray, and anything above 1 gets more vivid. o_0

  • filter: sepia(1) A brown tint!

  • filter: hue-rotate(90deg) Shifts colors on the color wheel!

  • filter: invert(1) Inverts colors!

AND...

  • filter: opacity(0.5) Self-explanatory! This is chainable too.

And that's it!

There are cooler SVG properties that can be applied to text, such as distortion (<feDisplacementMap>), but these will likely be explained in the later Shapes section - as I didn't think "Styling" would get this long. TwT

Just one last thing...

Classesβ €

One method of shadowing text I forgot to mention is by duplicating the text, moving it below then blurring it. A template for this is shown here:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 220 80' width='220' height='80'>
    <style>
        text { 
           font-family: sans-serif; 
           font-size: 60px; }
         .shadow { 
         fill: gray; 
         fill-opacity: 0.6; 
         filter: blur%282px%29; }
         .main { 
         fill: gold; }
      </style>
    <text class='shadow' x='14' y='62'>Shade!</text>
    <text class='main' x='10' y='58'>Shade!</text>
  </svg>)

You may wonder what those dots on ".shadow" and ".main" are. These are classes.

A class is essentially a template of values/declarations you can assign to a text field with class=''. When a text field is rendered with a set class, it uses the declarations within the text{} ruleset first, then applies the ones found in its class - with classes taking priority. In our template, the shadow is rendered first, then the fill second. It's like paint-order:, but you only need to properly arrange the <text> elements themselves.

The template result:

If classes define a value that's already set in the text{} ruleset, the class overrides it. Here is the above example but with font-family: monospace; added to both classes:

As you can see, even though the text ruleset defines sans-serif as the font, both classes override that.

Classes will become important later for animating text, which is why I chose to quickly insert a section about it!

Now, onto everyone's favorite....

Animationsβ €

YEAAHHHHHHH πŸ’₯ πŸ’₯ πŸ’₯ πŸ’₯ πŸ’₯

The reason this tutorial has gone over all the basics of how to render and style your text is because a lot of the things we've discussed above can be animated. And you can get some genuinely awesome effects with animation! ... but of course, I'll have to start with the basics of animation first.

The easiest thing to animate is tweaking a single value within, let's say, a filter. Here, we have a template for changing the hue:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 260 80' width='260' height='80'>
    <style>
      text {
        fill: red;
        font-family: sans-serif;
        font-size: 60px;
        animation: hue 4s linear infinite; }
      @keyframes hue {
        from { filter: hue-rotate%280deg%29; }
        to   { filter: hue-rotate%28360deg%29; }  }
      </style>
    <text x='10' y='60'>Rainbow!</text>
  </svg>)

As you can see, the text{} ruleset gains a new value: animation:. The 4 values in it are name duration ease repeat - however, the animation: property accepts a total of 7 unique values: name duration ease delay repeat direction fill-mode. You can set an animation name to whatever you want, so long as it isn't a keyword in itself (so for example, don't name it "linear" or "infinite").

The duration is for how long the animation lasts. The ease is how it moves, and the available values are: linear ease ease-in ease-out ease-in-out cubic-bezier(x1, y1, x2, y2) steps(n). "Linear" in our template means the animation moves linearly; that is, at a constant speed. We'll get to the other ease types later.

delay delays your animation by a set amount. repeat dictates how many times your animation repeats - and this is almost always set to infinite unless you're trying to do something more techy (like a page load animation).

direction controls the direction of the animation. Setting it to reverse plays every iteration backwards, alternate alternates it back and forth, and alternate-reverse does the same thing but starts with a reverse iteration first.

Then we get to the @keyframes block. This is what holds the frames for your animation, and our template uses a simple from to.

Given everything above, this is what our template looks like rendered:

You can change the 4s duration in animation: to be faster. This is at 2.5s:

Changing the ease slightly changes the hue shifting speed. This is at ease-in-out:

Notice how the hue hangs around the red colors more, then cycles through green and the other colors quicker.

Using steps() as your ease makes your animation... well, go in steps! Below is using steps%283%29:

There's only 3 steps to the animation, so it only shows three frames. Here's one with 12 steps instead:

cubic-bezier(x1 y1 x2 y2) essentially works like a graph with a straight diagonal line, where the x-axis is time and y-axis is animation progress. You can use cubic-bezier.com for a better visual representation, and below is an example with cubic-bezier(0, 0, 0.3, 0.9):

Want to apply more filters to a single text? It's possible! You can add additional effects separated by spaces. Taking the example above, here it is with blur%28 0px %29 added to the from keyframe, and a 5px one on to!

It doesn't matter if you have spaces between the values and the parentheses actually, just thought I'd point that out!

Notice that the rainbow text above starts blurring, then snaps back to being sharp. This is because our animation ease is in steps(), which animates linearly. You can change steps to alternate in order to make the animation alternate between the start and end; like reflect on gradients.

As the animation is no longer in steps, it doesn't look as choppy anymore. The hue changes backwards as the blur does as well.

The easiest way of moving the text itself is by using translate: x y within the @keyframes. An example is written like this:

βŽ—
βœ“
1
2
3
@keyframes movy {
   from { translate: 5px 60px; }
   to { translate: 25px 60px; } }

Let's take the rainbow and blur animations out of our previous text, and apply the translate animation above to it:

^ viewBox and width adjusted to fit.

Don't forget to change the animation name too! And do note that translate: begins moving text from 0; so if you have text at y=60 and translate also has y at 60px, the text will move 120 units downward in total.

SVG has its own version of animations called "SMIL animations" which have their own unique syntax and benefits, but this will be explained for much later...

Another way to animate text is by using transform: translate(0, 0). This is different from just translate: as transform is its own property with several options - including rotate, skew, and scale. The order of each transform animation matters.

Alongside translate:, there are other animation attributes like scale and rotate. Below is text that's animated from scale: 0.8; to scale: 1.2; - "1" is the default scale value.


But notice how the animation is working: it scales from the top left corner of the svg image... why?

By default, SVG transforms elements relative to an image's entire viewBox and from the (0, 0) coordinates if two attributes aren't explicitly set: transform-box and transform-origin. These are very important for animations, as both attributes define how your text is animated.

transform-box essentially sets another "coordinate space" for animation. By default it is set to view-box, which uses the SVG's entire viewBox as the coordinate space. transform-origin defines a pivot point within the transform-box around which certain animations - like scaling or rotation - happen. For a text SVG, you generally should set transform-box to fill-box, which defines the coordinate space within the text's bounds. Setting transform-origin: center; will set the pivot point in the center of the text - as defined with "fill-box". When using a scale animation with transform-box: fill-box; and transform-origin: center;, it defines the coordinate space within the text element and the origin point as the center of said text - thus, the scale animation makes the text grow from its center. Both transform- properties should be set within the text{} ruleset.

With the two attributes above set, this is what our animated text renders like now:

transform-origin also accepts x and y values, as well as percentages. 50% 50% behave identically as origin, and here is an example of transform-origin being 70% 50%:

And 100% 50%, which puts the transform pivot point to the rightmost edge of the text:

And yes, I didn't change any viewBox or width/height values to accommodate the text being cut off by the animation. This is to keep things consistent.

You may wonder why transform-box is set to fill-box instead of view-box even though both would behave almost identically with our example texts. And you're right! If the SVG viewBox and width/height is just big enough to house a single piece of text within its bounds, then both fill-box and view-box would act almost the same. But in cases where your text isn't perfectly centered in the SVG viewBox, using view-box will noticeably change things. Consider our example above, but with double the width: (text appears scaled down on mobile - enable desktop mode for accuracy!)

^ I've changed the origin point back to the center. Now let's set transform-box to view-box instead, which uses SVG's viewBox as the coordinate space instead of the text's bounds:

Now the center is closer to the text's rightmost edge, as the viewBox width is double the text's total width.

Setting the values back to their normal states, you can also input two values for scale: to scale the text on its x/y! This is what that looks like:

^ I sped up the animation to 1.5s from here on out.

Now that scale: is explained, here's rotate: in action! Like the hue-rotate filter, it uses degrees. Below is an example with the text rotating between 5deg and -10deg.

^ I've changed the animation ease to linear so it's easier to follow.

If two keyframes isn't quite doing it for you - which I imagine it likely isn't - you can use percentage values instead! They allow you to add multiple keyframes like this:

βŽ—
βœ“
1
2
3
0% { rotate: 5deg; } 
50% { rotate: 15deg; }
100% { rotate: -10deg; }

Which, when inserted into the text above, looks like this:

The snapping movement can be fixed by having the ending keyframe return to the first one's state. So if 0% started off with 5deg, 100% can end with 5deg as well.

And with the ease-in-out ease, as well as adjusted display size values:

It kinda looks like it's trying not to get blown away by the wind!

Now.. what about using multiple animations for a single text? It's possible! This is where we get into...

Animation groupsβ €

Groups in SVG are indicated with the <g> element, and they allow you to, well, group multiple elements together as a single unit. Nesting text inside groups allows you to apply multiple kinds of animations to it. Here we have a template:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 200' width='200' height='200'>
    <style>
      .orb {
        transform-box: view-box;
        transform-origin: center;
        animation: turn 6s linear infinite; }
      text {
        fill: gold;
        font-family: sans-serif;
        font-size: 24px;
        text-anchor: middle;
        transform-box: fill-box;
        transform-origin: center;
        animation: turn 2s linear infinite;  }
      @keyframes turn { 
        to { rotate: 360deg; } }
      </style>
    <g class='orb'><text x='150' y='105'>Spinny!</text></g></svg>)

You might have noticed a new option here: text-anchor: middle;. What's that?

text-anchor allows you to set which edge of the text sits on the x coordinate. By default it is set to start, which means the leftmost edge, but you can also set it to middle for the text's center or end for the rightmost edge. It keeps things consistent in our template.

In the above example, since we already have an animation within the text ruleset, it animates all text elements within the SVG image. The keyframes for the animation "turn" are already defined, so the ".orb" class can simply change the values of the animation property for itself without adding any keyframes.

First, the text is rendered in a square viewBox to the right from the origin center.

^ This is without the ".orb" class - and without the <g>roup using it on the text- in our template.

We can't simply insert another animation with different transform- declarations, as it'd simultaneously conflict with our existing first animation. This is what groups are for - you nest the text inside a group, which uses a class with different animation values to apply a second animation on top of the text!

You can think of it like trying to turn the same gear with two different wrenches. The gear can only be turned by one wrench at a time, so the gear is nested inside a container box that the second wrench can turn - while the first one is focused on just the gear itself!

This is the result with our ".orb" class and text group included:

^ with viewBox and w/h slightly adjusted to fit.

The group treats the already spinning text as a standalone unit, and uses the turn animation class (with the transform-origin point set to the viewBox center) to slowly spin the text clockwise.

🧭 Safari notice!

WebKit (Safari's browser engine) is pretty bad when it comes to rendering transform-box - the above examples will look broken if you're on an iPhone. Instead of using fill-box, I recommend having it at view-box (or remove the option altogether, since it already defaults to that) and setting the text coordinates manually through transform-origin. See Safari workarounds in /edit for an example.

The rotate: property also accepts turn (1turn = 360 degrees, 0.5turn = 180, etc), radians (2Ο€ rad = 360deg), and gradians (400grad = 360deg)

Groups are a general SVG element that do more than just animating text, but for the purposes of this tutorial, it's used exclusively for animated text!


Now that we've covered groups and the main three standalone rotate: translate: scale: properties, let's get to something slightly more customizable: transform:()! The transform:() property runs after the first aforementioned three, and it's got more options to play with! This includes:

  • All the standalone properties we discussed: translate(x, y), scale(x, y), and rotate(angle).
  • skew(ax, ay) to skew text!

Correlating transform animations can be combined with the standalone ones, such as using rotate: and transform:%28rotate: %29 at the same time with no conflict. If you want more than that, use classes. So, since we know how the existing TRS (translate/rotate/scale) properties work already, let's focus on the actually new option here: skew()!

Skew accepts two values: ax and ay. Skew distorts the text by an angle in the X and Y directions, so only the same values that rotate: accepts can be used for it.

Let's take this template for skewing text:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 230 55' width='230' height='55'>
    <style>
      text {
        fill: skyblue;
        font-family: sans-serif;
        font-size: 45px;
        transform-box: fill-box;
        transform-origin: center;
        animation: skw 1s alternate infinite;  }
      @keyframes skw { 
        from { transform: skew%28 20deg %29; }
        to { transform: skew%28 0deg %29; } }
      </style>
    <text x='15' y='40'>Skewy!</text></svg>)

Since we typed only one value in (x), it will only skew on the x-axis. The same effect can be achieved by using skewX(n) or skewY(n) for the y-axis.

Skewing relies on tangents to calculate how to skew the text. For example, a 45 degree skew on the horizontal axis (x) would result in the vertical edges of your text skewed at a 45 degree angle - so you can realistically skew up to ~89.9 degrees before rendering breaks. The tangents part will become a bit more relevant later.

This is how our template text renders:

And here, we input 2 values for ax and ay but only skew on the y-axis instead:

And yes, keeping the transform box options from before is important! But you can also change them to get more custom effects.

Below are examples with transform-origin set to both left and right:


Now if we add more keyframes and tweak our animation a bit, we can get some fun results as well!

Yep!

As a fun experiment, this is what you get with a looping, alternating 360deg skewing along both axes:

^ Okay I genuinely did NOT expect this result. This is extremely funny.

Oh, the transform-origin point was set to the right! This is the funny text above but with the origin at the center, and a slightly slower animation:

This looks arguably way cooler. The fun you get from learning all there is to CSS animations...

Now, as I've mentioned above, two animations with standalone TSRs and transform: declarations respectively can use the exact same property with no conflict. Here we have a simple rotating text:

And here, I apply a second animation wherein the @keyframes use transform: rotate(360deg) instead.

As you can see, both rotate animations co-exist and combine to make a twice-as-fast spinny text! And this is with the transform: rotate set to -360deg instead:

Pretty cool, right?

But now that I believe we've adequately covered combining animations and skew(), it's time to cover our last (but not least) animation property - matrices!

Matrices (Matrixes)β €

You should understand that all the previous translation and transform animations we've experimented with so far did a single thing in different ways: changing coordinates on the text. Matrices are a more "manual", mathematical way of doing that.

The matrix property is part of transform:, and is written like this:
transform: matrix(a, b, c, d, x, y);

Six values! But fear not, it's not that complex. Each value stands for:
a = horizontal scale
b = vertical skew
c = horizontal skew
d = vertical scale
x = horizontal movement
y = vertical movement

Every distorting or moving animation we've learned so far can also be done identically with matrices. By default, the values are
transform: matrix(1, 0, 0, 1, 0, 0); (called an "identity matrix"), which simply means normal vertical/horizontal scale with no additional translation or skewing. Below is a sample text with the aforementioned matrix:


As you can see, nothing changes from having the default matrix values. We can start with something simple like changing the vertical and horizontal movement values. This is with x, y set to 20, 10:

^ There's a duplicate transparent text with the original matrix values for comparison.

And x, y with -20, -10:

Next, we can change the horizontal and vertical scale values next (a and d)! Putting our text back in place, this is with the matrix (1.2, 0, 0, 0.8, 0, 0):

And (0.8, 0, 0, 1.2, 0, 0):

Now what about animating the matrices? By now you should know how the animation format works, so it's pretty intuitive from here!

^ You can open this in /edit to see the template.

Remember how I mentioned with skewing that it relies on tangents to calculate how to skew the text? This is where it becomes relevant for matrices now, as a matrix does not accept degree values in the same way skew() does.

To calculate the relevant value for a matrix skew, you need to input tan(n) where n is the amount of degrees (not radians) you want your text skewed at. tan(20Β°) for example is β‰ˆ 0.364. So to skew your text within a matrix by 20 degrees, you input 0.364 into the c (3rd number) value. c is for horizontal (x-axis) skew, whereas b is vertical.

I'm not qualified enough to speak on why the math behind this works, but it just does. If you didn't notice, there's a slightly differently colored text right below the matrix-skewed one for reference which uses the skewX property.

And this is an animation with both matrix skews using 0.364:

Since transform-origin is set in the center, the text is skewed from the middle of itself.

I've also said you can realistically go up to ~89.9 degrees. This is a slow animation showcase of what happens to your text when you approach 90Β° and go up to 180Β°:

Yep! That's also why our 360deg experiments looked so wacky.

But now that's matrices covered! Every transformation you've learned can be done with them as well - and I imagine combining animations together will be easy as well. Now let's get into something a bit new...

SMIL Animationsβ €

Everything you've learned about animations so far - which have been pure CSS - can pretty much be replicated within SVG! All you really need is to learn some new syntax, and you'd be good to go.

The main advantage of using SMIL animations is cross-browser compatibility. Someone viewing your text on Safari will almost certainly see the same thing as someone on Chrome or Firefox. (I imagine it's because SVG animations predate CSS ones by about a decade, so it technically has "more" support/consistency in that sense)

Let's begin with something simple!

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 350 40' width='350' height='40'>
    <style>
      text {
        fill: skyblue;  font-family: sans-serif;
        font-size: 30px; }
      </style>
   <text x='0' y='30'>
  Simply pawsome!
  <animate attributeName='x' from='0' to='100' dur='2s' repeatCount='indefinite'/></text></svg>)

If you've learned the @keyframes syntax already, this should feel instantly intuitive to you! Instead of having animations assigned through group classes, the <animate> element goes directly inside the element you want to animate. Just don't forget to close it off with a forward slash.

attributeName accepts any single SVG attribute within the element. In this case, x and y are both valid attributes for <text>, so one of them can be named and animated. As shown in our first @keyframes examples, from and to act identically to the CSS equivalent, and repeatCount='indefinite' is the same as the infinite value in a CSS animation. You can also write repeatDur='indefinite' instead. This reminds me of Geometry Dash triggers a lot, by the way.

As you can tell from the names, repeatCount is how many times an animation is repeated, whereas repeatDur controls the duration of each repeat.

And this is our template rendered:

We can also animate y in attributeName, as it's a valid attribute for the <text> element.

CSS keyframes can also combine to animate elements with SMIL animations! Below is an example of the text moving horizontally from SMIL, and vertically from @keyframes (translate: 0 30px):

^ Although beware, combining the two will often result in inconsistencies between the animations' starting points.

Back to our SMIL-only template, you don't need to stick to from to either. If you want to add multiple changing values over a duration of time, swap out from to with values='x;y;z'. The values are separated with semicolons, spread out based on the duration, and you can add as many as practically possible!

^ In this example, the values are 0;100;0 over a duration of 2s. SMIL animations work linearly by default, so at 0s the text is at 0, 1s is at 100 and 2s goes back to 0 - creating an effect similar to a CSS animation's alternate.

Below is an example with 0;100;50;100;0 instead:

Since we're adding more values without changing the duration, the animation noticeably speeds up.

To get something similar like steps() in CSS, you add calcMode='discrete' to the animate element. calcMode decides how SMIL animations move between values, and the accepted parameters are: linear (default), discrete, paced (ignoring keyTimes=) and spline. The latter two will be covered shortly.

Here is an example with calcMode set to discrete:

^ The text stays to the left a bit longer because of the two distinct 0 values in our <animate> element.

keyTimes allows you to set when each value within values= is reached. They function like percentages in @keyframes, and keyTimes uses 0-1 to define 0%-100%. More importantly, keyTimes must have the same amount of numbers as values=. So:

values='0;100;50;100;0' keyTimes='0;0.2;1'

is invalid, and the animation breaks:

Whereas:
values='0;100;50;100;0' keyTimes='0;0.1;0.5;0.9;1'

is valid, and produces this:

In the values set above, x is animated to 100 at 10% (0.1) of the animation, then moves smoothly to 50 at 50% (0.5) and 100 again at 90% (0.9), then snaps back to 0 at 100% (1).

But that animation looks pretty choppy.. is there a way to ease it out? Yes there is! This is what paced and spline in calcMode are for.

paced spreads out your animation values across distance instead of time. Notice how the example text we've been using spends an equal amount of time moving 100 units to the right (+100), then slowing down at 50 (-50) and 100 (+50), then speeding back up to 0 again %gray%(-100)%. Setting calcMode='paced' sets a constant speed to the whole animation so that every value gets the same, consistent speed. It cannot be used with keyTimes because paced sets its own timings.

Below is an example using paced:

^ linear red text added for reference.

🦊 Firefox notice!

Not Safari this time! In my testing, calcMode='paced' only works on Firefox, whereas Chrome and Safari seem to ignore it. That may not be the case on desktops.

Notice how the paced text remains at a constant speed within the <animate> values.

The spline value in calcMode pairs with another attribute: keySplines='x1 y1 x2 y2'. keySplines is similar to cubic-bezier in CSS, but actually accepts multiple curves for your <animate> values and separates coordinates with spaces instead of commas. There are two catches though: for every animation value gap (each ; in values=), you must add a new set of spline coordinates separated by semicolons. Like so:

keySplines='0 0 0.5 0.7; 0 0 0.5 0.3; 0 0 0.5 0.7; 0 0 0.5 0.3'

And unlike cubic-bezier, you cannot overshoot the y value for a bouncy effect - every single coordinate must stay within 0-1.

With that said, here is a rendered example using the keySplines above:

As mentioned previously, attributeName can accept any valid attribute for the element it works with (in our examples, <text>) even if it's not explicitly defined in <text> first. Below is an example of textLength being animated with 350;100;50;100;350 and normal easing:

And with lengthAdjust='spacingAndGlyphs' in <text>:

It can also animate non-numeric values! Below is an example of the text above with an extra <animate> that blinks between spacing;spacingAndGlyphs:

This should visually look like the letters keep changing width, then snapping back to normal, then so on and so forth.

<animate> is a good element for animating individual properties. But if you specifically want transforms for your text, <animateTransform> is much better at that job. A template for it is shown below:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 350 40' width='350' height='40'>
    <style>
      text {
        font-family: sans-serif;
        font-size: 30px; }
      </style>
<text x='0' y='33' fill='skyblue'> Simply saucey!
  <animateTransform 
   attributeName='transform' type='translate' 
   values='0 0; 30 0; 0 10; 0 0' dur='2s'
   repeatCount='indefinite'/> </text> </svg>)

Our <animate> changes to <animateTransform> and gains a new attribute: type! This is where you select from the available transform types: translate, rotate, scale, skewX and skewY. The attributeName='transform' is pretty much what xmlns='http://www.w3.org/2000/svg' is to <svg> - seems redundant, but it breaks if you remove it. With <animateTransform> specifically, it's always set to "transform".

This is what our template rendered looks like:

And here's type rotate:

Unlike the CSS rotate, you leave out "deg" and use only numeric values. rotate specifically can accept X and Y values after degrees to rotate on a certain point. Here it uses half of the viewBox values (so the center):

width adjusted 350 -> 250
You can get some pretty cool effects by changing where the rotate x/y values are in each value!


^ Added a little circle as a general reference for where the rotate point moves

This is skewX, with degrees as the default:

And skewY:

Do note that unlike in CSS, a single "skew" type accepting 2 values doesn't exist for animateTransform - you'll have to stack two <animateTransform> elements together and add additive='sum' to the second one.

additive='sum' composes the transform on top of whichever transforms came before it instead of overriding it by default (additive='replace'). It's not necessary for the first ever <animateTransform>.

Remember the funny results from our previous skewing texts? Heh...

You can duplicate text (including all its animations and properties) by defining it with an id and using the <use> element like so:

<use href='%23IDHERE' x='10' y='70'/>

Neat, right? It's cleaner than copy pasting the two animateTransforms...

...what have you DONE.

..Ahem! You can also set accumulate='sum' in <animateTransform> (and <animate> by extension) to make a transform build on itself. So if you had values 0;90 for rotating text, by default it would rotate 90 degrees then snap back to 0. With accumulate='sum', it would rotate 90 degrees, then 180, 270, and so on. An example is shown here:

first ever instance of using negative numbers in viewBox - can you believe it?
^ with adjusted viewBox values to show the rotation better.

But what if ordinary transforms are a bit limited for you? What if you could animate text along a path as well? Well, here we introduce <animateMotion>!

This element allows you to animate an object along a path, and it's got some pretty cool attributes! Below is a template:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 400 200' width='400' height='200'>
    <style>
      text {
        font-family: sans-serif;
        font-size: 40px; }
      </style>
<text x='0' y='40' fill='skyblue'>W <animateMotion
        path='M 0 0 L 200 100 Z'
        dur='3s' repeatDur='indefinite'/> </text> </svg>)

Most of what we'll learn right now will be focused entirely within <animateMotion>'s path= attribute. So, what are M L and Z for? This again is reminding me of the Geometry Dash move trigger, lol. If you're familiar with those I'm sure this will be a bit more intuitive to understand at first!

M is technically a move command, but M 0 0 define the initial coordinates for your path and it is actually case-sensitive (so don't write m yet)! Since we have M set at 0 0, the Motion's path begins from 0, 0. L moves your text in a straight Line to another set of x/y coordinates, and Z moves it back to its initial starting position. Z is not necessary to include at the end of every path set - L 0 0 produces an identical effect (if your other path points are also Linear), and excluding it simply makes the text snap back to the beginning.

The difference between Uppercase and lowercase path commands is that Uppercase ones use viewBox coordinates, whereas lowercase ones use relative ones. So if you have M 0 0 L 200 100, it means the point starts from 0, 0 and moves to the coordinates 200 100. The lowercase l by comparison simply moves by 200 100 relative to its current position.

SVG is also actually pretty flexible when it comes to Motion paths. You can separate them with commas or spaces, so long as the coordinates themselves aren't messed up.
This is what our example rendered looks like:

You can change the L coordinates to something else. This is with L 50 300:

You can also add more points for your text to move to! Taking our example above, this is with M 0 0, L 50 150, L 300 50, Z:

This works similar to how adding more values in an <animateTransform> works.

Having different calcModes is also possible for animateMotion! This is the example above with calcMode='discrete':

Animating along a cubic bezier path is also possible - with C! C accepts three sets of coordinates: x1 y1 x2 y2 X Y. x1 y1 is for the first control point for the cubic bezier curve, x2 y2 is the second point and X Y is the destination. Below is an example with M 0 50 C 100 -50 250 100 300 50 Z (the M y coordinate is changed for better visibility):


^ added a motion path for better viewability.

Dragging our first control point from 100 -50 to 100 -150 (upwards) gives us a stronger initial curve:

As mentioned before, Z moves the text back to its initial position on a Linear path. If you want the text to go back on the same curve, you can duplicate the initial paths and reorder the coordinates. This is with an extra C 250 100 100 -50 0 50 replacing the Z:

If you only need to move your text in a vertical or horizontal line, there exist shorthands for that - H and V! H 200 for example would only move your text 200 pixels forward (x+), and V -50 would send your text upwards (+y).

S is a Smooth cubic bezier, and it automatically continues a curve from a previous C command only. The example below uses M 0 50 C 100 -50 250 100 300 50 s 15 10 20 40 Z:

^ You may have noticed in /edit that the path coordinates within animateMotion were replaced with <mpath>. <mpath>, like the CSS url() parameter, inherits paths using #IDs (in SVG, without brackets). This makes it a bit easier for me so I don't have to retype the coordinates twice.

Quadratic Bezier is like Cubic bezier, but only uses one control point. Below is an example with M 0 50 Q 150 0 300 50 Z:

T is the Quadratic equivalent to S and only requires a single set of coordinates.

There is another final command - Arcs - which I will cover later in editing.

You can head on over to the SVG templates page to see some examples of how these paths are used!

Shapesβ €

I'll work on this later.


Safari workarounds!

This is a section that explains how to render some text properties with modified code to keep cross-browser consistency with Safari (because webkit sucks). As this doesn't apply to everything (and because I'm not in the mood for any big rewrites), all workarounds will be listed here with relevant notices instead of being integrated into the tutorial.

Examples of WebKit not knowing how to render CSS properly:

  • None of the css filter:s work.
    • In addition to that: if you use any of the SVG's built-in filters instead, WebKit will render the text rasterized at a noticeably decreased resolution - nothing fixes that. I don't know how this will affect animations yet.
  • Radial gradients have mismatching values compared to Blink/Gecko (Chrome/Firefox's engines)
  • I assume not a single Base64 font-face works either - it's only two fonts in the metadata for Safari users then...
  • transform-box is very finnicky. Just pretend it never works on Safari.

Things will be generally kept a bit shorter here.

Safari: filter:s

For a rainbow effect (instead of hue-rotate), animate each color transition manually using fill in keyframes:

For text shadows, the SVG element <feGaussianBlur> should work as a substitute - and you must duplicate the text and apply the filter to the duplicate below the main one. Since Safari renders SVG-filtered text at a lower res, it doesn't matter much for text shadows:

^ Template:

βŽ—
βœ“
![](data:image/svg+xml,
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 220 70' width='220' height='70'>
    <defs>
      <filter id='fGB' x='-0.2' y='-0.5' width='1.5' height='2'>
        <feGaussianBlur stdDeviation='2'/>
      </filter> </defs>
    <style>
      text { fill: red; font-family: sans-serif; font-size: 44px; }
    .shad { fill: black; filter: url%28%23fGB%29; }
       </style>
    <text class='shad' x='10' y='50'>Test</text>
  <text x='10' y='50'>Test</text></svg>)

The previous short section about <feDropShadow> explains what the values do. stdDeviation functions the same as the blur filter. Don't forget text-shadow still works fine, albeit still blurrier/more distant on Chrome/Safari than Firefox.

For everything color related (brightness, contrast, sepia, etc) just adjust the color values manually - the SVG filters crunch your text's resolution.

Safari: transform-boxes

Safari struggles a lot when you use any other transform-box: other than view-box. For any cool animations you're making, I recommend setting it to view-box and defining your text's coordinates manually in transform-origin - although slightly adjusted to its center (so not the same as the <text> coords) if you aren't also using text-anchor: middle;

Safari: <radialGradient>s

image showing radial gradient differences btwn browsers

I have just as much clue as you do, and I don't think there's a way to make it cross-browser consistent. If exact radial gradient parameters aren't important to you, this shouldn't be a problem. Otherwise, I'll maybe test out how different they are exactly later, but not right now.


Edit

Pub: 20 Nov 2026 19:06 UTC

Edit: 03 Dec 2026 20:08 UTC

Views: 136

Auto Theme: Dark