# Using Inline SVGs With HTML5

It goes without saying that SVG isn't as widely used as many people in the web development community might believe it should be. Setting the debate aside, in this tutorial, I will demonstrate how to use inline SVGs, covering a range of techniques and exploring the interaction between web page and graphic. When used in conjunction with other emerging standards in HTML5, JavaScript and CSS3, inline SVGs can significantly add to the user experience.

## Step 1: Create an HTML5 Page

You can interact with the SVG elements using DOM techniques, as you would with other web page elements.

In this tutorial, we are going to work through a practical example of an inline SVG by creating a simple component representing a record deck. The record will spin, and the user will be able to interact with it - pressing to slow it down, and releasing to speed it up again. The SVG will also contain a button which users can click to change the record, causing its appearance to alter slightly. The visible SVG elements will be enhanced with gradients and drop-shadow filters.

Check out the demo now so that you have a clear idea of what we are building.

As you likely will be aware, inline SVGs fall within the emerging HTML5 standards, so the code we use in this tutorial will not be fully supported across all browsers. Although inline SVGs are theoretically supported in all current versions of the major browsers, the internal animations and interactive effects that we'll use are not so well supported just yet. The final result should function correctly in current versions of Firefox, Chrome and Opera. As always with HTML5 techniques, make sure you don't rely on these effects in any live sites you work on, and include alternatives where possible.

Let's dig in, and begin by creating an HTML5 page outline, like so:

One of the primary advantages to using SVG is how scalable it is. To exploit this, we are primarily going to use relative values to define the SVG content. Add a container element for the SVG in the page body:

To see how the SVG sits within the containing element, add the following to the style section in your page head:

## Step 2: Add the SVG Element

In the container element in the body of your page, add the SVG element outline, as follows:

We've set the width and height to 100%, as we are going to specify the width of the containing element. However, rather than specifying the dimensions explicitly, we'll instead use a JavaScript function to reveal how easily you can scale the SVG up and down. You can include a fixed width and height in the opening SVG tag or the style section.

## Step 3: Create the Definitions

The visible shapes in the graphic will be defined inside the SVG element. Before that, we will work on the defs section. The defs section is where you place definitions that you can later refer to when creating your shapes. For this tutorial, the defs section is going to contain definitions for a few gradient fills and a couple of drop shadows. Add this new section inside the SVG element:

The items we place in this section will not actually appear in the image, but will be used as fills and filters for the shapes which do. We'll include several gradient fills; so let's work through each one in turn.

First up is a linear gradient:

This gradient is going to be specified as the fill for the background rectangle area. The x1 and y1 attributes represent the starting points of the gradient within the filled shape, with the gradient unfolding from there to the point represented by x2 and y2. In this case, the gradient will run from top to bottom. The stop elements represent color points in the gradient. The first one states that 10% from the start of the gradient will be a solid dark red color and the second stop that 90% from the end of the gradient will be a yellow color. Between these two points, the gradient will blend the colors into one another. Both colors have full opacity.

Next let's add a gradient for the record itself. This one is a bit more complex - it's a radial gradient with several color stops:

A radial gradient starts from the inside of the circle, with the innermost and outermost parts of the circle defined by cx, cy, fx and fy, listed alongside the radius. In this case, the radial gradient is going to occupy the whole of the circular record shape. The bulk of the record will be black, with two rings of slightly lighter color representing the smoother sections at center of the record and its edges. We will be placing a label on the record at its center too, so the first patch of lighter color on the record will appear just outside that. Add the label gradient fill next:

This is a simple linear gradient that will be used as fill for the circular record label. However, notice that the gradient ID has a zero on the end of it. This is due to the fact that we're going to add an interactive function, allowing the user to "change the record". A JavaScript function will toggle between a range of gradient fills for the label element. For this purpose, add another couple of gradients:

The gradients each have an ID ending with an incrementing integer, so that we can iterate through them in JavaScript. Now, define another gradient to create a shine effect on top of the record:

This time, the gradient uses opaque and alpha transparent color stops; the effect will be a subtle shine across the record. Finally, we need a metallic fill for the button and spindle:

This time, the radial gradient is slightly off center to create a sense of depth and light, which will be complemented by a drop shadow filter.

### Filters

Before we finish with the defs section, add a couple of drop shadows to give some of the shapes a bit more depth:

This one is going to appear behind the record area. The x, y, width and height properties refer to the position and dimensions within the shape using this filter. The offset defines the shadow relative to the original shape. The blur prevents the offset shape from being solid color, so that it appears as a shadow. In this case only the shadow will appear, not the shape itself - the shadow is going to be defined by a dedicated shape which will be placed behind the record shape. For the user controls, which are circular and metallic, we also want a drop shadow but we want the shape itself to display as well:

This main difference here, apart from the scale of the shadow, is the blend element, which will preserve the original shape while also showing the shadow around it.

## Step 4: Create the Shapes

That's enough preparation; let's get on with the graphics! Each item you add to the body of the SVG will be displayed on top of previously listed elements, so we will work from the bottom up, starting with the shapes at the back and ending with those at the front.

### Background Rectangle

First, add a rectangle shape for the background:

The rect element dimensions and position are specified relative to the containing SVG, which, if you remember, is relative to the size of the containing element. We will set this later in JavaScript. We will use relative size and position values wherever possible, so that the entire image plus animation and interaction can scale up or down on demand. Notice that the element fill specifies one of the gradients we defined, using its ID attribute.

Next up from the bottom is the record shadow, using one of the drop shadow filters we created:

