Animating SVG elements are quick, easy, and do not add a lot of ‘weight’ to your page load. Learn to use CSS keyframes to add an animation within an SVG file. This technique will work in most modern browsers with the exception of the Microsoft browsers, which include Internet Explorer 11 and the Edge browser.
- [Chris] Hi. This is Chris Converse and in this episode we'll use CSS keyframes to add in animation within an svg file. This technique will work in all modern browsers, with the exception of Microsoft's Internet Explorer and Edge browsers, which at the time of this recording, make up less than 14% of the browsers used worldwide. If you'd like to follow along, download the exercise files and we'll begin by opening the HTML file and then Text Editor. Once you have the HTML file open, you'll see in the body area we have a header. Inside of the header, there's an image reference that points to title.svg.
This is the graphic here with the sun graphic and the word sunlight. There's also a background graphic being assigned into the header element through CSS which uses another svg file. It's that svg file where were going to be adding our animation. To continue, let's go back to the exercise files, and inside the Images directory, let's open the sun_rays.svg file in our Text Editor. Now inside the svg file, you'll see that we have a definitions element set up. Then inside of there, we have the style element, and all of our CSS is inside of a CDATA comment here because this is a XML file.
To begin, let's get our cursor after the rule that targets the # of rays space path and we're going to add our keyframes. So we'll start with an at symbol, type in keyframes, then a space. Next we'll name the animation. We'll call this rotate_rays. Add a space. Put in our curly brackets. We'll split this open. For the first keyframe position, we're going to set 0%. Then I'll add a few spaces. Add in another set of curly brackets and we're going to set the 0% keyframe transform property to zero degrees.
So we'll add the transform property, colon, space, then type rotate, beginning and ending parenthesis, then a semicolon. Inside of the parentheses, we're going to type zero then deg for degrees. Next let's duplicate that line. Let's bring the cursor back to the beginning. Let's set this keyframe at the 100% mark. We'll keep the transform property and let's change the rotation to 360 degrees.
Now that we have our keyframes defined, we need to apply this animation to the rule that targets the rays #. Up here we can see #rays. This targets this group down here inside of our svg. Let's come up here after transform-origin. Let's hit a return. Let's type in the animation property. Colon, space. I'll come down here, selecting copy the rotate_rays name. Let's paste it up here. Then add a space.
Let's type in 55s for 55 seconds. We want this to take 55 seconds to do a complete rotation. Then a space. We'll type in the word infinite. We want it to loop forever. We're going to set the animation physics to linear. This way the animation will move in a constant state. With all of these in place, let's hit Save in the svg file. Go back to the browser and hit Reload. We'll now see that we have our rays # rotating around 360 degrees over the course of 55 seconds.
Again, this animation is contained inside of the svg file. So any browser that supports transformers inside svg will have this animation in place. With that, I'll conclude this episode. As always, thanks for watching.
Author
Updated
12/11/2018Released
12/6/2016Skill Level Intermediate
Duration
Views
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.
Related Courses
-
CSS to Sass: Converting an Existing Site
with John Riviello2h 31m Intermediate -
CSS: Animation
with Val Head1h 59m Intermediate
-
Introduction
-
Welcome34s
-
-
February 2019
-
Multiple background images3m 34s
-
OpenType swashes2m 36s
-
Photo gallery, part 14m 17s
-
Photo gallery, part 26m 38s
-
-
January 2019
-
Accordion panels5m 39s
-
Border image3m 54s
-
Aspect ratio3m 37s
-
Style by download type6m 51s
-
-
December 2018
-
Every nth element4m 58s
-
Layout patterns5m 17s
-
Style placeholder text2m 25s
-
-
July 2018
-
Diamond-shaped image crop5m 50s
-
Responsive iframes5m 42s
-
Create a star10m 33s
-
Color SVG with CSS5m 45s
-
Style a progress bar10m 48s
-
Curve your header4m 4s
-
Striped pattern with CSS4m 46s
-
-
June 2018
-
Clip an image3m 53s
-
Vertically aligned columns2m 47s
-
Gradient text4m 2s
-
May 2018
-
Create a speech bubble6m 20s
-
Animate SVG elements3m 37s
-
See more links7m 54s
-
April 2018
-
Create a vignette5m 47s
-
Create variables6m 33s
-
CSS not selector4m 4s
-
Parallax scroll effect4m 26s
-
-
March 2018
-
Filter a list7m 35s
-
3D shadow text3m 25s
-
Show URLs when printing4m 19s
-
Style breadcrumb links5m 27s
-
-
February 2018
-
Box sizing to the rescue4m 24s
-
Set text on an angle4m 12s
-
Triangle bullets3m 51s
-
Force text to wrap2m 8s
-
-
January 2018
-
Animate a sprite sheet5m 19s
-
Style telephone links4m 12s
-
December 2017
-
Sticky footers2m 45s
-
Selection colors3m 28s
-
November 2017
-
Detect screen resolution5m 1s
-
Continue a numbered list4m 46s
-
Add a QR code for print4m 56s
-
Interactive tab panels7m 23s
-
-
October 2017
-
Outline your text2m 56s
-
Create polka dots3m 42s
-
Gradient horizontal rule3m 22s
-
Embed SVG art in CSS3m 15s
-
Style big list numbers4m 57s
-
-
September 2017
-
Text indents3m 44s
-
Display images as grayscale2m 57s
-
Customized radio buttons7m 37s
-
-
August 2017
-
Indicating off-site links4m 15s
-
Creating a pie chart8m 42s
-
Numbering items3m 44s
-
Animating backgrounds3m 55s
-
Angled headers3m 55s
-
-
July 2017
-
Checkboxes to switches9m 36s
-
Blurring images5m 18s
-
Overlapping heading rule4m 4s
-
-
June 2017
-
Center A positioned element5m 32s
-
Put your best footer forward11m 23s
-
Mixin' it up with SASS12m 4s
-
Mixin' it up with LESS13m 15s
-
-
May 2017
-
Drop the cap5m 41s
-
Get nestled with SASS7m 4s
-
Get nestled with LESS6m 11s
-
Flex your columns5m 34s
-
Let’s focus on the focus5m 44s
-
-
April 2017
-
Expand your mobile menu6m 38s
-
Vertical and center6m 32s
-
Creating a grid9m 33s
-
-
March 2017
-
Get your links in a row8m 28s
-
Give me a few tips8m 48s
-
Setting a stylish table6m 8s
-
-
February 2017
-
Hover in a hover4m 24s
-
January 2017
-
A button for your link5m 16s
-
Visiting the visited link6m 47s
-
You can quote me5m 34s
-
A glowing property10m 11s
-
Dress up that link4m 54s
-
-
December 2016
-
Give me some background10m 45s
-
What’s your position?11m 19s
-
Let me float this by you6m 56s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Animate SVG elements