SVG text outline fun
I’ve lately been a bit obsessed with SVG. I was an illustrator in a past life, and this new found realization that I can do a little sketch, wave a magic wand, copy paste a long, complicated string of characters that look like jibberish, and boom, render this illustration on a screen was quite possibly the coolest thing in the world.
This realization led to little projects like this, this, and this.
So, what is an SVG? Short for Scalable Vector Graphic, an SVG is a XML-based vector illustration format that can be edited and created with both text editors and drawing software. SVGs are interactive and animatable and can be styled with CSS and scripted with Javascript. You can create paths, basic shapes, text, gradients, and more, which make SVGs an incredible tool for anybody interested in graphic art and front-end design.
Much of what I did in the examples I linked above included creating an SVG in drawing software (Since my Creative Cloud membership has lapsed, I’m currently using Autodesk Graphic for Ipad which allows you to export illustrations to SVG) and then dynamically changing the CSS styling based on scroll position.
In this post, I’ll demonstrate something super simple (If you couldn’t tell, I’m a little bit obsessed with giraffes):
What’s cool about SVGs is that you can style it with inline CSS or use an external CSS stylesheet, and you can use CSS to color and style the fills and strokes of any element you create.
And the CSS:
What you see here is an SVG with a text element. We give the element a class name and then specify its font-size, spacing, and font-family with CSS. The fun part here is that by giving the element a stroke and fidgeting with the opacity of the fill, we can give our text a fun outline. The stroke and fill can be specified in the separate CSS file as well, but I'm about to add a fun gradient, so I'm keeping it all inline for now.
Disregarding the text for a moment, here’s an example of using the <linearGradient> element to define a gradient to your fill or stroke. We’re applying it to the fill of a simple rectangle element right now. Now, if we apply it to our text stroke and make its fill-opacity 0, here’s what we get:
Awesome!
Now, how do we animate this? With each stroke, we have two attributes that we can manipulate: stroke-dasharray and stroke-dashoffset. The stroke-dasharray attributes controls the pattern of dashes and gaps. Add the following to your CSS file to see how it changes:
stroke-dasharray: 5,5
stroke-dasharray: 25,5
The stroke-dashoffset specifies the distance into the dash to start the dash pattern. Using keyframe animations, if you set the stroke-dasharray to 5,5 and change the stroke-dashoffset from 0 to 20, you get something like this:
If we give the stroke-dasharray a length that is equal to the stroke length, and change the stroke-dashoffset from 0 to a length that is also equal to the stroke length, you can make it seem like the text is drawing itself.
Here's my final code:
CSS:
Checkout the codepen here: http://codepen.io/5ftwndr/pen/xdpRYv