The shadow is going to lie behind the record, as a circular shape with a radius that is roughly a third of the space allocated to the image, placed in the center. Since the filter in this case does not apply blending with the image, the circle itself will not appear, just its shadow.

### Record

Next up is the record itself:

As with the shadow, the cx and cy attributes represent the center of the record, which is centered in the image horizontally and vertically, with a radius of about a third. Again, we use one of the gradients we defined, which we will do in each shape.

### Label

On top of the record is its label, so add it next:

The label circle has the same central point as the record, across which it stretches about a third of the way. We start with the first of the label gradient options we defined, and will implement the user switching between these later - we include an ID attribute here to refer to this element in JavaScript.

### Shine

Now, let's put some shine on top of the record:

When the record spins, it is going to move to the right and down just a little, so we keep the shine slightly smaller than the record so that it does not appear to spread beyond it when it moves. This element also has an ID attribute to detect user interaction.

### Spindle

For completeness, let's add a little spindle in the center of the record:

This shape uses the metallic gradient we created. We also apply the second drop shadow filter, which includes blending so that the shape and shadow both appear.

### Button

Last but not least, we need a little button for users to control changing the record, using the same fill and filter as the spindle:

This time, instead of a self-closing element, we separate the opening and closing circle tags. This is because we are going to animate the button when users click it, and will include the animation effect between these tags. Notice that we have been able to reuse fill and filter elements from the defs section. Here is the initial appearance of the graphic once the page dimensions are in place:

Each item you add to the body of the SVG will be displayed on top of previously listed elements.

Now we have our visual elements in place, let's add some animation. We can make the record spin using SVG animation transformations, which are an extension of SMIL animation. These animated effects are defined within the SVG markup. An effect applies to whatever SVG element it appears within. You can use CSS3 transforms on SVG elements, but the SMIL-based alternatives give you a greater level of control.

We are going to include two simple animations: the record is going to spin and the button is going to move a little when the user clicks it. Let's start with the slightly more straight-forward animation for the button.

Inside the button shape element, between the opening and closing circle tags we created, add the animate transform as follows:

The animateTransform applies to an XML attribute within the element it appears in. In this case, it is a translate transform. The from and to attributes represent the start and end positions for the element - these are relative to its starting position, so the button is going to move to the right and down by a single pixel. The transform will begin when a user clicks, elapse over one tenth of a second, and execute once. The button will return to its original position when the animation completes. Tip: To keep an element in the end position after an animation, specify fill="freeze".

Now for spinning the record. An animateTransform applies to an SVG element, but we need the spin to apply to more than one element - specifically to the record and the label (not to the shine or shadow). Rather than creating separate animations for each and executing them concurrently, we can use a single transform by grouping these elements together. Before the circle element representing the record (with "recordGrad" as its fill) add an opening group tag:

After the circle representing the label, close the group:

Now add the transform before this closing group tag so that it applies to the whole group:

This time, the animated effect is a rotate transform. The element will rotate by 360 degrees, and, to add to the effect, it will move to the right and down by a single pixel on each rotation, over a period of one second, repeating indefinitely. This transform will also include a from attribute, as it is necessary to specify the initial position of the elements being rotated. If you do not specify this position, the elements rotate around the 0, 0 point by default. However, at the moment, you cannot supply relative (i.e. percentage) values to these attributes, only fixed values. For this reason, we are going to set the from attribute when we specify the SVG dimensions in JavaScript.

Now let's implement our interactive functions: clicking the button to change the record and pressing the record to slow it down.

### Change the Record

First, in the script section of your page head, add these variables to count and keep track of the label designs:

Now, inside the opening tag for the circle element representing the button (which now has an animation between its tags), add the following click event listener:

Each time the user presses the button, we will move to the next label, moving back to the first when we reach the last one:

The last line here demonstrates how you can interact with the SVG elements using DOM techniques, as you would with other web page elements. Here, we set the fill attribute of the label circle element to use the next gradient fill, specifying the fill ID.

### Slow it Down

Now add the following event attributes to the record shine element (with "shineGrad" as its fill), as we are going to use mouse down and up events on it to trigger slowing the record down and speeding it up again:

Back in the script section, add the function for when a user is pressing the record:

Inside this function, we can slow the record spinning animation by altering the animateTransform duration attribute. We also alter the shine opacity to create the impression of pressing down:

When the user releases the record, we want it to go back to normal speed and appearance, so add the "mouse up" function next:

## Step 7: Specify the Size

We can finally set the overall size of the SVG now. At the top of the script section, add a new variable:

We will initially use 300 pixels for both the width and height of the graphic, but you are free to alter this at any point. Define a function in the script section to set these dimensions:

We set the size on the containing div element. Take a moment to look at the final line in this function. Since the rotate transform animation cannot use relative percentage values, we must set the from element using the size variable (divided by two for the central point of the record). With 300 as the SVG size, this is how the transform would appear with fixed values:

If you want to use fixed values in your SVG you can do so. We are only using this technique to demonstrate using relative dimensions. Finally, call this function at the end of the script section:

## Conclusion

Our interactive SVG animation is now complete! Open your page in a supporting browser to view the effect; don't forget to try interacting with the record and button. Try altering the size variable to see how the SVG elements all adapt to suit, including the animations and interactions.

If you'd like to explore SVG further, some topics to consider include paths, text, masking and clipping. There are also a range of additional animation options to consider. Of course, these effects will not work for all users right now, but hopefully one day soon...